Created
July 15, 2019 18:31
-
-
Save shreddd/5f73a258a46ad87a903ddb2c780702f5 to your computer and use it in GitHub Desktop.
filepicker ipywidget
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "cells": [ | |
| { | |
| "cell_type": "code", | |
| "execution_count": 1, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "from ipywidgets import widgets, Layout\n", | |
| "import os" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 2, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "class FileBrowser(object):\n", | |
| " \n", | |
| " def __init__(self, path=None): \n", | |
| " self.path = path or os.getcwd()\n", | |
| " self._update_files()\n", | |
| " self.filepicker = self.widget() \n", | |
| " self.value = self.filepicker.value\n", | |
| " self.filepicker.observe(self._update)\n", | |
| " display(self.filepicker)\n", | |
| "\n", | |
| " @property\n", | |
| " def filename(self):\n", | |
| " if self.value == \"..\" or self.value is None:\n", | |
| " name = \"\"\n", | |
| " else:\n", | |
| " name = self.value\n", | |
| " return os.path.join(self.path, name)\n", | |
| " \n", | |
| " def _update_files(self):\n", | |
| " self.files = list()\n", | |
| " # Seed with parent dir\n", | |
| " self.dirs = list([\"..\"])\n", | |
| " if(os.path.isdir(self.path)):\n", | |
| " for f in os.listdir(self.path):\n", | |
| " ff = os.path.join(self.path, f)\n", | |
| " if os.path.isdir(ff):\n", | |
| " self.dirs.append(f + \"/\")\n", | |
| " else:\n", | |
| " self.files.append(f)\n", | |
| " \n", | |
| " def widget(self):\n", | |
| " # box = widgets.VBox()\n", | |
| " box = widgets.Dropdown(\n", | |
| " options=self.dirs + self.files,\n", | |
| " value=None,\n", | |
| " description=self.path,\n", | |
| " style={'description_width': 'initial'},\n", | |
| " layout=Layout(width='75%')\n", | |
| " )\n", | |
| " return box\n", | |
| " \n", | |
| " def _update(self, change):\n", | |
| " if change['type'] == 'change' and change['name'] == 'value':\n", | |
| " self.value = change['owner'].value\n", | |
| " if self.value == '..':\n", | |
| " target = os.path.split(self.path)[0]\n", | |
| " else: \n", | |
| " target = os.path.join(self.path, self.value)\n", | |
| " if os.path.isdir(target):\n", | |
| " self.path = target\n", | |
| " self._update_files()\n", | |
| " self.filepicker.options = self.dirs + self.files\n", | |
| " self.filepicker.description = self.path\n", | |
| " self.filepicker.value = None" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 3, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "application/vnd.jupyter.widget-view+json": { | |
| "model_id": "9a6f923c475f4a2090f2e3e1a23c4c72", | |
| "version_major": 2, | |
| "version_minor": 0 | |
| }, | |
| "text/plain": [ | |
| "Dropdown(description='/global/u1/s/shreyas', layout=Layout(width='75%'), options=('..', 'R/', 'safenet_etoken/…" | |
| ] | |
| }, | |
| "metadata": {}, | |
| "output_type": "display_data" | |
| } | |
| ], | |
| "source": [ | |
| "f = FileBrowser()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 5, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'/global/u1/s/shreyas/pgsql/doc/edblogo.png'" | |
| ] | |
| }, | |
| "execution_count": 5, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "f.filename" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [] | |
| } | |
| ], | |
| "metadata": { | |
| "kernelspec": { | |
| "display_name": "Python 3", | |
| "language": "python", | |
| "name": "python3" | |
| }, | |
| "language_info": { | |
| "codemirror_mode": { | |
| "name": "ipython", | |
| "version": 3 | |
| }, | |
| "file_extension": ".py", | |
| "mimetype": "text/x-python", | |
| "name": "python", | |
| "nbconvert_exporter": "python", | |
| "pygments_lexer": "ipython3", | |
| "version": "3.6.8" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 2 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment