|import ipywidgets as widgets|
|self.path = os.getcwd()|
|self.files = list()|
|self.dirs = list()|
|for f in os.listdir(self.path):|
|ff = self.path + "/" + f|
|box = widgets.VBox()|
|def _update(self, box):|
|if b.description == '..':|
|self.path = os.path.split(self.path)|
|self.path = self.path + "/" + b.description|
|buttons = |
|button = widgets.Button(description='..', background_color='#d0d0ff')|
|for f in self.dirs:|
|button = widgets.Button(description=f, background_color='#d0d0ff')|
|for f in self.files:|
|button = widgets.Button(description=f)|
|box.children = tuple([widgets.HTML("<h2>%s</h2>" % (self.path,))] + buttons)|
|# example usage:|
|# f = FileBrowser()|
|# <interact with widget, select a path>|
|# in a separate cell:|
|# f.path # returns the selected path|
Oct 9, 2017
Nice code, but there is some issues if you try to go up to the root directory. I suggest to delete line 38 if statement:
if self.files: after this an other error will occur if you go to root and then you try to go back some subdirectories. Thus I suggest to add after line 33:
### fix the problem occurring when go up to root and come back ### try: if self.path == '/': self.path = self.path[1:]; except: pass;
Also for me it is useless to list hidden files and directories. I easily got rid of them using: the
if f != '.': in the
Although it is a very useful piece of code!
Apr 8, 2018
Nice code! Thanks for sharing.
Jul 25, 2018
very nice! Thanks for sharing!
Sep 28, 2018
Has anyone tried to use it in a Jupyter Notebook? I have tried the following, and it's not working:
try: from selectfile import FileBrowser train_file_picker = FileBrowser() train_file_picker.widget() df = pd.read_csv(train_file_picker.path) except ImportError: pass
train_file_picker.widget() does not output any browser. Indeed, I have tried to do something like, and it does not work neither:
if flag == 2: train_file_picker.widget()
It seems that train_file_picker.widget() is not working if it's used inside a conditional statement in Jupyter Notebook. Any idea or suggestion will be appreciated. Thanks
Jan 2, 2020
Is there any means for the user to choose the file to be uploaded from a file browser widget?
Feb 3, 2020
@gabbyteku. If you mean if there's any way to use the operating system file widget, if you are under GNU/Linux using KDE you can use the program /usr/bin/kdialog but it'll be launched from the backend (not from the browser) so the user experience will be confusing (it'll not render the browser modal nor will bring the dialog on top of it). For a cross-platform option, you can use jfilechooser. Just keep in mind the python code runs in the backend, not in the browser.
If you mean if there's any way in the above code to pre-select a file, not with the code above, but you can change the constructor to set the path to the desired file from the constructor:
def __init__(self, starting_path=os.getcwd()): self.path = starting_path self._update_files()
Feb 3, 2020
@masaguaro The widget has to be the last item in the cell. The code above states "in a separate cell, access f.path". In your example, you need to split into two cells and access
train_file_picker.path in the second cell.
@thomasaarholt will do, I'm trying to put an improved version out.
@rstofi nice points, I'll try to add them into an improved version.
Feb 3, 2020
@DrDub Thanks. I will keep it in mind for the next time.
Nice! I'd suggest replacing the instances of ``... + "/" + ...
withos.path.join()` to support Windows as well.
If there are other solutions available now (2017), I'd be interesting in hearing of them!