Spaces:
Sleeping
Sleeping
File size: 2,872 Bytes
dd3611a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/helper_utilities.ipynb.
# %% auto 0
__all__ = ['check_is_colab', 'MultiFileChooser', 'setup_drives']
# %% ../nbs/helper_utilities.ipynb 3
import ipywidgets as widgets
from IPython.display import display, clear_output
from functools import partial
from ipyfilechooser import FileChooser
import os
# %% ../nbs/helper_utilities.ipynb 4
def check_is_colab():
"""
Check if the current environment is Google Colab.
"""
try:
import google.colab
return True
except:
return False
# %% ../nbs/helper_utilities.ipynb 7
class MultiFileChooser:
def __init__(self):
self.fc = FileChooser('.')
self.fc.title = "Use the following file chooser to add each file individually.\n You can remove files by clicking the remove button."
self.fc.use_dir_icons = True
self.fc.show_only_dirs = False
self.selected_files = []
self.fc.register_callback(self.file_selected)
self.output = widgets.Output()
def file_selected(self, chooser):
if self.fc.selected is not None and self.fc.selected not in self.selected_files:
self.selected_files.append(self.fc.selected)
self.update_display()
def update_display(self):
with self.output:
clear_output()
for this_file in self.selected_files:
remove_button = widgets.Button(description="Remove", tooltip="Remove this file")
remove_button.on_click(partial(self.remove_file, file=this_file))
display(widgets.HBox([widgets.Label(value=this_file), remove_button]))
def remove_file(self, button, this_file):
if this_file in self.selected_files:
self.selected_files.remove(this_file)
self.update_display()
def display(self):
display(self.fc, self.output)
def get_selected_files(self):
return self.selected_files
# %% ../nbs/helper_utilities.ipynb 12
def setup_drives(upload_set):
upload_set = upload_set.lower()
uploaded = None
# allow them to mount the drive if they chose Google Colab.
if upload_set == 'google drive':
if check_is_colab():
from google.colab import drive
drive.mount('/content/drive')
else:
raise ValueError("It looks like you're not on Google Colab. Google Drive mounting is currently only implemented for Google Colab.")
# Everything else means that they'll need to use a file chooser (including Google Drive)
if check_is_colab():
from google.colab import files
uploaded = files.upload()
else:
# Create file chooser and interact
mfc = MultiFileChooser()
mfc.display()
uploaded = mfc.get_selected_files()
return uploaded
|