Skip to content

Commit

Permalink
No public description
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 684560175
  • Loading branch information
colaboratory-team committed Oct 10, 2024
1 parent dba0ea7 commit 6232d48
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions google/colab/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,14 @@ def upload_file(filename):
)


def upload():
def upload(target_dir=''):
"""Render a widget to upload local (to the browser) files to the kernel.
Blocks until the files are available.
Args:
target_dir: If provided, corresponds to the directory to write the files to.
Returns:
A map of the form {<filename>: <file contents>} for all uploaded files.
"""
Expand All @@ -71,11 +74,16 @@ def upload():
local_filenames = dict()
uploaded_files_with_updated_names = dict()

# If there was a specified target directory, create it.
if target_dir:
_os.makedirs(target_dir, exist_ok=True)

for filename, data in uploaded_files.items():
local_filename = local_filenames.get(filename)
complete_filename = _os.path.join(target_dir, filename)
local_filename = local_filenames.get(complete_filename)
if not local_filename:
local_filename = _get_unique_filename(filename)
local_filenames[filename] = local_filename
local_filename = _get_unique_filename(complete_filename)
local_filenames[complete_filename] = local_filename
print(
'Saving {filename} to {local_filename}'.format(
filename=filename, local_filename=local_filename
Expand Down

0 comments on commit 6232d48

Please sign in to comment.