Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A file selection class build for ipywidgets without any extra dependencies.
import os
import ipywidgets as widgets
class FileBrowser(object):
def __init__(self):
self.path = os.getcwd()
self._update_files()
def _update_files(self):
self.files = list()
self.dirs = list()
if(os.path.isdir(self.path)):
for f in os.listdir(self.path):
ff = self.path + "/" + f
if os.path.isdir(ff):
self.dirs.append(f)
else:
self.files.append(f)
def widget(self):
box = widgets.VBox()
self._update(box)
return box
def _update(self, box):
def on_click(b):
if b.description == '..':
self.path = os.path.split(self.path)[0]
else:
self.path = self.path + "/" + b.description
self._update_files()
self._update(box)
buttons = []
if self.files:
button = widgets.Button(description='..', background_color='#d0d0ff')
button.on_click(on_click)
buttons.append(button)
for f in self.dirs:
button = widgets.Button(description=f, background_color='#d0d0ff')
button.on_click(on_click)
buttons.append(button)
for f in self.files:
button = widgets.Button(description=f)
button.on_click(on_click)
buttons.append(button)
box.children = tuple([widgets.HTML("<h2>%s</h2>" % (self.path,))] + buttons)
# example usage:
# f = FileBrowser()
# f.widget()
# <interact with widget, select a path>
# in a separate cell:
# f.path # returns the selected path
@DrDub

This comment has been minimized.

Copy link
Owner Author

commented Jan 3, 2016

This code is dedicated to the Public Domain.

@thomasaarholt

This comment has been minimized.

Copy link

commented Jun 1, 2017

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!

@rstofi

This comment has been minimized.

Copy link

commented 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[1] == '/':
                    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[0] != '.': in the _update_files(self) function.

Although it is a very useful piece of code!

@acelere

This comment has been minimized.

Copy link

commented Apr 8, 2018

Nice code! Thanks for sharing.

@LukasCBossert

This comment has been minimized.

Copy link

commented Jul 25, 2018

very nice! Thanks for sharing!

@masaguaro

This comment has been minimized.

Copy link

commented 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

The 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.