{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "run_control": { "frozen": false, "read_only": false } }, "outputs": [], "source": [ "from __future__ import print_function\n", "import sys, os\n", "sys.path.insert(0, os.path.abspath('..'))\n", "import ipywe.fileselector\n", "import ipywidgets as ipyw" ] }, { "cell_type": "markdown", "metadata": { "run_control": { "frozen": false, "read_only": false } }, "source": [ "# Single file selector with seach tool" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "run_control": { "frozen": false, "read_only": false } }, "outputs": [], "source": [ "fsel= ipywe.fileselector.FileSelectorPanel(instruction='select file', start_dir='.')\n", "fsel.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "run_control": { "frozen": false, "read_only": false } }, "outputs": [], "source": [ "print(fsel.selected)" ] }, { "cell_type": "markdown", "metadata": { "run_control": { "frozen": false, "read_only": false } }, "source": [ "# Single file selector with filter " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "run_control": { "frozen": false, "read_only": false }, "scrolled": false }, "outputs": [], "source": [ "fsel= ipywe.fileselector.FileSelectorPanel(\n", " instruction='select file', start_dir='.', \n", " filters={'notebooks': ['*.ipynb'],'csv': ['*.csv']}, default_filter='notebooks')\n", "fsel.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "run_control": { "frozen": false, "read_only": false } }, "outputs": [], "source": [ "print (fsel.selected)" ] }, { "cell_type": "markdown", "metadata": { "run_control": { "frozen": false, "read_only": false } }, "source": [ "# Single directory selector" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "run_control": { "frozen": false, "read_only": false } }, "outputs": [], "source": [ "fsel= ipywe.fileselector.FileSelectorPanel(instruction='select file', start_dir='.', type='directory')\n", "fsel.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "run_control": { "frozen": false, "read_only": false } }, "outputs": [], "source": [ "print (fsel.selected)" ] }, { "cell_type": "markdown", "metadata": { "run_control": { "frozen": false, "read_only": false } }, "source": [ "# Single directory selector with \"new directory\" button" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "run_control": { "frozen": false, "read_only": false } }, "outputs": [], "source": [ "fsel= ipywe.fileselector.FileSelectorPanel(\n", " instruction='select file', start_dir='.', type='directory', newdir_toolbar_button=True)\n", "fsel.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "run_control": { "frozen": false, "read_only": false } }, "outputs": [], "source": [ "print (fsel.selected)" ] }, { "cell_type": "markdown", "metadata": { "run_control": { "frozen": false, "read_only": false } }, "source": [ "# Multiple files selector" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "run_control": { "frozen": false, "read_only": false } }, "outputs": [], "source": [ "fsel= ipywe.fileselector.FileSelectorPanel(instruction='select file', start_dir='.', multiple=True)\n", "fsel.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "run_control": { "frozen": false, "read_only": false } }, "outputs": [], "source": [ "print (fsel.selected)" ] }, { "cell_type": "markdown", "metadata": { "run_control": { "frozen": false, "read_only": false } }, "source": [ "# Multiple directories selector" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "run_control": { "frozen": false, "read_only": false } }, "outputs": [], "source": [ "fsel= ipywe.fileselector.FileSelectorPanel(instruction='select file', start_dir='.', type='directory', multiple=True)\n", "fsel.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "run_control": { "frozen": false, "read_only": false } }, "outputs": [], "source": [ "print (fsel.selected)" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true, "run_control": { "frozen": false, "read_only": false } }, "source": [ "# Single file selector with callback" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "run_control": { "frozen": false, "read_only": false } }, "outputs": [], "source": [ "def callback(selected): print(selected)\n", "fsel= ipywe.fileselector.FileSelectorPanel(instruction='select file', start_dir='.', next=callback)\n", "fsel.show()" ] }, { "cell_type": "markdown", "metadata": { "run_control": { "frozen": false, "read_only": false } }, "source": [ "# Disable file selector" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "run_control": { "frozen": false, "read_only": false } }, "outputs": [], "source": [ "fsel= ipywe.fileselector.FileSelectorPanel(instruction='select file', start_dir='.', next=print)\n", "fsel.show()\n", "# disable the UI \n", "fsel.disable()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "run_control": { "frozen": false, "read_only": false } }, "outputs": [], "source": [ "# enable the UI\n", "fsel.enable()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "run_control": { "frozen": false, "read_only": false } }, "outputs": [], "source": [ "# disable the UI \n", "fsel.disable()" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true, "run_control": { "frozen": false, "read_only": false } }, "source": [ "# Custom layout\n", "valid widget names:\n", "* select\n", "* select_multiple\n", "* button" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "run_control": { "frozen": false, "read_only": false } }, "outputs": [], "source": [ "fsel= ipywe.fileselector.FileSelectorPanel(\n", " instruction='select file', start_dir='.',\n", " custom_layout=dict(select=ipyw.Layout(height=\"200px\", width=\"600px\"),\n", " button=ipyw.Layout(height=\"70px\"))\n", ")\n", "fsel.show()" ] }, { "cell_type": "markdown", "metadata": { "run_control": { "frozen": false, "read_only": false } }, "source": [ "# A UI with more than just the file selector\n", "In this example we show that the file selector can be used in a container with other UI elements.\n", "\n", "The initial interface has a header and footer, and the file selector is in between them.\n", "\n", "After the user choose the file, the event handler function \"next\" will be called and a new widget can be placed at the same position where the file selector was." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "run_control": { "frozen": false, "read_only": false } }, "outputs": [], "source": [ "import ipywidgets as ipyw\n", "from IPython.display import display\n", "# create UI elements\n", "# - header\n", "header = ipyw.HTML(\"

Header

\")\n", "# - body\n", "# * the file selector\n", "fsel= ipywe.fileselector.FileSelectorPanel(\n", " instruction='select file', start_dir='.', type='file', multiple=False, \n", ")\n", "# * the container of body UI element(s)\n", "body = ipyw.VBox(children=[fsel.panel])\n", "def next(selected):\n", " # the file selector UI element will be closed already at this point\n", " # create a new panel and add it to the body\n", " new_panel = ipyw.Label(\"New panel\")\n", " body.children = [new_panel]\n", " return\n", "fsel.select_layout = ipyw.Layout(height=\"300px\")\n", "fsel.next = next # assign the event handler\n", "# - footer\n", "footer = ipyw.HTML(\"

Footer

\")\n", "# put everything together\n", "container = ipyw.VBox(children=[header, body, footer])\n", "display(container)" ] }, { "cell_type": "markdown", "metadata": { "run_control": { "frozen": false, "read_only": false } }, "source": [ "# Fileselector stays alive after validation of file/directory " ] }, { "cell_type": "markdown", "metadata": { "run_control": { "frozen": false, "read_only": false } }, "source": [ "Once the selection has been done, even clicking **Select** won't close the fileselector display." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "run_control": { "frozen": false, "read_only": false }, "scrolled": false }, "outputs": [], "source": [ "fsel= ipywe.fileselector.FileSelectorPanel(instruction='select file', start_dir='.', stay_alive=True, next=print)\n", "fsel.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "run_control": { "frozen": false, "read_only": false } }, "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.4" }, "toc": { "colors": { "hover_highlight": "#DAA520", "running_highlight": "#FF0000", "selected_highlight": "#FFD700" }, "moveMenuLeft": true, "nav_menu": { "height": "210px", "width": "252px" }, "navigate_menu": true, "number_sections": true, "sideBar": true, "threshold": 4, "toc_cell": false, "toc_section_display": "block", "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 1 }