Skip to content

Instantly share code, notes, and snippets.

@draperjames
Created August 3, 2017 21:59
Show Gist options
  • Select an option

  • Save draperjames/aa5ff93c24373434421f00693724a521 to your computer and use it in GitHub Desktop.

Select an option

Save draperjames/aa5ff93c24373434421f00693724a521 to your computer and use it in GitHub Desktop.
ipywidgets upload files and run somekind of process on them
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "b6b7f87196054d58b9108e66c72a2e9b",
"version_major": "2",
"version_minor": "0"
},
"text/plain": [
"A Jupyter Widget"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "bf1f3794818d480f8cff99cd6c53c157",
"version_major": "2",
"version_minor": "0"
},
"text/plain": [
"A Jupyter Widget"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import traitlets\n",
"from ipywidgets import widgets\n",
"from IPython.display import display\n",
"from tkinter import Tk, filedialog\n",
"\n",
"def process(files):\n",
" # Copy and paste top secret algorithm here.\n",
" \n",
" print(\"Here are your preocessed files:\")\n",
" [print(i) for i in files]\n",
"\n",
"class SelectFilesButton(widgets.Button):\n",
" \"\"\"A file widget that leverages tkinter.filedialog.\"\"\"\n",
" files = traitlets.List([], help=\"List of file paths\").tag(sync=True)\n",
"\n",
" def __init__(self, *args, **kwargs):\n",
" \"\"\"Initialize the SelectFilesButton class.\"\"\"\n",
" super(SelectFilesButton, self).__init__(*args, **kwargs)\n",
" # Create the button.\n",
" self.description = \"Select Files\"\n",
" self.icon = \"square-o\"\n",
" self.style.button_color = \"orange\"\n",
" # Set on click behavior.\n",
" self.on_click(self.select_files)\n",
"\n",
" @staticmethod\n",
" def select_files(b):\n",
" \"\"\"Generate instance of tkinter.filedialog.\n",
"\n",
" Parameters\n",
" ----------\n",
" b : obj:\n",
" An instance of ipywidgets.widgets.Button\n",
" \"\"\"\n",
" # Create Tk root\n",
" root = Tk()\n",
" # Hide the main window\n",
" root.withdraw()\n",
" # Raise the root to the top of all windows.\n",
" root.call('wm', 'attributes', '.', '-topmost', True)\n",
" # List of selected fileswill be set to b.value\n",
" b.files = filedialog.askopenfilename(multiple=True)\n",
"\n",
" b.description = \"Files Selected\"\n",
" b.icon = \"check-square-o\"\n",
" b.style.button_color = \"lightgreen\"\n",
"\n",
"\n",
"class RunButton(widgets.Button):\n",
" \"\"\"Button that begins processing the selected files.\"\"\"\n",
" files = traitlets.List([], help=\"List of file paths\").tag(sync=True)\n",
" \n",
" def __init__(self, *args, **kwargs):\n",
" \"\"\"Initialize the SelectFilesButton class.\"\"\"\n",
" super(RunButton, self).__init__(*args, **kwargs)\n",
" self.add_traits(data=traitlets.Instance(object))\n",
" self.description = \"Run\"\n",
" self.button_style = \"info\"\n",
" self.icon = \"play\"\n",
" self.process = \"process\"\n",
" self.on_click(self.run_process)\n",
"\n",
" @staticmethod\n",
" def run_process(b):\n",
" \"\"\"Run the process defined in the __init__ def.\"\"\"\n",
" process = eval(b.process)\n",
" b.data = process(b.files)\n",
"\n",
"rb = RunButton()\n",
"sb = SelectFilesButton()\n",
"widgets.jslink((sb,'files'), (rb,'files'))\n",
"display(sb,rb)\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"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.1"
},
"varInspector": {
"cols": {
"lenName": 16,
"lenType": 16,
"lenVar": 40
},
"kernels_config": {
"python": {
"delete_cmd_postfix": "",
"delete_cmd_prefix": "del ",
"library": "var_list.py",
"varRefreshCmd": "print(var_dic_list())"
},
"r": {
"delete_cmd_postfix": ") ",
"delete_cmd_prefix": "rm(",
"library": "var_list.r",
"varRefreshCmd": "cat(var_dic_list()) "
}
},
"types_to_exclude": [
"module",
"function",
"builtin_function_or_method",
"instance",
"_Feature"
],
"window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment