Skip to content

Instantly share code, notes, and snippets.

@DrDub
Created January 3, 2016 11:44
  • Star 22 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
Star You must be signed in to star a gist
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
Copy link
Author

DrDub commented 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()

@DrDub
Copy link
Author

DrDub commented 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.

@masaguaro
Copy link

@DrDub Thanks. I will keep it in mind for the next time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment