Show More
File Upload Widget.ipynb
219 lines
| 7.1 KiB
| text/plain
|
TextLexer
|
r14337 | { | |
"metadata": { | |||
"cell_tags": [ | |||
[ | |||
"<None>", | |||
null | |||
] | |||
], | |||
"name": "" | |||
}, | |||
"nbformat": 3, | |||
"nbformat_minor": 0, | |||
"worksheets": [ | |||
{ | |||
"cells": [ | |||
{ | |||
"cell_type": "code", | |||
"collapsed": false, | |||
"input": [ | |||
"from IPython.html import widgets # Widget definitions\n", | |||
|
r14342 | "from IPython.display import display # Used to display widgets in the notebook" | |
|
r14337 | ], | |
"language": "python", | |||
"metadata": {}, | |||
|
r14342 | "outputs": [], | |
|
r14338 | "prompt_number": 1 | |
|
r14337 | }, | |
{ | |||
|
r14339 | "cell_type": "heading", | |
"level": 1, | |||
"metadata": {}, | |||
"source": [ | |||
"Custom Widget" | |||
] | |||
}, | |||
{ | |||
|
r14337 | "cell_type": "code", | |
"collapsed": false, | |||
"input": [ | |||
"# Import the base Widget class and the traitlets Unicode class.\n", | |||
"from IPython.html.widgets import Widget\n", | |||
"from IPython.utils.traitlets import Unicode, Int\n", | |||
"\n", | |||
"# Define our FileWidget and its target model and default view.\n", | |||
"class FileWidget(Widget):\n", | |||
" target_name = Unicode('FileWidgetModel')\n", | |||
" default_view_name = Unicode('FilePickerView')\n", | |||
" \n", | |||
" # Define the custom state properties to sync with the front-end\n", | |||
" _keys = ['value', 'filename']\n", | |||
" value = Unicode('')\n", | |||
" filename = Unicode('')\n", | |||
" on_failed = Int(0)" | |||
], | |||
"language": "python", | |||
"metadata": {}, | |||
"outputs": [], | |||
|
r14338 | "prompt_number": 2 | |
|
r14337 | }, | |
{ | |||
"cell_type": "code", | |||
"collapsed": false, | |||
"input": [ | |||
"%%javascript\n", | |||
"\n", | |||
"require([\"notebook/js/widget\"], function(){\n", | |||
" \n", | |||
|
r14338 | " // Define the FileModel and register it with the widget manager.\n", | |
|
r14337 | " var FileModel = IPython.WidgetModel.extend({});\n", | |
|
r14342 | " IPython.widget_manager.register_widget_model('FileWidgetModel', FileModel);\n", | |
|
r14337 | " \n", | |
|
r14338 | " // Define the FilePickerView\n", | |
|
r14337 | " var FilePickerView = IPython.WidgetView.extend({\n", | |
" \n", | |||
" render: function(){\n", | |||
" var that = this;\n", | |||
" this.$el = $('<input />')\n", | |||
" .attr('type', 'file')\n", | |||
" .change(function(evt){ that.handleFileChange(evt) });\n", | |||
" },\n", | |||
" \n", | |||
" // Handles: User input\n", | |||
" handleFileChange: function(evt) { \n", | |||
" \n", | |||
" //Retrieve the first (and only!) File from the FileList object\n", | |||
" var that = this;\n", | |||
" var f = evt.target.files[0];\n", | |||
" if (f) {\n", | |||
" var r = new FileReader();\n", | |||
" r.onload = function(e) {\n", | |||
" that.model.set('value', e.target.result);\n", | |||
" that.model.update_other_views(that);\n", | |||
" }\n", | |||
" r.readAsText(f);\n", | |||
|
r14338 | " } else {\n", | |
|
r14337 | " this.model.set('on_failed', this.model.get('on_failed') + 1);\n", | |
" this.model.update_other_views(this);\n", | |||
" }\n", | |||
" this.model.set('filename', f.name);\n", | |||
" this.model.update_other_views(this);\n", | |||
" },\n", | |||
" });\n", | |||
" \n", | |||
" // Register the DatePickerView with the widget manager.\n", | |||
|
r14342 | " IPython.widget_manager.register_widget_view('FilePickerView', FilePickerView);\n", | |
|
r14337 | "});" | |
], | |||
"language": "python", | |||
"metadata": {}, | |||
"outputs": [ | |||
{ | |||
"javascript": [ | |||
"\n", | |||
"require([\"notebook/js/widget\"], function(){\n", | |||
" \n", | |||
|
r14338 | " // Define the FileModel and register it with the widget manager.\n", | |
|
r14337 | " var FileModel = IPython.WidgetModel.extend({});\n", | |
|
r14342 | " IPython.widget_manager.register_widget_model('FileWidgetModel', FileModel);\n", | |
|
r14337 | " \n", | |
|
r14338 | " // Define the FilePickerView\n", | |
|
r14337 | " var FilePickerView = IPython.WidgetView.extend({\n", | |
" \n", | |||
" render: function(){\n", | |||
" var that = this;\n", | |||
" this.$el = $('<input />')\n", | |||
" .attr('type', 'file')\n", | |||
" .change(function(evt){ that.handleFileChange(evt) });\n", | |||
" },\n", | |||
" \n", | |||
" // Handles: User input\n", | |||
" handleFileChange: function(evt) { \n", | |||
" \n", | |||
" //Retrieve the first (and only!) File from the FileList object\n", | |||
" var that = this;\n", | |||
" var f = evt.target.files[0];\n", | |||
" if (f) {\n", | |||
" var r = new FileReader();\n", | |||
" r.onload = function(e) {\n", | |||
" that.model.set('value', e.target.result);\n", | |||
" that.model.update_other_views(that);\n", | |||
" }\n", | |||
" r.readAsText(f);\n", | |||
|
r14338 | " } else {\n", | |
|
r14337 | " this.model.set('on_failed', this.model.get('on_failed') + 1);\n", | |
" this.model.update_other_views(this);\n", | |||
" }\n", | |||
" this.model.set('filename', f.name);\n", | |||
" this.model.update_other_views(this);\n", | |||
" },\n", | |||
" });\n", | |||
" \n", | |||
" // Register the DatePickerView with the widget manager.\n", | |||
|
r14342 | " IPython.widget_manager.register_widget_view('FilePickerView', FilePickerView);\n", | |
|
r14337 | "});" | |
], | |||
"metadata": {}, | |||
"output_type": "display_data", | |||
"text": [ | |||
|
r14342 | "<IPython.core.display.Javascript at 0x319fe90>" | |
|
r14337 | ] | |
} | |||
], | |||
|
r14338 | "prompt_number": 3 | |
|
r14337 | }, | |
{ | |||
|
r14339 | "cell_type": "heading", | |
"level": 1, | |||
"metadata": {}, | |||
"source": [ | |||
"Usage" | |||
] | |||
}, | |||
{ | |||
|
r14337 | "cell_type": "code", | |
"collapsed": false, | |||
"input": [ | |||
"file_widget = FileWidget()\n", | |||
"display(file_widget)\n", | |||
"\n", | |||
"def file_loading():\n", | |||
" print \"Loading %s\" % file_widget.filename\n", | |||
"\n", | |||
"def file_loaded():\n", | |||
" print \"Loaded, file contents: %s\" % file_widget.value\n", | |||
"\n", | |||
"def file_failed(name, old_value, new_value):\n", | |||
" if new_value > old_value:\n", | |||
" print \"Could not load file contents of %s\" % file_widget.filename\n", | |||
"\n", | |||
"\n", | |||
"file_widget.on_trait_change(file_loading, 'filename')\n", | |||
"file_widget.on_trait_change(file_loaded, 'value')\n", | |||
"file_widget.on_trait_change(file_failed, 'on_failed')" | |||
], | |||
"language": "python", | |||
"metadata": {}, | |||
"outputs": [ | |||
{ | |||
"output_type": "stream", | |||
"stream": "stdout", | |||
"text": [ | |||
"Loading test.txt\n" | |||
] | |||
}, | |||
{ | |||
"output_type": "stream", | |||
"stream": "stdout", | |||
"text": [ | |||
"Loaded, file contents: \n", | |||
"hello world!\n" | |||
] | |||
} | |||
], | |||
|
r14338 | "prompt_number": 4 | |
|
r14337 | } | |
], | |||
"metadata": {} | |||
} | |||
] | |||
} |