Show More
This diff has been collapsed as it changes many lines, (837 lines changed) Show them Hide them | |||||
@@ -0,0 +1,837 b'' | |||||
|
1 | { | |||
|
2 | "metadata": { | |||
|
3 | "name": "", | |||
|
4 | "signature": "sha256:1bcfb489c7d06f192acb47b6bbdecb16bb661cd695b6a2977221c17f521e81bb" | |||
|
5 | }, | |||
|
6 | "nbformat": 3, | |||
|
7 | "nbformat_minor": 0, | |||
|
8 | "worksheets": [ | |||
|
9 | { | |||
|
10 | "cells": [ | |||
|
11 | { | |||
|
12 | "cell_type": "code", | |||
|
13 | "collapsed": false, | |||
|
14 | "input": [ | |||
|
15 | "from __future__ import print_function # For py 2.7 compat" | |||
|
16 | ], | |||
|
17 | "language": "python", | |||
|
18 | "metadata": {}, | |||
|
19 | "outputs": [], | |||
|
20 | "prompt_number": 20 | |||
|
21 | }, | |||
|
22 | { | |||
|
23 | "cell_type": "heading", | |||
|
24 | "level": 1, | |||
|
25 | "metadata": {}, | |||
|
26 | "source": [ | |||
|
27 | "Building a Custom Widget" | |||
|
28 | ] | |||
|
29 | }, | |||
|
30 | { | |||
|
31 | "cell_type": "markdown", | |||
|
32 | "metadata": {}, | |||
|
33 | "source": [ | |||
|
34 | "The widget framework is built on top of the Comm framework (short for communication). The Comm framework is a framework that allows you send/recieve JSON messages to/from the front-end. To create a custom widget, you need to define the widget both in the back-end and in the front-end. " | |||
|
35 | ] | |||
|
36 | }, | |||
|
37 | { | |||
|
38 | "cell_type": "heading", | |||
|
39 | "level": 2, | |||
|
40 | "metadata": {}, | |||
|
41 | "source": [ | |||
|
42 | "Back-end (Python)" | |||
|
43 | ] | |||
|
44 | }, | |||
|
45 | { | |||
|
46 | "cell_type": "heading", | |||
|
47 | "level": 3, | |||
|
48 | "metadata": {}, | |||
|
49 | "source": [ | |||
|
50 | "DOMWidget and Widget" | |||
|
51 | ] | |||
|
52 | }, | |||
|
53 | { | |||
|
54 | "cell_type": "markdown", | |||
|
55 | "metadata": {}, | |||
|
56 | "source": [ | |||
|
57 | "To define a widget, you must inherit from the Widget or DOMWidget base class. If you intend for your widget to be displayed in the IPython notebook, you'll need to inherit from the DOMWidget. The DOMWidget class itself inherits from the Widget class. The Widget class is useful for cases in which the Widget isn't meant to be displayed directly in the notebook, but instead as a child of another rendering environment. For example, if you wanted to create a D3.js widget (a popular WebGL library), you would implement the rendering window as a DOMWidget and any 3D objects or lights meant to be rendered in that window as Widgets." | |||
|
58 | ] | |||
|
59 | }, | |||
|
60 | { | |||
|
61 | "cell_type": "heading", | |||
|
62 | "level": 3, | |||
|
63 | "metadata": {}, | |||
|
64 | "source": [ | |||
|
65 | "sync=True traitlets" | |||
|
66 | ] | |||
|
67 | }, | |||
|
68 | { | |||
|
69 | "cell_type": "markdown", | |||
|
70 | "metadata": {}, | |||
|
71 | "source": [ | |||
|
72 | "Inheriting from the DOMWidget doesn't tell the widget framework what front-end widget to associate with your back-end widget. Instead, you must tell it yourself by defining a specially named Traitlet, `_view_name` (as seen below)." | |||
|
73 | ] | |||
|
74 | }, | |||
|
75 | { | |||
|
76 | "cell_type": "code", | |||
|
77 | "collapsed": false, | |||
|
78 | "input": [ | |||
|
79 | "from IPython.html import widgets\n", | |||
|
80 | "from IPython.utils.traitlets import Unicode\n", | |||
|
81 | "\n", | |||
|
82 | "class HelloWidget(widgets.DOMWidget):\n", | |||
|
83 | " _view_name = Unicode('HelloView', sync=True)" | |||
|
84 | ], | |||
|
85 | "language": "python", | |||
|
86 | "metadata": {}, | |||
|
87 | "outputs": [], | |||
|
88 | "prompt_number": 21 | |||
|
89 | }, | |||
|
90 | { | |||
|
91 | "cell_type": "markdown", | |||
|
92 | "metadata": {}, | |||
|
93 | "source": [ | |||
|
94 | "Traitlets is an IPython library for defining type-safe properties on configurable objects. For this tutorial you do not need to worry about the *configurable* piece of the traitlets machinery. The `sync=True` kwarg tells the widget framework to handle synchronizing that value to the front-end. Without `sync=True`, the front-end would have no knowlege of `_view_name`." | |||
|
95 | ] | |||
|
96 | }, | |||
|
97 | { | |||
|
98 | "cell_type": "heading", | |||
|
99 | "level": 3, | |||
|
100 | "metadata": {}, | |||
|
101 | "source": [ | |||
|
102 | "Other traitlet types" | |||
|
103 | ] | |||
|
104 | }, | |||
|
105 | { | |||
|
106 | "cell_type": "markdown", | |||
|
107 | "metadata": {}, | |||
|
108 | "source": [ | |||
|
109 | "Unicode is not the only Traitlet type, there are many more: \n", | |||
|
110 | "\n", | |||
|
111 | "- Any\n", | |||
|
112 | "- Bool\n", | |||
|
113 | "- Bytes\n", | |||
|
114 | "- CBool\n", | |||
|
115 | "- CBytes\n", | |||
|
116 | "- CComplex\n", | |||
|
117 | "- CFloat\n", | |||
|
118 | "- CInt\n", | |||
|
119 | "- CLong\n", | |||
|
120 | "- CRegExp\n", | |||
|
121 | "- CUnicode\n", | |||
|
122 | "- CaselessStrEnum\n", | |||
|
123 | "- Complex\n", | |||
|
124 | "- Dict\n", | |||
|
125 | "- DottedObjectName\n", | |||
|
126 | "- Enum\n", | |||
|
127 | "- Float\n", | |||
|
128 | "- FunctionType\n", | |||
|
129 | "- Instance\n", | |||
|
130 | "- InstanceType\n", | |||
|
131 | "- Int\n", | |||
|
132 | "- List\n", | |||
|
133 | "- Long\n", | |||
|
134 | "- Set\n", | |||
|
135 | "- TCPAddress\n", | |||
|
136 | "- Tuple\n", | |||
|
137 | "- Type\n", | |||
|
138 | "- Unicode\n", | |||
|
139 | "\n", | |||
|
140 | "Not all of these traitlets can be synchronized across the network, only the JSON-able traits and Widget instances will be synchronized." | |||
|
141 | ] | |||
|
142 | }, | |||
|
143 | { | |||
|
144 | "cell_type": "heading", | |||
|
145 | "level": 2, | |||
|
146 | "metadata": {}, | |||
|
147 | "source": [ | |||
|
148 | "Front-end (JavaScript)" | |||
|
149 | ] | |||
|
150 | }, | |||
|
151 | { | |||
|
152 | "cell_type": "heading", | |||
|
153 | "level": 3, | |||
|
154 | "metadata": {}, | |||
|
155 | "source": [ | |||
|
156 | "Models and views" | |||
|
157 | ] | |||
|
158 | }, | |||
|
159 | { | |||
|
160 | "cell_type": "markdown", | |||
|
161 | "metadata": {}, | |||
|
162 | "source": [ | |||
|
163 | "The IPython widget framework front-end relies heavily on [Backbone.js](http://backbonejs.org/). Backbone.js is an MVC (model view controller) framework. Widgets defined in the back-end are automatically synchronized with generic Backbone.js models in the front-end. The traitlets are added to the front-end instance automatically on first state push. The `_view_name` trait that you defined earlier is used by the widget framework to create the corresponding Backbone.js view and link that view to the generic model." | |||
|
164 | ] | |||
|
165 | }, | |||
|
166 | { | |||
|
167 | "cell_type": "heading", | |||
|
168 | "level": 3, | |||
|
169 | "metadata": {}, | |||
|
170 | "source": [ | |||
|
171 | "Defining a view" | |||
|
172 | ] | |||
|
173 | }, | |||
|
174 | { | |||
|
175 | "cell_type": "markdown", | |||
|
176 | "metadata": {}, | |||
|
177 | "source": [ | |||
|
178 | "You first need to import the WidgetManager. You will use it later to register your view by name (the same name you used in the backend). To import the widget manager, use the `require` method of [require.js](http://requirejs.org/) (as seen below)." | |||
|
179 | ] | |||
|
180 | }, | |||
|
181 | { | |||
|
182 | "cell_type": "code", | |||
|
183 | "collapsed": false, | |||
|
184 | "input": [ | |||
|
185 | "%%javascript\n", | |||
|
186 | "\n", | |||
|
187 | "require([\"widgets/js/widget\"], function(WidgetManager){\n", | |||
|
188 | " \n", | |||
|
189 | "});" | |||
|
190 | ], | |||
|
191 | "language": "python", | |||
|
192 | "metadata": {}, | |||
|
193 | "outputs": [ | |||
|
194 | { | |||
|
195 | "javascript": [ | |||
|
196 | "\n", | |||
|
197 | "require([\"widgets/js/widget\"], function(WidgetManager){\n", | |||
|
198 | " \n", | |||
|
199 | "});" | |||
|
200 | ], | |||
|
201 | "metadata": {}, | |||
|
202 | "output_type": "display_data", | |||
|
203 | "text": [ | |||
|
204 | "<IPython.core.display.Javascript object>" | |||
|
205 | ] | |||
|
206 | } | |||
|
207 | ], | |||
|
208 | "prompt_number": 22 | |||
|
209 | }, | |||
|
210 | { | |||
|
211 | "cell_type": "markdown", | |||
|
212 | "metadata": {}, | |||
|
213 | "source": [ | |||
|
214 | "Next define your widget view class. Inherit from the `DOMWidgetView` by using the `.extend` method. Register the view class with the widget manager by calling `.register_widget_view`. The first parameter is the widget view name (`_view_name` that you defined earlier in Python) and the second is a handle to the class type." | |||
|
215 | ] | |||
|
216 | }, | |||
|
217 | { | |||
|
218 | "cell_type": "code", | |||
|
219 | "collapsed": false, | |||
|
220 | "input": [ | |||
|
221 | "%%javascript\n", | |||
|
222 | "\n", | |||
|
223 | "require([\"widgets/js/widget\"], function(WidgetManager){\n", | |||
|
224 | " \n", | |||
|
225 | " // Define the HelloView\n", | |||
|
226 | " var HelloView = IPython.DOMWidgetView.extend({\n", | |||
|
227 | " \n", | |||
|
228 | " });\n", | |||
|
229 | " \n", | |||
|
230 | " // Register the HelloView with the widget manager.\n", | |||
|
231 | " WidgetManager.register_widget_view('HelloView', HelloView);\n", | |||
|
232 | "});" | |||
|
233 | ], | |||
|
234 | "language": "python", | |||
|
235 | "metadata": {}, | |||
|
236 | "outputs": [ | |||
|
237 | { | |||
|
238 | "javascript": [ | |||
|
239 | "\n", | |||
|
240 | "require([\"widgets/js/widget\"], function(WidgetManager){\n", | |||
|
241 | " \n", | |||
|
242 | " // Define the HelloView\n", | |||
|
243 | " var HelloView = IPython.DOMWidgetView.extend({\n", | |||
|
244 | " \n", | |||
|
245 | " });\n", | |||
|
246 | " \n", | |||
|
247 | " // Register the HelloView with the widget manager.\n", | |||
|
248 | " WidgetManager.register_widget_view('HelloView', HelloView);\n", | |||
|
249 | "});" | |||
|
250 | ], | |||
|
251 | "metadata": {}, | |||
|
252 | "output_type": "display_data", | |||
|
253 | "text": [ | |||
|
254 | "<IPython.core.display.Javascript object>" | |||
|
255 | ] | |||
|
256 | } | |||
|
257 | ], | |||
|
258 | "prompt_number": 23 | |||
|
259 | }, | |||
|
260 | { | |||
|
261 | "cell_type": "markdown", | |||
|
262 | "metadata": {}, | |||
|
263 | "source": [ | |||
|
264 | "Lastly, override the base `render` method of the view to define custom rendering logic. A handle to the widget's default div element can be aquired via `this.$el`. The `$el` property is a [jQuery](http://jquery.com/) object handle (which can be thought of as a supercharged version of the normal DOM element's handle)." | |||
|
265 | ] | |||
|
266 | }, | |||
|
267 | { | |||
|
268 | "cell_type": "code", | |||
|
269 | "collapsed": false, | |||
|
270 | "input": [ | |||
|
271 | "%%javascript\n", | |||
|
272 | "\n", | |||
|
273 | "require([\"widgets/js/widget\"], function(WidgetManager){ \n", | |||
|
274 | " \n", | |||
|
275 | " var HelloView = IPython.DOMWidgetView.extend({\n", | |||
|
276 | " \n", | |||
|
277 | " // Render the view.\n", | |||
|
278 | " render: function(){ \n", | |||
|
279 | " this.$el.text('Hello World!'); \n", | |||
|
280 | " },\n", | |||
|
281 | " });\n", | |||
|
282 | " \n", | |||
|
283 | " WidgetManager.register_widget_view('HelloView', HelloView);\n", | |||
|
284 | "});" | |||
|
285 | ], | |||
|
286 | "language": "python", | |||
|
287 | "metadata": {}, | |||
|
288 | "outputs": [ | |||
|
289 | { | |||
|
290 | "javascript": [ | |||
|
291 | "\n", | |||
|
292 | "require([\"widgets/js/widget\"], function(WidgetManager){ \n", | |||
|
293 | " \n", | |||
|
294 | " var HelloView = IPython.DOMWidgetView.extend({\n", | |||
|
295 | " \n", | |||
|
296 | " // Render the view.\n", | |||
|
297 | " render: function(){ \n", | |||
|
298 | " this.$el.text('Hello World!'); \n", | |||
|
299 | " },\n", | |||
|
300 | " });\n", | |||
|
301 | " \n", | |||
|
302 | " WidgetManager.register_widget_view('HelloView', HelloView);\n", | |||
|
303 | "});" | |||
|
304 | ], | |||
|
305 | "metadata": {}, | |||
|
306 | "output_type": "display_data", | |||
|
307 | "text": [ | |||
|
308 | "<IPython.core.display.Javascript object>" | |||
|
309 | ] | |||
|
310 | } | |||
|
311 | ], | |||
|
312 | "prompt_number": 24 | |||
|
313 | }, | |||
|
314 | { | |||
|
315 | "cell_type": "heading", | |||
|
316 | "level": 2, | |||
|
317 | "metadata": {}, | |||
|
318 | "source": [ | |||
|
319 | "Test" | |||
|
320 | ] | |||
|
321 | }, | |||
|
322 | { | |||
|
323 | "cell_type": "markdown", | |||
|
324 | "metadata": {}, | |||
|
325 | "source": [ | |||
|
326 | "You should be able to display your widget just like any other widget now." | |||
|
327 | ] | |||
|
328 | }, | |||
|
329 | { | |||
|
330 | "cell_type": "code", | |||
|
331 | "collapsed": false, | |||
|
332 | "input": [ | |||
|
333 | "HelloWidget()" | |||
|
334 | ], | |||
|
335 | "language": "python", | |||
|
336 | "metadata": {}, | |||
|
337 | "outputs": [], | |||
|
338 | "prompt_number": 25 | |||
|
339 | }, | |||
|
340 | { | |||
|
341 | "cell_type": "heading", | |||
|
342 | "level": 2, | |||
|
343 | "metadata": {}, | |||
|
344 | "source": [ | |||
|
345 | "Making the widget stateful" | |||
|
346 | ] | |||
|
347 | }, | |||
|
348 | { | |||
|
349 | "cell_type": "markdown", | |||
|
350 | "metadata": {}, | |||
|
351 | "source": [ | |||
|
352 | "There isn't much that you can do with the above example that you can't do with the IPython display framework. To change this, you will make the widget stateful. Instead of displaying a static \"hello world\" message, it will display a string set by the backend. First you need to add the traitlet in the back-end. Use the name of `value` to stay consistent with the rest of the widget framework and to allow your widget to be used with interact." | |||
|
353 | ] | |||
|
354 | }, | |||
|
355 | { | |||
|
356 | "cell_type": "code", | |||
|
357 | "collapsed": false, | |||
|
358 | "input": [ | |||
|
359 | "class HelloWidget(widgets.DOMWidget):\n", | |||
|
360 | " _view_name = Unicode('HelloView', sync=True)\n", | |||
|
361 | " value = Unicode('Hello World!', sync=True)" | |||
|
362 | ], | |||
|
363 | "language": "python", | |||
|
364 | "metadata": {}, | |||
|
365 | "outputs": [], | |||
|
366 | "prompt_number": 26 | |||
|
367 | }, | |||
|
368 | { | |||
|
369 | "cell_type": "heading", | |||
|
370 | "level": 3, | |||
|
371 | "metadata": {}, | |||
|
372 | "source": [ | |||
|
373 | "Backbone.js model" | |||
|
374 | ] | |||
|
375 | }, | |||
|
376 | { | |||
|
377 | "cell_type": "markdown", | |||
|
378 | "metadata": {}, | |||
|
379 | "source": [ | |||
|
380 | "To access the model associate with a view instance, use the `model` property of the view. `get` and `set` methods are used to interact with the Backbone model. `get` is trivial, however you have to be careful when using `set`. After calling the model `set` you need call the view's `touch` method. This associates the `set` operation with a particular view so output will be routed to the correct IPython cell. The model also has a `on` method which allows you to listen to events triggered by the model (like value changes)." | |||
|
381 | ] | |||
|
382 | }, | |||
|
383 | { | |||
|
384 | "cell_type": "heading", | |||
|
385 | "level": 3, | |||
|
386 | "metadata": {}, | |||
|
387 | "source": [ | |||
|
388 | "Rendering model contents" | |||
|
389 | ] | |||
|
390 | }, | |||
|
391 | { | |||
|
392 | "cell_type": "markdown", | |||
|
393 | "metadata": {}, | |||
|
394 | "source": [ | |||
|
395 | "By replacing the string literal with a call to `model.get`, the view will now display the value of the back-end upon display. However, it will not update itself to a new value when the value changes." | |||
|
396 | ] | |||
|
397 | }, | |||
|
398 | { | |||
|
399 | "cell_type": "code", | |||
|
400 | "collapsed": false, | |||
|
401 | "input": [ | |||
|
402 | "%%javascript\n", | |||
|
403 | "\n", | |||
|
404 | "require([\"widgets/js/widget\"], function(WidgetManager){ \n", | |||
|
405 | " \n", | |||
|
406 | " var HelloView = IPython.DOMWidgetView.extend({\n", | |||
|
407 | " \n", | |||
|
408 | " render: function(){ \n", | |||
|
409 | " this.$el.text(this.model.get('value')); \n", | |||
|
410 | " },\n", | |||
|
411 | " });\n", | |||
|
412 | " \n", | |||
|
413 | " WidgetManager.register_widget_view('HelloView', HelloView);\n", | |||
|
414 | "});" | |||
|
415 | ], | |||
|
416 | "language": "python", | |||
|
417 | "metadata": {}, | |||
|
418 | "outputs": [ | |||
|
419 | { | |||
|
420 | "javascript": [ | |||
|
421 | "\n", | |||
|
422 | "require([\"widgets/js/widget\"], function(WidgetManager){ \n", | |||
|
423 | " \n", | |||
|
424 | " var HelloView = IPython.DOMWidgetView.extend({\n", | |||
|
425 | " \n", | |||
|
426 | " render: function(){ \n", | |||
|
427 | " this.$el.text(this.model.get('value')); \n", | |||
|
428 | " },\n", | |||
|
429 | " });\n", | |||
|
430 | " \n", | |||
|
431 | " WidgetManager.register_widget_view('HelloView', HelloView);\n", | |||
|
432 | "});" | |||
|
433 | ], | |||
|
434 | "metadata": {}, | |||
|
435 | "output_type": "display_data", | |||
|
436 | "text": [ | |||
|
437 | "<IPython.core.display.Javascript object>" | |||
|
438 | ] | |||
|
439 | } | |||
|
440 | ], | |||
|
441 | "prompt_number": 27 | |||
|
442 | }, | |||
|
443 | { | |||
|
444 | "cell_type": "markdown", | |||
|
445 | "metadata": {}, | |||
|
446 | "source": [ | |||
|
447 | "To get the view to update itself dynamically, register a function to update the view's value when the model's `value` property changes. This can be done using the `model.on` method. The `on` method takes three parameters, an event name, callback handle, and callback context. The Backbone event named `change` will fire whenever the model changes. By appending `:value` to it, you tell Backbone to only listen to the change event of the `value` property (as seen below)." | |||
|
448 | ] | |||
|
449 | }, | |||
|
450 | { | |||
|
451 | "cell_type": "code", | |||
|
452 | "collapsed": false, | |||
|
453 | "input": [ | |||
|
454 | "%%javascript\n", | |||
|
455 | "\n", | |||
|
456 | "require([\"widgets/js/widget\"], function(WidgetManager){ \n", | |||
|
457 | " \n", | |||
|
458 | " var HelloView = IPython.DOMWidgetView.extend({\n", | |||
|
459 | " \n", | |||
|
460 | " \n", | |||
|
461 | " render: function(){ \n", | |||
|
462 | " this.value_changed();\n", | |||
|
463 | " this.model.on('change:value', this.value_changed, this);\n", | |||
|
464 | " },\n", | |||
|
465 | " \n", | |||
|
466 | " value_changed: function() {\n", | |||
|
467 | " this.$el.text(this.model.get('value')); \n", | |||
|
468 | " },\n", | |||
|
469 | " });\n", | |||
|
470 | " \n", | |||
|
471 | " WidgetManager.register_widget_view('HelloView', HelloView);\n", | |||
|
472 | "});" | |||
|
473 | ], | |||
|
474 | "language": "python", | |||
|
475 | "metadata": {}, | |||
|
476 | "outputs": [ | |||
|
477 | { | |||
|
478 | "javascript": [ | |||
|
479 | "\n", | |||
|
480 | "require([\"widgets/js/widget\"], function(WidgetManager){ \n", | |||
|
481 | " \n", | |||
|
482 | " var HelloView = IPython.DOMWidgetView.extend({\n", | |||
|
483 | " \n", | |||
|
484 | " \n", | |||
|
485 | " render: function(){ \n", | |||
|
486 | " this.value_changed();\n", | |||
|
487 | " this.model.on('change:value', this.value_changed, this);\n", | |||
|
488 | " },\n", | |||
|
489 | " \n", | |||
|
490 | " value_changed: function() {\n", | |||
|
491 | " this.$el.text(this.model.get('value')); \n", | |||
|
492 | " },\n", | |||
|
493 | " });\n", | |||
|
494 | " \n", | |||
|
495 | " WidgetManager.register_widget_view('HelloView', HelloView);\n", | |||
|
496 | "});" | |||
|
497 | ], | |||
|
498 | "metadata": {}, | |||
|
499 | "output_type": "display_data", | |||
|
500 | "text": [ | |||
|
501 | "<IPython.core.display.Javascript object>" | |||
|
502 | ] | |||
|
503 | } | |||
|
504 | ], | |||
|
505 | "prompt_number": 38 | |||
|
506 | }, | |||
|
507 | { | |||
|
508 | "cell_type": "heading", | |||
|
509 | "level": 2, | |||
|
510 | "metadata": {}, | |||
|
511 | "source": [ | |||
|
512 | "Test" | |||
|
513 | ] | |||
|
514 | }, | |||
|
515 | { | |||
|
516 | "cell_type": "code", | |||
|
517 | "collapsed": false, | |||
|
518 | "input": [ | |||
|
519 | "w = HelloWidget()\n", | |||
|
520 | "w" | |||
|
521 | ], | |||
|
522 | "language": "python", | |||
|
523 | "metadata": {}, | |||
|
524 | "outputs": [], | |||
|
525 | "prompt_number": 39 | |||
|
526 | }, | |||
|
527 | { | |||
|
528 | "cell_type": "code", | |||
|
529 | "collapsed": false, | |||
|
530 | "input": [ | |||
|
531 | "w.value = 'test'" | |||
|
532 | ], | |||
|
533 | "language": "python", | |||
|
534 | "metadata": {}, | |||
|
535 | "outputs": [], | |||
|
536 | "prompt_number": 40 | |||
|
537 | }, | |||
|
538 | { | |||
|
539 | "cell_type": "heading", | |||
|
540 | "level": 2, | |||
|
541 | "metadata": {}, | |||
|
542 | "source": [ | |||
|
543 | "Bidirectional communication" | |||
|
544 | ] | |||
|
545 | }, | |||
|
546 | { | |||
|
547 | "cell_type": "markdown", | |||
|
548 | "metadata": {}, | |||
|
549 | "source": [ | |||
|
550 | "The examples above dump the value directly into the DOM. There is no way for you to interact with this dumped data in the front-end. To create an example that accepts input, you will have to do something more than blindly dumping the contents of value into the DOM. For this part of the tutorial, you will use a jQuery spinner to display and accept input in the front-end. IPython currently lacks a spinner implementation so this widget will be unique." | |||
|
551 | ] | |||
|
552 | }, | |||
|
553 | { | |||
|
554 | "cell_type": "heading", | |||
|
555 | "level": 3, | |||
|
556 | "metadata": {}, | |||
|
557 | "source": [ | |||
|
558 | "Update the Python code" | |||
|
559 | ] | |||
|
560 | }, | |||
|
561 | { | |||
|
562 | "cell_type": "markdown", | |||
|
563 | "metadata": {}, | |||
|
564 | "source": [ | |||
|
565 | "You will need to change the type of the value traitlet to `Int`. It also makes sense to change the name of the widget to something more appropriate, like `SpinnerWidget`." | |||
|
566 | ] | |||
|
567 | }, | |||
|
568 | { | |||
|
569 | "cell_type": "code", | |||
|
570 | "collapsed": false, | |||
|
571 | "input": [ | |||
|
572 | "from IPython.utils.traitlets import CInt\n", | |||
|
573 | "class SpinnerWidget(widgets.DOMWidget):\n", | |||
|
574 | " _view_name = Unicode('SpinnerView', sync=True)\n", | |||
|
575 | " value = CInt(0, sync=True)" | |||
|
576 | ], | |||
|
577 | "language": "python", | |||
|
578 | "metadata": {}, | |||
|
579 | "outputs": [], | |||
|
580 | "prompt_number": 41 | |||
|
581 | }, | |||
|
582 | { | |||
|
583 | "cell_type": "heading", | |||
|
584 | "level": 3, | |||
|
585 | "metadata": {}, | |||
|
586 | "source": [ | |||
|
587 | "Updating the Javascript code" | |||
|
588 | ] | |||
|
589 | }, | |||
|
590 | { | |||
|
591 | "cell_type": "markdown", | |||
|
592 | "metadata": {}, | |||
|
593 | "source": [ | |||
|
594 | "The [jQuery docs for the spinner control](https://jqueryui.com/spinner/) say to use `.spinner` to create a spinner in an element. Calling `.spinner` on `$el` will create a spinner inside `$el`. Make sure to update the widget name here too so it's the same as `_view_name` in the back-end." | |||
|
595 | ] | |||
|
596 | }, | |||
|
597 | { | |||
|
598 | "cell_type": "code", | |||
|
599 | "collapsed": false, | |||
|
600 | "input": [ | |||
|
601 | "%%javascript\n", | |||
|
602 | "\n", | |||
|
603 | "require([\"widgets/js/widget\"], function(WidgetManager){ \n", | |||
|
604 | " \n", | |||
|
605 | " var SpinnerView = IPython.DOMWidgetView.extend({\n", | |||
|
606 | " \n", | |||
|
607 | " render: function(){ \n", | |||
|
608 | " \n", | |||
|
609 | " // jQuery code to create a spinner and append it to $el\n", | |||
|
610 | " this.$input = $('<input />');\n", | |||
|
611 | " this.$el.append(this.$input);\n", | |||
|
612 | " this.$spinner = this.$input.spinner({\n", | |||
|
613 | " change: function( event, ui ) {}\n", | |||
|
614 | " });\n", | |||
|
615 | " \n", | |||
|
616 | " this.value_changed();\n", | |||
|
617 | " this.model.on('change:value', this.value_changed, this);\n", | |||
|
618 | " },\n", | |||
|
619 | " \n", | |||
|
620 | " value_changed: function() {\n", | |||
|
621 | " \n", | |||
|
622 | " },\n", | |||
|
623 | " });\n", | |||
|
624 | " \n", | |||
|
625 | " WidgetManager.register_widget_view('SpinnerView', SpinnerView);\n", | |||
|
626 | "});" | |||
|
627 | ], | |||
|
628 | "language": "python", | |||
|
629 | "metadata": {}, | |||
|
630 | "outputs": [ | |||
|
631 | { | |||
|
632 | "javascript": [ | |||
|
633 | "\n", | |||
|
634 | "require([\"widgets/js/widget\"], function(WidgetManager){ \n", | |||
|
635 | " \n", | |||
|
636 | " var SpinnerView = IPython.DOMWidgetView.extend({\n", | |||
|
637 | " \n", | |||
|
638 | " render: function(){ \n", | |||
|
639 | " this.$spinner = this.$el.spinner();\n", | |||
|
640 | " this.value_changed();\n", | |||
|
641 | " this.model.on('change:value', this.value_changed, this);\n", | |||
|
642 | " },\n", | |||
|
643 | " \n", | |||
|
644 | " value_changed: function() {\n", | |||
|
645 | " //this.$el.text(this.model.get('value')); \n", | |||
|
646 | " },\n", | |||
|
647 | " });\n", | |||
|
648 | " \n", | |||
|
649 | " WidgetManager.register_widget_view('SpinnerView', SpinnerView);\n", | |||
|
650 | "});" | |||
|
651 | ], | |||
|
652 | "metadata": {}, | |||
|
653 | "output_type": "display_data", | |||
|
654 | "text": [ | |||
|
655 | "<IPython.core.display.Javascript object>" | |||
|
656 | ] | |||
|
657 | } | |||
|
658 | ], | |||
|
659 | "prompt_number": 44 | |||
|
660 | }, | |||
|
661 | { | |||
|
662 | "cell_type": "markdown", | |||
|
663 | "metadata": {}, | |||
|
664 | "source": [ | |||
|
665 | "To set the value of the spinner on update, you need to use jQuery's spinner API. `spinner.spinner('value', new)` will set the value of the spinner. Add that code to the `value_changed` method to make the spinner update with the value stored in the back-end. Using jQuery's spinner API, you can add a function to handle the spinner `change` event by passing it in when constructing the spinner. Inside the `change` event, call `model.set` to set the value and then `touch` to inform the framework that this view was the view that caused the change to the model. Note: The `var that = this;` is a JavaScript trick to pass the current context into closures." | |||
|
666 | ] | |||
|
667 | }, | |||
|
668 | { | |||
|
669 | "cell_type": "code", | |||
|
670 | "collapsed": false, | |||
|
671 | "input": [ | |||
|
672 | "%%javascript\n", | |||
|
673 | "\n", | |||
|
674 | "require([\"widgets/js/widget\"], function(WidgetManager){ \n", | |||
|
675 | " \n", | |||
|
676 | " var SpinnerView = IPython.DOMWidgetView.extend({\n", | |||
|
677 | " \n", | |||
|
678 | " render: function(){ \n", | |||
|
679 | "\n", | |||
|
680 | " var that = this;\n", | |||
|
681 | " this.$input = $('<input />');\n", | |||
|
682 | " this.$el.append(this.$input);\n", | |||
|
683 | " this.$spinner = this.$input.spinner({\n", | |||
|
684 | " change: function( event, ui ) {\n", | |||
|
685 | " that.handle_spin();\n", | |||
|
686 | " },\n", | |||
|
687 | " spin: function( event, ui ) {\n", | |||
|
688 | " that.handle_spin();\n", | |||
|
689 | " }\n", | |||
|
690 | " });\n", | |||
|
691 | " \n", | |||
|
692 | " this.value_changed();\n", | |||
|
693 | " this.model.on('change:value', this.value_changed, this);\n", | |||
|
694 | " },\n", | |||
|
695 | " \n", | |||
|
696 | " value_changed: function() {\n", | |||
|
697 | " this.$spinner.spinner('value', this.model.get('value'));\n", | |||
|
698 | " },\n", | |||
|
699 | " \n", | |||
|
700 | " handle_spin: function() {\n", | |||
|
701 | " this.model.set('value', this.$spinner.spinner('value'));\n", | |||
|
702 | " this.touch();\n", | |||
|
703 | " },\n", | |||
|
704 | " });\n", | |||
|
705 | " \n", | |||
|
706 | " WidgetManager.register_widget_view('SpinnerView', SpinnerView);\n", | |||
|
707 | "});" | |||
|
708 | ], | |||
|
709 | "language": "python", | |||
|
710 | "metadata": {}, | |||
|
711 | "outputs": [ | |||
|
712 | { | |||
|
713 | "javascript": [ | |||
|
714 | "\n", | |||
|
715 | "require([\"widgets/js/widget\"], function(WidgetManager){ \n", | |||
|
716 | " \n", | |||
|
717 | " var SpinnerView = IPython.DOMWidgetView.extend({\n", | |||
|
718 | " \n", | |||
|
719 | " render: function(){ \n", | |||
|
720 | "\n", | |||
|
721 | " var that = this;\n", | |||
|
722 | " this.$input = $('<input />');\n", | |||
|
723 | " this.$el.append(this.$input);\n", | |||
|
724 | " this.$spinner = this.$input.spinner({\n", | |||
|
725 | " change: function( event, ui ) {\n", | |||
|
726 | " that.handle_spin();\n", | |||
|
727 | " },\n", | |||
|
728 | " spin: function( event, ui ) {\n", | |||
|
729 | " that.handle_spin();\n", | |||
|
730 | " }\n", | |||
|
731 | " });\n", | |||
|
732 | " \n", | |||
|
733 | " this.value_changed();\n", | |||
|
734 | " this.model.on('change:value', this.value_changed, this);\n", | |||
|
735 | " },\n", | |||
|
736 | " \n", | |||
|
737 | " value_changed: function() {\n", | |||
|
738 | " this.$spinner.spinner('value', this.model.get('value'));\n", | |||
|
739 | " },\n", | |||
|
740 | " \n", | |||
|
741 | " handle_spin: function() {\n", | |||
|
742 | " this.model.set('value', this.$spinner.spinner('value'));\n", | |||
|
743 | " this.touch();\n", | |||
|
744 | " },\n", | |||
|
745 | " });\n", | |||
|
746 | " \n", | |||
|
747 | " WidgetManager.register_widget_view('SpinnerView', SpinnerView);\n", | |||
|
748 | "});" | |||
|
749 | ], | |||
|
750 | "metadata": {}, | |||
|
751 | "output_type": "display_data", | |||
|
752 | "text": [ | |||
|
753 | "<IPython.core.display.Javascript object>" | |||
|
754 | ] | |||
|
755 | } | |||
|
756 | ], | |||
|
757 | "prompt_number": 74 | |||
|
758 | }, | |||
|
759 | { | |||
|
760 | "cell_type": "heading", | |||
|
761 | "level": 2, | |||
|
762 | "metadata": {}, | |||
|
763 | "source": [ | |||
|
764 | "Test" | |||
|
765 | ] | |||
|
766 | }, | |||
|
767 | { | |||
|
768 | "cell_type": "code", | |||
|
769 | "collapsed": false, | |||
|
770 | "input": [ | |||
|
771 | "w = SpinnerWidget(value=5)\n", | |||
|
772 | "w" | |||
|
773 | ], | |||
|
774 | "language": "python", | |||
|
775 | "metadata": {}, | |||
|
776 | "outputs": [], | |||
|
777 | "prompt_number": 75 | |||
|
778 | }, | |||
|
779 | { | |||
|
780 | "cell_type": "code", | |||
|
781 | "collapsed": false, | |||
|
782 | "input": [ | |||
|
783 | "w.value" | |||
|
784 | ], | |||
|
785 | "language": "python", | |||
|
786 | "metadata": {}, | |||
|
787 | "outputs": [ | |||
|
788 | { | |||
|
789 | "metadata": {}, | |||
|
790 | "output_type": "pyout", | |||
|
791 | "prompt_number": 76, | |||
|
792 | "text": [ | |||
|
793 | "5" | |||
|
794 | ] | |||
|
795 | } | |||
|
796 | ], | |||
|
797 | "prompt_number": 76 | |||
|
798 | }, | |||
|
799 | { | |||
|
800 | "cell_type": "code", | |||
|
801 | "collapsed": false, | |||
|
802 | "input": [ | |||
|
803 | "w.value = 20" | |||
|
804 | ], | |||
|
805 | "language": "python", | |||
|
806 | "metadata": {}, | |||
|
807 | "outputs": [], | |||
|
808 | "prompt_number": 77 | |||
|
809 | }, | |||
|
810 | { | |||
|
811 | "cell_type": "markdown", | |||
|
812 | "metadata": {}, | |||
|
813 | "source": [ | |||
|
814 | "Trying to use the spinner with another widget." | |||
|
815 | ] | |||
|
816 | }, | |||
|
817 | { | |||
|
818 | "cell_type": "code", | |||
|
819 | "collapsed": false, | |||
|
820 | "input": [ | |||
|
821 | "w1 = SpinnerWidget(value=0)\n", | |||
|
822 | "w2 = widgets.IntSliderWidget()\n", | |||
|
823 | "display(w1,w2)\n", | |||
|
824 | "\n", | |||
|
825 | "from IPython.utils.traitlets import link\n", | |||
|
826 | "mylink = link((w1, 'value'), (w2, 'value'))" | |||
|
827 | ], | |||
|
828 | "language": "python", | |||
|
829 | "metadata": {}, | |||
|
830 | "outputs": [], | |||
|
831 | "prompt_number": 78 | |||
|
832 | } | |||
|
833 | ], | |||
|
834 | "metadata": {} | |||
|
835 | } | |||
|
836 | ] | |||
|
837 | } No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now