Show More
@@ -1,248 +1,248 b'' | |||
|
1 | 1 | { |
|
2 | 2 | "metadata": { |
|
3 | 3 | "cell_tags": [ |
|
4 | 4 | [ |
|
5 | 5 | "<None>", |
|
6 | 6 | null |
|
7 | 7 | ] |
|
8 | 8 | ], |
|
9 | 9 | "name": "" |
|
10 | 10 | }, |
|
11 | 11 | "nbformat": 3, |
|
12 | 12 | "nbformat_minor": 0, |
|
13 | 13 | "worksheets": [ |
|
14 | 14 | { |
|
15 | 15 | "cells": [ |
|
16 | 16 | { |
|
17 | 17 | "cell_type": "code", |
|
18 | 18 | "collapsed": false, |
|
19 | 19 | "input": [ |
|
20 | 20 | "import base64\n", |
|
21 | 21 | "from __future__ import print_function # py 2.7 compat.\n", |
|
22 | 22 | "from IPython.html import widgets # Widget definitions.\n", |
|
23 | 23 | "from IPython.utils.traitlets import Unicode # Traitlet needed to add synced attributes to the widget." |
|
24 | 24 | ], |
|
25 | 25 | "language": "python", |
|
26 | 26 | "metadata": {}, |
|
27 | 27 | "outputs": [], |
|
28 | 28 | "prompt_number": 1 |
|
29 | 29 | }, |
|
30 | 30 | { |
|
31 | 31 | "cell_type": "markdown", |
|
32 | 32 | "metadata": {}, |
|
33 | 33 | "source": [ |
|
34 | 34 | "This is a custom widget that allows the user to upload file data to the notebook server. The file data is sent via a statefull `value` attribute of the widget. The widget has an upload failed event that fires in the front-end and is echoed to the back-end using a custom msg." |
|
35 | 35 | ] |
|
36 | 36 | }, |
|
37 | 37 | { |
|
38 | 38 | "cell_type": "code", |
|
39 | 39 | "collapsed": false, |
|
40 | 40 | "input": [ |
|
41 | 41 | "class FileWidget(widgets.DOMWidget):\n", |
|
42 | 42 | " _view_name = Unicode('FilePickerView', sync=True)\n", |
|
43 | 43 | " value = Unicode(sync=True)\n", |
|
44 | 44 | " filename = Unicode(sync=True)\n", |
|
45 | 45 | " \n", |
|
46 | 46 | " def __init__(self, **kwargs):\n", |
|
47 | 47 | " \"\"\"Constructor\"\"\"\n", |
|
48 | 48 | " widgets.DOMWidget.__init__(self, **kwargs) # Call the base.\n", |
|
49 | 49 | " \n", |
|
50 | 50 | " # Allow the user to register error callbacks with the following signatures:\n", |
|
51 | 51 | " # callback()\n", |
|
52 | 52 | " # callback(sender)\n", |
|
53 | 53 | " self.errors = widgets.CallbackDispatcher(accepted_nargs=[0, 1])\n", |
|
54 | 54 | " \n", |
|
55 | 55 | " # Listen for custom msgs\n", |
|
56 | 56 | " self.on_msg(self._handle_custom_msg)\n", |
|
57 | 57 | "\n", |
|
58 | 58 | " def _handle_custom_msg(self, content):\n", |
|
59 | 59 | " \"\"\"Handle a msg from the front-end.\n", |
|
60 | 60 | "\n", |
|
61 | 61 | " Parameters\n", |
|
62 | 62 | " ----------\n", |
|
63 | 63 | " content: dict\n", |
|
64 | 64 | " Content of the msg.\"\"\"\n", |
|
65 | 65 | " if 'event' in content and content['event'] == 'error':\n", |
|
66 | 66 | " self.errors()\n", |
|
67 | 67 | " self.errors(self)\n", |
|
68 | 68 | " " |
|
69 | 69 | ], |
|
70 | 70 | "language": "python", |
|
71 | 71 | "metadata": {}, |
|
72 | 72 | "outputs": [], |
|
73 | 73 | "prompt_number": 2 |
|
74 | 74 | }, |
|
75 | 75 | { |
|
76 | 76 | "cell_type": "code", |
|
77 | 77 | "collapsed": false, |
|
78 | 78 | "input": [ |
|
79 | 79 | "%%javascript\n", |
|
80 | 80 | "\n", |
|
81 | 81 | "require([\"widgets/js/widget\"], function(WidgetManager){\n", |
|
82 | 82 | "\n", |
|
83 | 83 | " var FilePickerView = IPython.WidgetView.extend({\n", |
|
84 | 84 | " render: function(){\n", |
|
85 | 85 | " // Render the view.\n", |
|
86 | 86 | " this.setElement($('<input />')\n", |
|
87 | 87 | " .attr('type', 'file'));\n", |
|
88 | 88 | " },\n", |
|
89 | 89 | " \n", |
|
90 | 90 | " events: {\n", |
|
91 | 91 | " // List of events and their handlers.\n", |
|
92 | 92 | " 'change': 'handle_file_change',\n", |
|
93 | 93 | " },\n", |
|
94 | 94 | " \n", |
|
95 | 95 | " handle_file_change: function(evt) { \n", |
|
96 | 96 | " // Handle when the user has changed the file.\n", |
|
97 | 97 | " \n", |
|
98 | 98 | " // Retrieve the first (and only!) File from the FileList object\n", |
|
99 | 99 | " var file = evt.target.files[0];\n", |
|
100 | 100 | " if (file) {\n", |
|
101 | 101 | "\n", |
|
102 | 102 | " // Read the file's textual content and set value to those contents.\n", |
|
103 | 103 | " var that = this;\n", |
|
104 | 104 | " var file_reader = new FileReader();\n", |
|
105 | 105 | " file_reader.onload = function(e) {\n", |
|
106 | 106 | " that.model.set('value', e.target.result);\n", |
|
107 | 107 | " that.touch();\n", |
|
108 | 108 | " }\n", |
|
109 | 109 | " file_reader.readAsText(file);\n", |
|
110 | 110 | " } else {\n", |
|
111 | 111 | "\n", |
|
112 | 112 | " // The file couldn't be opened. Send an error msg to the\n", |
|
113 | 113 | " // back-end.\n", |
|
114 | 114 | " this.send({ 'event': 'error' });\n", |
|
115 | 115 | " }\n", |
|
116 | 116 | "\n", |
|
117 | 117 | " // Set the filename of the file.\n", |
|
118 | 118 | " this.model.set('filename', file.name);\n", |
|
119 | 119 | " this.touch();\n", |
|
120 | 120 | " },\n", |
|
121 | 121 | " });\n", |
|
122 | 122 | " \n", |
|
123 | 123 | " // Register the DatePickerView with the widget manager.\n", |
|
124 | 124 | " WidgetManager.register_widget_view('FilePickerView', FilePickerView);\n", |
|
125 | 125 | "});" |
|
126 | 126 | ], |
|
127 | 127 | "language": "python", |
|
128 | 128 | "metadata": {}, |
|
129 | 129 | "outputs": [ |
|
130 | 130 | { |
|
131 | 131 | "javascript": [ |
|
132 | 132 | "\n", |
|
133 |
"require([\" |
|
|
133 | "require([\"widgets/js/widget\"], function(WidgetManager){\n", | |
|
134 | 134 | "\n", |
|
135 | 135 | " var FilePickerView = IPython.WidgetView.extend({\n", |
|
136 | 136 | " render: function(){\n", |
|
137 | 137 | " // Render the view.\n", |
|
138 | 138 | " this.setElement($('<input />')\n", |
|
139 | 139 | " .attr('type', 'file'));\n", |
|
140 | 140 | " },\n", |
|
141 | 141 | " \n", |
|
142 | 142 | " events: {\n", |
|
143 | 143 | " // List of events and their handlers.\n", |
|
144 | 144 | " 'change': 'handle_file_change',\n", |
|
145 | 145 | " },\n", |
|
146 | 146 | " \n", |
|
147 | 147 | " handle_file_change: function(evt) { \n", |
|
148 | 148 | " // Handle when the user has changed the file.\n", |
|
149 | 149 | " \n", |
|
150 | 150 | " // Retrieve the first (and only!) File from the FileList object\n", |
|
151 | 151 | " var file = evt.target.files[0];\n", |
|
152 | 152 | " if (file) {\n", |
|
153 | 153 | "\n", |
|
154 | 154 | " // Read the file's textual content and set value to those contents.\n", |
|
155 | 155 | " var that = this;\n", |
|
156 | 156 | " var file_reader = new FileReader();\n", |
|
157 | 157 | " file_reader.onload = function(e) {\n", |
|
158 | 158 | " that.model.set('value', e.target.result);\n", |
|
159 | 159 | " that.touch();\n", |
|
160 | 160 | " }\n", |
|
161 | 161 | " file_reader.readAsText(file);\n", |
|
162 | 162 | " } else {\n", |
|
163 | 163 | "\n", |
|
164 | 164 | " // The file couldn't be opened. Send an error msg to the\n", |
|
165 | 165 | " // back-end.\n", |
|
166 | 166 | " this.send({ 'event': 'error' });\n", |
|
167 | 167 | " }\n", |
|
168 | 168 | "\n", |
|
169 | 169 | " // Set the filename of the file.\n", |
|
170 | 170 | " this.model.set('filename', file.name);\n", |
|
171 | 171 | " this.touch();\n", |
|
172 | 172 | " },\n", |
|
173 | 173 | " });\n", |
|
174 | 174 | " \n", |
|
175 | 175 | " // Register the DatePickerView with the widget manager.\n", |
|
176 | 176 | " WidgetManager.register_widget_view('FilePickerView', FilePickerView);\n", |
|
177 | 177 | "});" |
|
178 | 178 | ], |
|
179 | 179 | "metadata": {}, |
|
180 | 180 | "output_type": "display_data", |
|
181 | 181 | "text": [ |
|
182 | 182 | "<IPython.core.display.Javascript at 0x36df2d0>" |
|
183 | 183 | ] |
|
184 | 184 | } |
|
185 | 185 | ], |
|
186 | 186 | "prompt_number": 3 |
|
187 | 187 | }, |
|
188 | 188 | { |
|
189 | 189 | "cell_type": "markdown", |
|
190 | 190 | "metadata": {}, |
|
191 | 191 | "source": [ |
|
192 | 192 | "The following shows how the file widget can be used." |
|
193 | 193 | ] |
|
194 | 194 | }, |
|
195 | 195 | { |
|
196 | 196 | "cell_type": "code", |
|
197 | 197 | "collapsed": false, |
|
198 | 198 | "input": [ |
|
199 | 199 | "file_widget = FileWidget()\n", |
|
200 | 200 | "\n", |
|
201 | 201 | "# Register an event to echo the filename when it has been changed.\n", |
|
202 | 202 | "def file_loading():\n", |
|
203 | 203 | " print(\"Loading %s\" % file_widget.filename)\n", |
|
204 | 204 | "file_widget.on_trait_change(file_loading, 'filename')\n", |
|
205 | 205 | "\n", |
|
206 | 206 | "# Register an event to echo the filename and contents when a file\n", |
|
207 | 207 | "# has been uploaded.\n", |
|
208 | 208 | "def file_loaded():\n", |
|
209 | 209 | " print(\"Loaded, file contents: %s\" % file_widget.value)\n", |
|
210 | 210 | "file_widget.on_trait_change(file_loaded, 'value')\n", |
|
211 | 211 | "\n", |
|
212 | 212 | "# Register an event to print an error message when a file could not\n", |
|
213 | 213 | "# be opened. Since the error messages are not handled through\n", |
|
214 | 214 | "# traitlets but instead handled through custom msgs, the registration\n", |
|
215 | 215 | "# of the handler is different than the two examples above. Instead\n", |
|
216 | 216 | "# the API provided by the CallbackDispatcher must be used.\n", |
|
217 | 217 | "def file_failed():\n", |
|
218 | 218 | " print(\"Could not load file contents of %s\" % file_widget.filename)\n", |
|
219 | 219 | "file_widget.errors.register_callback(file_failed)\n", |
|
220 | 220 | "\n", |
|
221 | 221 | "file_widget" |
|
222 | 222 | ], |
|
223 | 223 | "language": "python", |
|
224 | 224 | "metadata": {}, |
|
225 | 225 | "outputs": [ |
|
226 | 226 | { |
|
227 | 227 | "output_type": "stream", |
|
228 | 228 | "stream": "stdout", |
|
229 | 229 | "text": [ |
|
230 | 230 | "Loading test.txt\n" |
|
231 | 231 | ] |
|
232 | 232 | }, |
|
233 | 233 | { |
|
234 | 234 | "output_type": "stream", |
|
235 | 235 | "stream": "stdout", |
|
236 | 236 | "text": [ |
|
237 | 237 | "Loaded, file contents: Hello World!\n", |
|
238 | 238 | "\n" |
|
239 | 239 | ] |
|
240 | 240 | } |
|
241 | 241 | ], |
|
242 | 242 | "prompt_number": 4 |
|
243 | 243 | } |
|
244 | 244 | ], |
|
245 | 245 | "metadata": {} |
|
246 | 246 | } |
|
247 | 247 | ] |
|
248 | 248 | } |
General Comments 0
You need to be logged in to leave comments.
Login now