##// END OF EJS Templates
Updated comments in File Upload Widget example.
Jonathan Frederic -
Show More
@@ -1,292 +1,288 b''
1 {
1 {
2 "metadata": {
2 "metadata": {
3 "cell_tags": [
3 "cell_tags": [
4 [
4 [
5 "<None>",
5 "<None>",
6 null
6 null
7 ]
7 ]
8 ],
8 ],
9 "name": ""
9 "name": ""
10 },
10 },
11 "nbformat": 3,
11 "nbformat": 3,
12 "nbformat_minor": 0,
12 "nbformat_minor": 0,
13 "worksheets": [
13 "worksheets": [
14 {
14 {
15 "cells": [
15 "cells": [
16 {
16 {
17 "cell_type": "code",
17 "cell_type": "code",
18 "collapsed": false,
18 "collapsed": false,
19 "input": [
19 "input": [
20 "from IPython.html import widgets # Widget definitions\n",
20 "from IPython.html import widgets # Widget definitions\n",
21 "from IPython.display import display # Used to display widgets in the notebook\n",
21 "from IPython.display import display # Used to display widgets in the notebook\n",
22 "\n",
22 "\n",
23 "# Enable widgets in this notebook\n",
23 "# Enable widgets in this notebook\n",
24 "widgets.init_widget_js()"
24 "widgets.init_widget_js()"
25 ],
25 ],
26 "language": "python",
26 "language": "python",
27 "metadata": {},
27 "metadata": {},
28 "outputs": [
28 "outputs": [
29 {
29 {
30 "javascript": [
30 "javascript": [
31 "$.getScript($(\"body\").data(\"baseProjectUrl\") + \"static/notebook/js/widgets/button.js\");"
31 "$.getScript($(\"body\").data(\"baseProjectUrl\") + \"static/notebook/js/widgets/button.js\");"
32 ],
32 ],
33 "metadata": {},
33 "metadata": {},
34 "output_type": "display_data"
34 "output_type": "display_data"
35 },
35 },
36 {
36 {
37 "javascript": [
37 "javascript": [
38 "$.getScript($(\"body\").data(\"baseProjectUrl\") + \"static/notebook/js/widgets/int_range.js\");"
38 "$.getScript($(\"body\").data(\"baseProjectUrl\") + \"static/notebook/js/widgets/int_range.js\");"
39 ],
39 ],
40 "metadata": {},
40 "metadata": {},
41 "output_type": "display_data"
41 "output_type": "display_data"
42 },
42 },
43 {
43 {
44 "javascript": [
44 "javascript": [
45 "$.getScript($(\"body\").data(\"baseProjectUrl\") + \"static/notebook/js/widgets/string.js\");"
45 "$.getScript($(\"body\").data(\"baseProjectUrl\") + \"static/notebook/js/widgets/string.js\");"
46 ],
46 ],
47 "metadata": {},
47 "metadata": {},
48 "output_type": "display_data"
48 "output_type": "display_data"
49 },
49 },
50 {
50 {
51 "javascript": [
51 "javascript": [
52 "$.getScript($(\"body\").data(\"baseProjectUrl\") + \"static/notebook/js/widgets/multicontainer.js\");"
52 "$.getScript($(\"body\").data(\"baseProjectUrl\") + \"static/notebook/js/widgets/multicontainer.js\");"
53 ],
53 ],
54 "metadata": {},
54 "metadata": {},
55 "output_type": "display_data"
55 "output_type": "display_data"
56 },
56 },
57 {
57 {
58 "javascript": [
58 "javascript": [
59 "$.getScript($(\"body\").data(\"baseProjectUrl\") + \"static/notebook/js/widgets/bool.js\");"
59 "$.getScript($(\"body\").data(\"baseProjectUrl\") + \"static/notebook/js/widgets/bool.js\");"
60 ],
60 ],
61 "metadata": {},
61 "metadata": {},
62 "output_type": "display_data"
62 "output_type": "display_data"
63 },
63 },
64 {
64 {
65 "javascript": [
65 "javascript": [
66 "$.getScript($(\"body\").data(\"baseProjectUrl\") + \"static/notebook/js/widgets/int.js\");"
66 "$.getScript($(\"body\").data(\"baseProjectUrl\") + \"static/notebook/js/widgets/int.js\");"
67 ],
67 ],
68 "metadata": {},
68 "metadata": {},
69 "output_type": "display_data"
69 "output_type": "display_data"
70 },
70 },
71 {
71 {
72 "javascript": [
72 "javascript": [
73 "$.getScript($(\"body\").data(\"baseProjectUrl\") + \"static/notebook/js/widgets/selection.js\");"
73 "$.getScript($(\"body\").data(\"baseProjectUrl\") + \"static/notebook/js/widgets/selection.js\");"
74 ],
74 ],
75 "metadata": {},
75 "metadata": {},
76 "output_type": "display_data"
76 "output_type": "display_data"
77 },
77 },
78 {
78 {
79 "javascript": [
79 "javascript": [
80 "$.getScript($(\"body\").data(\"baseProjectUrl\") + \"static/notebook/js/widgets/float.js\");"
80 "$.getScript($(\"body\").data(\"baseProjectUrl\") + \"static/notebook/js/widgets/float.js\");"
81 ],
81 ],
82 "metadata": {},
82 "metadata": {},
83 "output_type": "display_data"
83 "output_type": "display_data"
84 },
84 },
85 {
85 {
86 "javascript": [
86 "javascript": [
87 "$.getScript($(\"body\").data(\"baseProjectUrl\") + \"static/notebook/js/widgets/float_range.js\");"
87 "$.getScript($(\"body\").data(\"baseProjectUrl\") + \"static/notebook/js/widgets/float_range.js\");"
88 ],
88 ],
89 "metadata": {},
89 "metadata": {},
90 "output_type": "display_data"
90 "output_type": "display_data"
91 },
91 },
92 {
92 {
93 "javascript": [
93 "javascript": [
94 "$.getScript($(\"body\").data(\"baseProjectUrl\") + \"static/notebook/js/widgets/container.js\");"
94 "$.getScript($(\"body\").data(\"baseProjectUrl\") + \"static/notebook/js/widgets/container.js\");"
95 ],
95 ],
96 "metadata": {},
96 "metadata": {},
97 "output_type": "display_data"
97 "output_type": "display_data"
98 }
98 }
99 ],
99 ],
100 "prompt_number": 22
100 "prompt_number": 1
101 },
101 },
102 {
102 {
103 "cell_type": "code",
103 "cell_type": "code",
104 "collapsed": false,
104 "collapsed": false,
105 "input": [
105 "input": [
106 "# Import the base Widget class and the traitlets Unicode class.\n",
106 "# Import the base Widget class and the traitlets Unicode class.\n",
107 "from IPython.html.widgets import Widget\n",
107 "from IPython.html.widgets import Widget\n",
108 "from IPython.utils.traitlets import Unicode, Int\n",
108 "from IPython.utils.traitlets import Unicode, Int\n",
109 "\n",
109 "\n",
110 "# Define our FileWidget and its target model and default view.\n",
110 "# Define our FileWidget and its target model and default view.\n",
111 "class FileWidget(Widget):\n",
111 "class FileWidget(Widget):\n",
112 " target_name = Unicode('FileWidgetModel')\n",
112 " target_name = Unicode('FileWidgetModel')\n",
113 " default_view_name = Unicode('FilePickerView')\n",
113 " default_view_name = Unicode('FilePickerView')\n",
114 " \n",
114 " \n",
115 " # Define the custom state properties to sync with the front-end\n",
115 " # Define the custom state properties to sync with the front-end\n",
116 " _keys = ['value', 'filename']\n",
116 " _keys = ['value', 'filename']\n",
117 " value = Unicode('')\n",
117 " value = Unicode('')\n",
118 " filename = Unicode('')\n",
118 " filename = Unicode('')\n",
119 " on_failed = Int(0)"
119 " on_failed = Int(0)"
120 ],
120 ],
121 "language": "python",
121 "language": "python",
122 "metadata": {},
122 "metadata": {},
123 "outputs": [],
123 "outputs": [],
124 "prompt_number": 23
124 "prompt_number": 2
125 },
125 },
126 {
126 {
127 "cell_type": "code",
127 "cell_type": "code",
128 "collapsed": false,
128 "collapsed": false,
129 "input": [
129 "input": [
130 "%%javascript\n",
130 "%%javascript\n",
131 "\n",
131 "\n",
132 "require([\"notebook/js/widget\"], function(){\n",
132 "require([\"notebook/js/widget\"], function(){\n",
133 " \n",
133 " \n",
134 " // Define the DateModel and register it with the widget manager.\n",
134 " // Define the FileModel and register it with the widget manager.\n",
135 " var FileModel = IPython.WidgetModel.extend({});\n",
135 " var FileModel = IPython.WidgetModel.extend({});\n",
136 " IPython.notebook.widget_manager.register_widget_model('FileWidgetModel', FileModel);\n",
136 " IPython.notebook.widget_manager.register_widget_model('FileWidgetModel', FileModel);\n",
137 " \n",
137 " \n",
138 " // Define the DatePickerView\n",
138 " // Define the FilePickerView\n",
139 " var FilePickerView = IPython.WidgetView.extend({\n",
139 " var FilePickerView = IPython.WidgetView.extend({\n",
140 " \n",
140 " \n",
141 " render: function(){\n",
141 " render: function(){\n",
142 " var that = this;\n",
142 " var that = this;\n",
143 " this.$el = $('<input />')\n",
143 " this.$el = $('<input />')\n",
144 " .attr('type', 'file')\n",
144 " .attr('type', 'file')\n",
145 " .change(function(evt){ that.handleFileChange(evt) });\n",
145 " .change(function(evt){ that.handleFileChange(evt) });\n",
146 " },\n",
146 " },\n",
147 " \n",
147 " \n",
148 " // Handles: User input\n",
148 " // Handles: User input\n",
149 " events: { \"change\" : \"handleFileChange\" }, \n",
149 " events: { \"change\" : \"handleFileChange\" }, \n",
150 " handleFileChange: function(evt) { \n",
150 " handleFileChange: function(evt) { \n",
151 " \n",
151 " \n",
152 " //Retrieve the first (and only!) File from the FileList object\n",
152 " //Retrieve the first (and only!) File from the FileList object\n",
153 " var that = this;\n",
153 " var that = this;\n",
154 " var f = evt.target.files[0];\n",
154 " var f = evt.target.files[0];\n",
155 " if (f) {\n",
155 " if (f) {\n",
156 " var r = new FileReader();\n",
156 " var r = new FileReader();\n",
157 " r.onload = function(e) {\n",
157 " r.onload = function(e) {\n",
158 " that.model.set('value', e.target.result);\n",
158 " that.model.set('value', e.target.result);\n",
159 " that.model.update_other_views(that);\n",
159 " that.model.update_other_views(that);\n",
160 " }\n",
160 " }\n",
161 " r.readAsText(f);\n",
161 " r.readAsText(f);\n",
162 " }\n",
162 " } else {\n",
163 " else\n",
164 " {\n",
165 " this.model.set('on_failed', this.model.get('on_failed') + 1);\n",
163 " this.model.set('on_failed', this.model.get('on_failed') + 1);\n",
166 " this.model.update_other_views(this);\n",
164 " this.model.update_other_views(this);\n",
167 " }\n",
165 " }\n",
168 " this.model.set('filename', f.name);\n",
166 " this.model.set('filename', f.name);\n",
169 " this.model.update_other_views(this);\n",
167 " this.model.update_other_views(this);\n",
170 " },\n",
168 " },\n",
171 " });\n",
169 " });\n",
172 " \n",
170 " \n",
173 " // Register the DatePickerView with the widget manager.\n",
171 " // Register the DatePickerView with the widget manager.\n",
174 " IPython.notebook.widget_manager.register_widget_view('FilePickerView', FilePickerView);\n",
172 " IPython.notebook.widget_manager.register_widget_view('FilePickerView', FilePickerView);\n",
175 "});"
173 "});"
176 ],
174 ],
177 "language": "python",
175 "language": "python",
178 "metadata": {},
176 "metadata": {},
179 "outputs": [
177 "outputs": [
180 {
178 {
181 "javascript": [
179 "javascript": [
182 "\n",
180 "\n",
183 "require([\"notebook/js/widget\"], function(){\n",
181 "require([\"notebook/js/widget\"], function(){\n",
184 " \n",
182 " \n",
185 " // Define the DateModel and register it with the widget manager.\n",
183 " // Define the FileModel and register it with the widget manager.\n",
186 " var FileModel = IPython.WidgetModel.extend({});\n",
184 " var FileModel = IPython.WidgetModel.extend({});\n",
187 " IPython.notebook.widget_manager.register_widget_model('FileWidgetModel', FileModel);\n",
185 " IPython.notebook.widget_manager.register_widget_model('FileWidgetModel', FileModel);\n",
188 " \n",
186 " \n",
189 " // Define the DatePickerView\n",
187 " // Define the FilePickerView\n",
190 " var FilePickerView = IPython.WidgetView.extend({\n",
188 " var FilePickerView = IPython.WidgetView.extend({\n",
191 " \n",
189 " \n",
192 " render: function(){\n",
190 " render: function(){\n",
193 " var that = this;\n",
191 " var that = this;\n",
194 " this.$el = $('<input />')\n",
192 " this.$el = $('<input />')\n",
195 " .attr('type', 'file')\n",
193 " .attr('type', 'file')\n",
196 " .change(function(evt){ that.handleFileChange(evt) });\n",
194 " .change(function(evt){ that.handleFileChange(evt) });\n",
197 " },\n",
195 " },\n",
198 " \n",
196 " \n",
199 " // Handles: User input\n",
197 " // Handles: User input\n",
200 " events: { \"change\" : \"handleFileChange\" }, \n",
198 " events: { \"change\" : \"handleFileChange\" }, \n",
201 " handleFileChange: function(evt) { \n",
199 " handleFileChange: function(evt) { \n",
202 " \n",
200 " \n",
203 " //Retrieve the first (and only!) File from the FileList object\n",
201 " //Retrieve the first (and only!) File from the FileList object\n",
204 " var that = this;\n",
202 " var that = this;\n",
205 " var f = evt.target.files[0];\n",
203 " var f = evt.target.files[0];\n",
206 " if (f) {\n",
204 " if (f) {\n",
207 " var r = new FileReader();\n",
205 " var r = new FileReader();\n",
208 " r.onload = function(e) {\n",
206 " r.onload = function(e) {\n",
209 " that.model.set('value', e.target.result);\n",
207 " that.model.set('value', e.target.result);\n",
210 " that.model.update_other_views(that);\n",
208 " that.model.update_other_views(that);\n",
211 " }\n",
209 " }\n",
212 " r.readAsText(f);\n",
210 " r.readAsText(f);\n",
213 " }\n",
211 " } else {\n",
214 " else\n",
215 " {\n",
216 " this.model.set('on_failed', this.model.get('on_failed') + 1);\n",
212 " this.model.set('on_failed', this.model.get('on_failed') + 1);\n",
217 " this.model.update_other_views(this);\n",
213 " this.model.update_other_views(this);\n",
218 " }\n",
214 " }\n",
219 " this.model.set('filename', f.name);\n",
215 " this.model.set('filename', f.name);\n",
220 " this.model.update_other_views(this);\n",
216 " this.model.update_other_views(this);\n",
221 " },\n",
217 " },\n",
222 " });\n",
218 " });\n",
223 " \n",
219 " \n",
224 " // Register the DatePickerView with the widget manager.\n",
220 " // Register the DatePickerView with the widget manager.\n",
225 " IPython.notebook.widget_manager.register_widget_view('FilePickerView', FilePickerView);\n",
221 " IPython.notebook.widget_manager.register_widget_view('FilePickerView', FilePickerView);\n",
226 "});"
222 "});"
227 ],
223 ],
228 "metadata": {},
224 "metadata": {},
229 "output_type": "display_data",
225 "output_type": "display_data",
230 "text": [
226 "text": [
231 "<IPython.core.display.Javascript at 0x21d8cd0>"
227 "<IPython.core.display.Javascript at 0x2fa80d0>"
232 ]
228 ]
233 }
229 }
234 ],
230 ],
235 "prompt_number": 24
231 "prompt_number": 3
236 },
232 },
237 {
233 {
238 "cell_type": "code",
234 "cell_type": "code",
239 "collapsed": false,
235 "collapsed": false,
240 "input": [
236 "input": [
241 "file_widget = FileWidget()\n",
237 "file_widget = FileWidget()\n",
242 "display(file_widget)\n",
238 "display(file_widget)\n",
243 "\n",
239 "\n",
244 "def file_loading():\n",
240 "def file_loading():\n",
245 " print \"Loading %s\" % file_widget.filename\n",
241 " print \"Loading %s\" % file_widget.filename\n",
246 "\n",
242 "\n",
247 "def file_loaded():\n",
243 "def file_loaded():\n",
248 " print \"Loaded, file contents: %s\" % file_widget.value\n",
244 " print \"Loaded, file contents: %s\" % file_widget.value\n",
249 "\n",
245 "\n",
250 "def file_failed(name, old_value, new_value):\n",
246 "def file_failed(name, old_value, new_value):\n",
251 " if new_value > old_value:\n",
247 " if new_value > old_value:\n",
252 " print \"Could not load file contents of %s\" % file_widget.filename\n",
248 " print \"Could not load file contents of %s\" % file_widget.filename\n",
253 "\n",
249 "\n",
254 "\n",
250 "\n",
255 "file_widget.on_trait_change(file_loading, 'filename')\n",
251 "file_widget.on_trait_change(file_loading, 'filename')\n",
256 "file_widget.on_trait_change(file_loaded, 'value')\n",
252 "file_widget.on_trait_change(file_loaded, 'value')\n",
257 "file_widget.on_trait_change(file_failed, 'on_failed')"
253 "file_widget.on_trait_change(file_failed, 'on_failed')"
258 ],
254 ],
259 "language": "python",
255 "language": "python",
260 "metadata": {},
256 "metadata": {},
261 "outputs": [
257 "outputs": [
262 {
258 {
263 "output_type": "stream",
259 "output_type": "stream",
264 "stream": "stdout",
260 "stream": "stdout",
265 "text": [
261 "text": [
266 "Loading test.txt\n"
262 "Loading test.txt\n"
267 ]
263 ]
268 },
264 },
269 {
265 {
270 "output_type": "stream",
266 "output_type": "stream",
271 "stream": "stdout",
267 "stream": "stdout",
272 "text": [
268 "text": [
273 "Loaded, file contents: \n",
269 "Loaded, file contents: \n",
274 "hello world!\n"
270 "hello world!\n"
275 ]
271 ]
276 }
272 }
277 ],
273 ],
278 "prompt_number": 25
274 "prompt_number": 4
279 },
275 },
280 {
276 {
281 "cell_type": "code",
277 "cell_type": "code",
282 "collapsed": false,
278 "collapsed": false,
283 "input": [],
279 "input": [],
284 "language": "python",
280 "language": "python",
285 "metadata": {},
281 "metadata": {},
286 "outputs": []
282 "outputs": []
287 }
283 }
288 ],
284 ],
289 "metadata": {}
285 "metadata": {}
290 }
286 }
291 ]
287 ]
292 } No newline at end of file
288 }
General Comments 0
You need to be logged in to leave comments. Login now