##// END OF EJS Templates
Update date picker widget example...
Thomas Kluyver -
Show More
@@ -1,1046 +1,837 b''
1 {
1 {
2 "cells": [
2 "cells": [
3 {
3 {
4 "cell_type": "markdown",
4 "cell_type": "markdown",
5 "metadata": {},
5 "metadata": {},
6 "source": [
6 "source": [
7 "Before reading, make sure to review\n",
7 "Before reading, make sure to review\n",
8 "\n",
8 "\n",
9 "- [MVC prgramming](http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller)\n",
9 "- [MVC prgramming](http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller)\n",
10 "- [Backbone.js](https://www.codeschool.com/courses/anatomy-of-backbonejs)\n",
10 "- [Backbone.js](https://www.codeschool.com/courses/anatomy-of-backbonejs)\n",
11 "- [The widget IPEP](https://github.com/ipython/ipython/wiki/IPEP-23%3A-Backbone.js-Widgets)\n",
11 "- [The widget IPEP](https://github.com/ipython/ipython/wiki/IPEP-23%3A-Backbone.js-Widgets)\n",
12 "- [The original widget PR discussion](https://github.com/ipython/ipython/pull/4374)"
12 "- [The original widget PR discussion](https://github.com/ipython/ipython/pull/4374)"
13 ]
13 ]
14 },
14 },
15 {
15 {
16 "cell_type": "code",
16 "cell_type": "code",
17 "execution_count": 1,
17 "execution_count": null,
18 "metadata": {
18 "metadata": {
19 "collapsed": false
19 "collapsed": false
20 },
20 },
21 "outputs": [],
21 "outputs": [],
22 "source": [
22 "source": [
23 "from __future__ import print_function # For py 2.7 compat\n",
23 "from __future__ import print_function # For py 2.7 compat\n",
24 "\n",
24 "\n",
25 "from IPython.html import widgets # Widget definitions\n",
25 "from IPython.html import widgets # Widget definitions\n",
26 "from IPython.display import display # Used to display widgets in the notebook\n",
26 "from IPython.display import display # Used to display widgets in the notebook\n",
27 "from IPython.utils.traitlets import Unicode # Used to declare attributes of our widget"
27 "from IPython.utils.traitlets import Unicode # Used to declare attributes of our widget"
28 ]
28 ]
29 },
29 },
30 {
30 {
31 "cell_type": "markdown",
31 "cell_type": "markdown",
32 "metadata": {},
32 "metadata": {},
33 "source": [
33 "source": [
34 "# Abstract"
34 "# Abstract"
35 ]
35 ]
36 },
36 },
37 {
37 {
38 "cell_type": "markdown",
38 "cell_type": "markdown",
39 "metadata": {},
39 "metadata": {},
40 "source": [
40 "source": [
41 "This notebook implements a custom date picker widget,\n",
41 "This notebook implements a custom date picker widget,\n",
42 "in order to demonstrate the widget creation process.\n",
42 "in order to demonstrate the widget creation process.\n",
43 "\n",
43 "\n",
44 "To create a custom widget, both Python and JavaScript code is required."
44 "To create a custom widget, both Python and JavaScript code is required."
45 ]
45 ]
46 },
46 },
47 {
47 {
48 "cell_type": "markdown",
48 "cell_type": "markdown",
49 "metadata": {},
49 "metadata": {},
50 "source": [
50 "source": [
51 "# Section 1 - Basics"
51 "# Section 1 - Basics"
52 ]
52 ]
53 },
53 },
54 {
54 {
55 "cell_type": "markdown",
55 "cell_type": "markdown",
56 "metadata": {},
56 "metadata": {},
57 "source": [
57 "source": [
58 "## Python"
58 "## Python"
59 ]
59 ]
60 },
60 },
61 {
61 {
62 "cell_type": "markdown",
62 "cell_type": "markdown",
63 "metadata": {},
63 "metadata": {},
64 "source": [
64 "source": [
65 "When starting a project like this, it is often easiest to make a simple base implementation,\n",
65 "When starting a project like this, it is often easiest to make a simple base implementation,\n",
66 "to verify that the underlying framework is working as expected.\n",
66 "to verify that the underlying framework is working as expected.\n",
67 "To start, we will create an empty widget and make sure that it can be rendered.\n",
67 "To start, we will create an empty widget and make sure that it can be rendered.\n",
68 "The first step is to define the widget in Python."
68 "The first step is to define the widget in Python."
69 ]
69 ]
70 },
70 },
71 {
71 {
72 "cell_type": "code",
72 "cell_type": "code",
73 "execution_count": 2,
73 "execution_count": null,
74 "metadata": {
74 "metadata": {
75 "collapsed": false
75 "collapsed": false
76 },
76 },
77 "outputs": [],
77 "outputs": [],
78 "source": [
78 "source": [
79 "class DateWidget(widgets.DOMWidget):\n",
79 "class DateWidget(widgets.DOMWidget):\n",
80 " _view_name = Unicode('DatePickerView', sync=True)"
80 " _view_name = Unicode('DatePickerView', sync=True)"
81 ]
81 ]
82 },
82 },
83 {
83 {
84 "cell_type": "markdown",
84 "cell_type": "markdown",
85 "metadata": {},
85 "metadata": {},
86 "source": [
86 "source": [
87 "Our widget inherits from `widgets.DOMWidget` since it is intended that it will be displayed in the notebook directly.\n",
87 "Our widget inherits from `widgets.DOMWidget` since it is intended that it will be displayed in the notebook directly.\n",
88 "The `_view_name` trait is special; the widget framework will read the `_view_name` trait to determine what Backbone view the widget is associated with.\n",
88 "The `_view_name` trait is special; the widget framework will read the `_view_name` trait to determine what Backbone view the widget is associated with.\n",
89 "**Using `sync=True` is very important** because it tells the widget framework that that specific traitlet should be synced between the front- and back-ends."
89 "**Using `sync=True` is very important** because it tells the widget framework that that specific traitlet should be synced between the front- and back-ends."
90 ]
90 ]
91 },
91 },
92 {
92 {
93 "cell_type": "markdown",
93 "cell_type": "markdown",
94 "metadata": {},
94 "metadata": {},
95 "source": [
95 "source": [
96 "## JavaScript"
96 "## JavaScript"
97 ]
97 ]
98 },
98 },
99 {
99 {
100 "cell_type": "markdown",
100 "cell_type": "markdown",
101 "metadata": {},
101 "metadata": {},
102 "source": [
102 "source": [
103 "In the IPython notebook [require.js](http://requirejs.org/) is used to load JavaScript dependencies.\n",
103 "In the IPython notebook [require.js](http://requirejs.org/) is used to load JavaScript dependencies.\n",
104 "All IPython widget code depends on `widgets/js/widget.js`,\n",
104 "All IPython widget code depends on `widgets/js/widget.js`,\n",
105 "where the base widget model and base view are defined.\n",
105 "where the base widget model and base view are defined.\n",
106 "We use require.js to load this file:"
106 "We use require.js to load this file:"
107 ]
107 ]
108 },
108 },
109 {
109 {
110 "cell_type": "code",
110 "cell_type": "code",
111 "execution_count": 3,
111 "execution_count": null,
112 "metadata": {
112 "metadata": {
113 "collapsed": false
113 "collapsed": false
114 },
114 },
115 "outputs": [
115 "outputs": [],
116 {
117 "data": {
118 "application/javascript": [
119 "\n",
120 "require([\"widgets/js/widget\"], function(WidgetManager){\n",
121 "\n",
122 "});"
123 ],
124 "text/plain": [
125 "<IPython.core.display.Javascript at 0x109491690>"
126 ]
127 },
128 "metadata": {},
129 "output_type": "display_data"
130 }
131 ],
132 "source": [
116 "source": [
133 "%%javascript\n",
117 "%%javascript\n",
134 "\n",
118 "\n",
135 "require([\"widgets/js/widget\"], function(WidgetManager){\n",
119 "require([\"widgets/js/widget\"], function(WidgetManager){\n",
136 "\n",
120 "\n",
137 "});"
121 "});"
138 ]
122 ]
139 },
123 },
140 {
124 {
141 "cell_type": "markdown",
125 "cell_type": "markdown",
142 "metadata": {},
126 "metadata": {},
143 "source": [
127 "source": [
144 "Now we need to define a view that can be used to represent the model.\n",
128 "Now we need to define a view that can be used to represent the model.\n",
145 "To do this, the `IPython.DOMWidgetView` is extended.\n",
129 "To do this, the `IPython.DOMWidgetView` is extended.\n",
146 "**A render function must be defined**.\n",
130 "**A render function must be defined**.\n",
147 "The render function is used to render a widget view instance to the DOM.\n",
131 "The render function is used to render a widget view instance to the DOM.\n",
148 "For now, the render function renders a div that contains the text *Hello World!*\n",
132 "For now, the render function renders a div that contains the text *Hello World!*\n",
149 "Lastly, the view needs to be registered with the widget manager.\n",
133 "Lastly, the view needs to be registered with the widget manager, for which we need to load another module.\n",
150 "\n",
134 "\n",
151 "**Final JavaScript code below:**"
135 "**Final JavaScript code below:**"
152 ]
136 ]
153 },
137 },
154 {
138 {
155 "cell_type": "code",
139 "cell_type": "code",
156 "execution_count": 4,
140 "execution_count": null,
157 "metadata": {
141 "metadata": {
158 "collapsed": false
142 "collapsed": false
159 },
143 },
160 "outputs": [
144 "outputs": [],
161 {
162 "data": {
163 "application/javascript": [
164 "\n",
165 "require([\"widgets/js/widget\"], function(WidgetManager){\n",
166 " \n",
167 " // Define the DatePickerView\n",
168 " var DatePickerView = IPython.DOMWidgetView.extend({\n",
169 " render: function(){ this.$el.text('Hello World!'); },\n",
170 " });\n",
171 " \n",
172 " // Register the DatePickerView with the widget manager.\n",
173 " WidgetManager.register_widget_view('DatePickerView', DatePickerView);\n",
174 "});"
175 ],
176 "text/plain": [
177 "<IPython.core.display.Javascript at 0x1094917d0>"
178 ]
179 },
180 "metadata": {},
181 "output_type": "display_data"
182 }
183 ],
184 "source": [
145 "source": [
185 "%%javascript\n",
146 "%%javascript\n",
186 "\n",
147 "\n",
187 "require([\"widgets/js/widget\"], function(WidgetManager){\n",
148 "require([\"widgets/js/widget\", \"widgets/js/manager\"], function(widget, manager){\n",
188 " \n",
149 " \n",
189 " // Define the DatePickerView\n",
150 " // Define the DatePickerView\n",
190 " var DatePickerView = IPython.DOMWidgetView.extend({\n",
151 " var DatePickerView = widget.DOMWidgetView.extend({\n",
191 " render: function(){ this.$el.text('Hello World!'); },\n",
152 " render: function(){ this.$el.text('Hello World!'); },\n",
192 " });\n",
153 " });\n",
193 " \n",
154 " \n",
194 " // Register the DatePickerView with the widget manager.\n",
155 " // Register the DatePickerView with the widget manager.\n",
195 " WidgetManager.register_widget_view('DatePickerView', DatePickerView);\n",
156 " manager.WidgetManager.register_widget_view('DatePickerView', DatePickerView);\n",
196 "});"
157 "});"
197 ]
158 ]
198 },
159 },
199 {
160 {
200 "cell_type": "markdown",
161 "cell_type": "markdown",
201 "metadata": {},
162 "metadata": {},
202 "source": [
163 "source": [
203 "## Test"
164 "## Test"
204 ]
165 ]
205 },
166 },
206 {
167 {
207 "cell_type": "markdown",
168 "cell_type": "markdown",
208 "metadata": {},
169 "metadata": {},
209 "source": [
170 "source": [
210 "To test what we have so far, create the widget, just like you would the builtin widgets:"
171 "To test what we have so far, create the widget, just like you would the builtin widgets:"
211 ]
172 ]
212 },
173 },
213 {
174 {
214 "cell_type": "code",
175 "cell_type": "code",
215 "execution_count": 5,
176 "execution_count": null,
216 "metadata": {
177 "metadata": {
217 "collapsed": false
178 "collapsed": false
218 },
179 },
219 "outputs": [],
180 "outputs": [],
220 "source": [
181 "source": [
221 "DateWidget()"
182 "DateWidget()"
222 ]
183 ]
223 },
184 },
224 {
185 {
225 "cell_type": "markdown",
186 "cell_type": "markdown",
226 "metadata": {},
187 "metadata": {},
227 "source": [
188 "source": [
228 "# Section 2 - Something useful"
189 "# Section 2 - Something useful"
229 ]
190 ]
230 },
191 },
231 {
192 {
232 "cell_type": "markdown",
193 "cell_type": "markdown",
233 "metadata": {},
194 "metadata": {},
234 "source": [
195 "source": [
235 "## Python"
196 "## Python"
236 ]
197 ]
237 },
198 },
238 {
199 {
239 "cell_type": "markdown",
200 "cell_type": "markdown",
240 "metadata": {},
201 "metadata": {},
241 "source": [
202 "source": [
242 "In the last section we created a simple widget that displayed *Hello World!*\n",
203 "In the last section we created a simple widget that displayed *Hello World!*\n",
243 "To make an actual date widget, we need to add a property that will be synced between the Python model and the JavaScript model.\n",
204 "To make an actual date widget, we need to add a property that will be synced between the Python model and the JavaScript model.\n",
244 "The new attribute must be a traitlet, so the widget machinery can handle it.\n",
205 "The new attribute must be a traitlet, so the widget machinery can handle it.\n",
245 "The traitlet must be constructed with a `sync=True` keyword argument, to tell the widget machinery knows to synchronize it with the front-end.\n",
206 "The traitlet must be constructed with a `sync=True` keyword argument, to tell the widget machinery knows to synchronize it with the front-end.\n",
246 "Adding this to the code from the last section:"
207 "Adding this to the code from the last section:"
247 ]
208 ]
248 },
209 },
249 {
210 {
250 "cell_type": "code",
211 "cell_type": "code",
251 "execution_count": 6,
212 "execution_count": null,
252 "metadata": {
213 "metadata": {
253 "collapsed": false
214 "collapsed": false
254 },
215 },
255 "outputs": [],
216 "outputs": [],
256 "source": [
217 "source": [
257 "class DateWidget(widgets.DOMWidget):\n",
218 "class DateWidget(widgets.DOMWidget):\n",
258 " _view_name = Unicode('DatePickerView', sync=True)\n",
219 " _view_name = Unicode('DatePickerView', sync=True)\n",
259 " value = Unicode(sync=True)"
220 " value = Unicode(sync=True)"
260 ]
221 ]
261 },
222 },
262 {
223 {
263 "cell_type": "markdown",
224 "cell_type": "markdown",
264 "metadata": {},
225 "metadata": {},
265 "source": [
226 "source": [
266 "## JavaScript"
227 "## JavaScript"
267 ]
228 ]
268 },
229 },
269 {
230 {
270 "cell_type": "markdown",
231 "cell_type": "markdown",
271 "metadata": {},
232 "metadata": {},
272 "source": [
233 "source": [
273 "In the JavaScript, there is no need to define counterparts to the traitlets.\n",
234 "In the JavaScript, there is no need to define counterparts to the traitlets.\n",
274 "When the JavaScript model is created for the first time,\n",
235 "When the JavaScript model is created for the first time,\n",
275 "it copies all of the traitlet `sync=True` attributes from the Python model.\n",
236 "it copies all of the traitlet `sync=True` attributes from the Python model.\n",
276 "We need to replace *Hello World!* with an actual HTML date picker widget."
237 "We need to replace *Hello World!* with an actual HTML date picker widget."
277 ]
238 ]
278 },
239 },
279 {
240 {
280 "cell_type": "code",
241 "cell_type": "code",
281 "execution_count": 7,
242 "execution_count": null,
282 "metadata": {
243 "metadata": {
283 "collapsed": false
244 "collapsed": false
284 },
245 },
285 "outputs": [
246 "outputs": [],
286 {
287 "data": {
288 "application/javascript": [
289 "\n",
290 "require([\"widgets/js/widget\"], function(WidgetManager){\n",
291 " \n",
292 " // Define the DatePickerView\n",
293 " var DatePickerView = IPython.DOMWidgetView.extend({\n",
294 " render: function(){\n",
295 " \n",
296 " // Create the date picker control.\n",
297 " this.$date = $('<input />')\n",
298 " .attr('type', 'date')\n",
299 " .appendTo(this.$el);\n",
300 " },\n",
301 " });\n",
302 " \n",
303 " // Register the DatePickerView with the widget manager.\n",
304 " WidgetManager.register_widget_view('DatePickerView', DatePickerView);\n",
305 "});"
306 ],
307 "text/plain": [
308 "<IPython.core.display.Javascript at 0x109491750>"
309 ]
310 },
311 "metadata": {},
312 "output_type": "display_data"
313 }
314 ],
315 "source": [
247 "source": [
316 "%%javascript\n",
248 "%%javascript\n",
317 "\n",
249 "\n",
318 "require([\"widgets/js/widget\"], function(WidgetManager){\n",
250 "require([\"widgets/js/widget\", \"widgets/js/manager\"], function(widget, manager){\n",
319 " \n",
251 " \n",
320 " // Define the DatePickerView\n",
252 " // Define the DatePickerView\n",
321 " var DatePickerView = IPython.DOMWidgetView.extend({\n",
253 " var DatePickerView = widget.DOMWidgetView.extend({\n",
322 " render: function(){\n",
254 " render: function(){\n",
323 " \n",
255 " \n",
324 " // Create the date picker control.\n",
256 " // Create the date picker control.\n",
325 " this.$date = $('<input />')\n",
257 " this.$date = $('<input />')\n",
326 " .attr('type', 'date')\n",
258 " .attr('type', 'date')\n",
327 " .appendTo(this.$el);\n",
259 " .appendTo(this.$el);\n",
328 " },\n",
260 " },\n",
329 " });\n",
261 " });\n",
330 " \n",
262 " \n",
331 " // Register the DatePickerView with the widget manager.\n",
263 " // Register the DatePickerView with the widget manager.\n",
332 " WidgetManager.register_widget_view('DatePickerView', DatePickerView);\n",
264 " manager.WidgetManager.register_widget_view('DatePickerView', DatePickerView);\n",
333 "});"
265 "});"
334 ]
266 ]
335 },
267 },
336 {
268 {
337 "cell_type": "markdown",
269 "cell_type": "markdown",
338 "metadata": {},
270 "metadata": {},
339 "source": [
271 "source": [
340 "In order to get the HTML date picker to update itself with the value set in the back-end, we need to implement an `update()` method."
272 "In order to get the HTML date picker to update itself with the value set in the back-end, we need to implement an `update()` method."
341 ]
273 ]
342 },
274 },
343 {
275 {
344 "cell_type": "code",
276 "cell_type": "code",
345 "execution_count": 8,
277 "execution_count": null,
346 "metadata": {
278 "metadata": {
347 "collapsed": false
279 "collapsed": false
348 },
280 },
349 "outputs": [
281 "outputs": [],
350 {
351 "data": {
352 "application/javascript": [
353 "\n",
354 "require([\"widgets/js/widget\"], function(WidgetManager){\n",
355 " \n",
356 " // Define the DatePickerView\n",
357 " var DatePickerView = IPython.DOMWidgetView.extend({\n",
358 " render: function(){\n",
359 " \n",
360 " // Create the date picker control.\n",
361 " this.$date = $('<input />')\n",
362 " .attr('type', 'date')\n",
363 " .appendTo(this.$el);\n",
364 " },\n",
365 " \n",
366 " update: function() {\n",
367 " \n",
368 " // Set the value of the date control and then call base.\n",
369 " this.$date.val(this.model.get('value')); // ISO format \"YYYY-MM-DDTHH:mm:ss.sssZ\" is required\n",
370 " return DatePickerView.__super__.update.apply(this);\n",
371 " },\n",
372 " });\n",
373 " \n",
374 " // Register the DatePickerView with the widget manager.\n",
375 " WidgetManager.register_widget_view('DatePickerView', DatePickerView);\n",
376 "});"
377 ],
378 "text/plain": [
379 "<IPython.core.display.Javascript at 0x109491750>"
380 ]
381 },
382 "metadata": {},
383 "output_type": "display_data"
384 }
385 ],
386 "source": [
282 "source": [
387 "%%javascript\n",
283 "%%javascript\n",
388 "\n",
284 "\n",
389 "require([\"widgets/js/widget\"], function(WidgetManager){\n",
285 "require([\"widgets/js/widget\", \"widgets/js/manager\"], function(widget, manager){\n",
390 " \n",
286 " \n",
391 " // Define the DatePickerView\n",
287 " // Define the DatePickerView\n",
392 " var DatePickerView = IPython.DOMWidgetView.extend({\n",
288 " var DatePickerView = widget.DOMWidgetView.extend({\n",
393 " render: function(){\n",
289 " render: function(){\n",
394 " \n",
290 " \n",
395 " // Create the date picker control.\n",
291 " // Create the date picker control.\n",
396 " this.$date = $('<input />')\n",
292 " this.$date = $('<input />')\n",
397 " .attr('type', 'date')\n",
293 " .attr('type', 'date')\n",
398 " .appendTo(this.$el);\n",
294 " .appendTo(this.$el);\n",
399 " },\n",
295 " },\n",
400 " \n",
296 " \n",
401 " update: function() {\n",
297 " update: function() {\n",
402 " \n",
298 " \n",
403 " // Set the value of the date control and then call base.\n",
299 " // Set the value of the date control and then call base.\n",
404 " this.$date.val(this.model.get('value')); // ISO format \"YYYY-MM-DDTHH:mm:ss.sssZ\" is required\n",
300 " this.$date.val(this.model.get('value')); // ISO format \"YYYY-MM-DDTHH:mm:ss.sssZ\" is required\n",
405 " return DatePickerView.__super__.update.apply(this);\n",
301 " return DatePickerView.__super__.update.apply(this);\n",
406 " },\n",
302 " },\n",
407 " });\n",
303 " });\n",
408 " \n",
304 " \n",
409 " // Register the DatePickerView with the widget manager.\n",
305 " // Register the DatePickerView with the widget manager.\n",
410 " WidgetManager.register_widget_view('DatePickerView', DatePickerView);\n",
306 " manager.WidgetManager.register_widget_view('DatePickerView', DatePickerView);\n",
411 "});"
307 "});"
412 ]
308 ]
413 },
309 },
414 {
310 {
415 "cell_type": "markdown",
311 "cell_type": "markdown",
416 "metadata": {},
312 "metadata": {},
417 "source": [
313 "source": [
418 "To get the changed value from the frontend to publish itself to the backend,\n",
314 "To get the changed value from the frontend to publish itself to the backend,\n",
419 "we need to listen to the change event triggered by the HTM date control and set the value in the model.\n",
315 "we need to listen to the change event triggered by the HTM date control and set the value in the model.\n",
420 "After the date change event fires and the new value is set in the model,\n",
316 "After the date change event fires and the new value is set in the model,\n",
421 "it is very important that we call `this.touch()` to let the widget machinery know which view changed the model.\n",
317 "it is very important that we call `this.touch()` to let the widget machinery know which view changed the model.\n",
422 "This is important because the widget machinery needs to know which cell to route the message callbacks to.\n",
318 "This is important because the widget machinery needs to know which cell to route the message callbacks to.\n",
423 "\n",
319 "\n",
424 "**Final JavaScript code below:**"
320 "**Final JavaScript code below:**"
425 ]
321 ]
426 },
322 },
427 {
323 {
428 "cell_type": "code",
324 "cell_type": "code",
429 "execution_count": 9,
325 "execution_count": null,
430 "metadata": {
326 "metadata": {
431 "collapsed": false
327 "collapsed": false
432 },
328 },
433 "outputs": [
329 "outputs": [],
434 {
435 "data": {
436 "application/javascript": [
437 "\n",
438 "\n",
439 "require([\"widgets/js/widget\"], function(WidgetManager){\n",
440 " \n",
441 " // Define the DatePickerView\n",
442 " var DatePickerView = IPython.DOMWidgetView.extend({\n",
443 " render: function(){\n",
444 " \n",
445 " // Create the date picker control.\n",
446 " this.$date = $('<input />')\n",
447 " .attr('type', 'date')\n",
448 " .appendTo(this.$el);\n",
449 " },\n",
450 " \n",
451 " update: function() {\n",
452 " \n",
453 " // Set the value of the date control and then call base.\n",
454 " this.$date.val(this.model.get('value')); // ISO format \"YYYY-MM-DDTHH:mm:ss.sssZ\" is required\n",
455 " return DatePickerView.__super__.update.apply(this);\n",
456 " },\n",
457 " \n",
458 " // Tell Backbone to listen to the change event of input controls (which the HTML date picker is)\n",
459 " events: {\"change\": \"handle_date_change\"},\n",
460 " \n",
461 " // Callback for when the date is changed.\n",
462 " handle_date_change: function(event) {\n",
463 " this.model.set('value', this.$date.val());\n",
464 " this.touch();\n",
465 " },\n",
466 " });\n",
467 " \n",
468 " // Register the DatePickerView with the widget manager.\n",
469 " WidgetManager.register_widget_view('DatePickerView', DatePickerView);\n",
470 "});"
471 ],
472 "text/plain": [
473 "<IPython.core.display.Javascript at 0x109491b10>"
474 ]
475 },
476 "metadata": {},
477 "output_type": "display_data"
478 }
479 ],
480 "source": [
330 "source": [
481 "%%javascript\n",
331 "%%javascript\n",
482 "\n",
332 "\n",
483 "\n",
333 "\n",
484 "require([\"widgets/js/widget\"], function(WidgetManager){\n",
334 "require([\"widgets/js/widget\", \"widgets/js/manager\"], function(widget, manager){\n",
485 " \n",
335 " \n",
486 " // Define the DatePickerView\n",
336 " // Define the DatePickerView\n",
487 " var DatePickerView = IPython.DOMWidgetView.extend({\n",
337 " var DatePickerView = widget.DOMWidgetView.extend({\n",
488 " render: function(){\n",
338 " render: function(){\n",
489 " \n",
339 " \n",
490 " // Create the date picker control.\n",
340 " // Create the date picker control.\n",
491 " this.$date = $('<input />')\n",
341 " this.$date = $('<input />')\n",
492 " .attr('type', 'date')\n",
342 " .attr('type', 'date')\n",
493 " .appendTo(this.$el);\n",
343 " .appendTo(this.$el);\n",
494 " },\n",
344 " },\n",
495 " \n",
345 " \n",
496 " update: function() {\n",
346 " update: function() {\n",
497 " \n",
347 " \n",
498 " // Set the value of the date control and then call base.\n",
348 " // Set the value of the date control and then call base.\n",
499 " this.$date.val(this.model.get('value')); // ISO format \"YYYY-MM-DDTHH:mm:ss.sssZ\" is required\n",
349 " this.$date.val(this.model.get('value')); // ISO format \"YYYY-MM-DDTHH:mm:ss.sssZ\" is required\n",
500 " return DatePickerView.__super__.update.apply(this);\n",
350 " return DatePickerView.__super__.update.apply(this);\n",
501 " },\n",
351 " },\n",
502 " \n",
352 " \n",
503 " // Tell Backbone to listen to the change event of input controls (which the HTML date picker is)\n",
353 " // Tell Backbone to listen to the change event of input controls (which the HTML date picker is)\n",
504 " events: {\"change\": \"handle_date_change\"},\n",
354 " events: {\"change\": \"handle_date_change\"},\n",
505 " \n",
355 " \n",
506 " // Callback for when the date is changed.\n",
356 " // Callback for when the date is changed.\n",
507 " handle_date_change: function(event) {\n",
357 " handle_date_change: function(event) {\n",
508 " this.model.set('value', this.$date.val());\n",
358 " this.model.set('value', this.$date.val());\n",
509 " this.touch();\n",
359 " this.touch();\n",
510 " },\n",
360 " },\n",
511 " });\n",
361 " });\n",
512 " \n",
362 " \n",
513 " // Register the DatePickerView with the widget manager.\n",
363 " // Register the DatePickerView with the widget manager.\n",
514 " WidgetManager.register_widget_view('DatePickerView', DatePickerView);\n",
364 " manager.WidgetManager.register_widget_view('DatePickerView', DatePickerView);\n",
515 "});"
365 "});"
516 ]
366 ]
517 },
367 },
518 {
368 {
519 "cell_type": "markdown",
369 "cell_type": "markdown",
520 "metadata": {},
370 "metadata": {},
521 "source": [
371 "source": [
522 "## Test"
372 "## Test"
523 ]
373 ]
524 },
374 },
525 {
375 {
526 "cell_type": "markdown",
376 "cell_type": "markdown",
527 "metadata": {},
377 "metadata": {},
528 "source": [
378 "source": [
529 "To test, create the widget the same way that the other widgets are created."
379 "To test, create the widget the same way that the other widgets are created."
530 ]
380 ]
531 },
381 },
532 {
382 {
533 "cell_type": "code",
383 "cell_type": "code",
534 "execution_count": 10,
384 "execution_count": null,
535 "metadata": {
385 "metadata": {
536 "collapsed": false
386 "collapsed": false
537 },
387 },
538 "outputs": [],
388 "outputs": [],
539 "source": [
389 "source": [
540 "my_widget = DateWidget()\n",
390 "my_widget = DateWidget()\n",
541 "display(my_widget)"
391 "display(my_widget)"
542 ]
392 ]
543 },
393 },
544 {
394 {
545 "cell_type": "markdown",
395 "cell_type": "markdown",
546 "metadata": {},
396 "metadata": {},
547 "source": [
397 "source": [
548 "Display the widget again to make sure that both views remain in sync."
398 "Display the widget again to make sure that both views remain in sync."
549 ]
399 ]
550 },
400 },
551 {
401 {
552 "cell_type": "code",
402 "cell_type": "code",
553 "execution_count": 11,
403 "execution_count": null,
554 "metadata": {
404 "metadata": {
555 "collapsed": false
405 "collapsed": false
556 },
406 },
557 "outputs": [],
407 "outputs": [],
558 "source": [
408 "source": [
559 "my_widget"
409 "my_widget"
560 ]
410 ]
561 },
411 },
562 {
412 {
563 "cell_type": "markdown",
413 "cell_type": "markdown",
564 "metadata": {},
414 "metadata": {},
565 "source": [
415 "source": [
566 "Read the date from Python"
416 "Read the date from Python"
567 ]
417 ]
568 },
418 },
569 {
419 {
570 "cell_type": "code",
420 "cell_type": "code",
571 "execution_count": 12,
421 "execution_count": null,
572 "metadata": {
422 "metadata": {
573 "collapsed": false
423 "collapsed": false
574 },
424 },
575 "outputs": [
425 "outputs": [],
576 {
577 "data": {
578 "text/plain": [
579 "u''"
580 ]
581 },
582 "execution_count": 12,
583 "metadata": {},
584 "output_type": "execute_result"
585 }
586 ],
587 "source": [
426 "source": [
588 "my_widget.value"
427 "my_widget.value"
589 ]
428 ]
590 },
429 },
591 {
430 {
592 "cell_type": "markdown",
431 "cell_type": "markdown",
593 "metadata": {},
432 "metadata": {},
594 "source": [
433 "source": [
595 "Set the date from Python"
434 "Set the date from Python"
596 ]
435 ]
597 },
436 },
598 {
437 {
599 "cell_type": "code",
438 "cell_type": "code",
600 "execution_count": 13,
439 "execution_count": null,
601 "metadata": {
440 "metadata": {
602 "collapsed": false
441 "collapsed": false
603 },
442 },
604 "outputs": [],
443 "outputs": [],
605 "source": [
444 "source": [
606 "my_widget.value = \"1998-12-01\" # December 1st, 1998"
445 "my_widget.value = \"1998-12-01\" # December 1st, 1998"
607 ]
446 ]
608 },
447 },
609 {
448 {
610 "cell_type": "markdown",
449 "cell_type": "markdown",
611 "metadata": {},
450 "metadata": {},
612 "source": [
451 "source": [
613 "# Section 3 - Extra credit"
452 "# Section 3 - Extra credit"
614 ]
453 ]
615 },
454 },
616 {
455 {
617 "cell_type": "markdown",
456 "cell_type": "markdown",
618 "metadata": {},
457 "metadata": {},
619 "source": [
458 "source": [
620 "The 3rd party `dateutil` library is required to continue. https://pypi.python.org/pypi/python-dateutil"
459 "The 3rd party `dateutil` library is required to continue. https://pypi.python.org/pypi/python-dateutil"
621 ]
460 ]
622 },
461 },
623 {
462 {
624 "cell_type": "code",
463 "cell_type": "code",
625 "execution_count": 14,
464 "execution_count": null,
626 "metadata": {
465 "metadata": {
627 "collapsed": false
466 "collapsed": false
628 },
467 },
629 "outputs": [],
468 "outputs": [],
630 "source": [
469 "source": [
631 "# Import the dateutil library to parse date strings.\n",
470 "# Import the dateutil library to parse date strings.\n",
632 "from dateutil import parser"
471 "from dateutil import parser"
633 ]
472 ]
634 },
473 },
635 {
474 {
636 "cell_type": "markdown",
475 "cell_type": "markdown",
637 "metadata": {},
476 "metadata": {},
638 "source": [
477 "source": [
639 "In the last section we created a fully working date picker widget.\n",
478 "In the last section we created a fully working date picker widget.\n",
640 "Now we will add custom validation and support for labels.\n",
479 "Now we will add custom validation and support for labels.\n",
641 "So far, only the ISO date format \"YYYY-MM-DD\" is supported.\n",
480 "So far, only the ISO date format \"YYYY-MM-DD\" is supported.\n",
642 "Now, we will add support for all of the date formats recognized by the Python dateutil library."
481 "Now, we will add support for all of the date formats recognized by the Python dateutil library."
643 ]
482 ]
644 },
483 },
645 {
484 {
646 "cell_type": "markdown",
485 "cell_type": "markdown",
647 "metadata": {},
486 "metadata": {},
648 "source": [
487 "source": [
649 "## Python"
488 "## Python"
650 ]
489 ]
651 },
490 },
652 {
491 {
653 "cell_type": "markdown",
492 "cell_type": "markdown",
654 "metadata": {},
493 "metadata": {},
655 "source": [
494 "source": [
656 "The standard property name used for widget labels is `description`.\n",
495 "The standard property name used for widget labels is `description`.\n",
657 "In the code block below, `description` has been added to the Python widget."
496 "In the code block below, `description` has been added to the Python widget."
658 ]
497 ]
659 },
498 },
660 {
499 {
661 "cell_type": "code",
500 "cell_type": "code",
662 "execution_count": 15,
501 "execution_count": null,
663 "metadata": {
502 "metadata": {
664 "collapsed": false
503 "collapsed": false
665 },
504 },
666 "outputs": [],
505 "outputs": [],
667 "source": [
506 "source": [
668 "class DateWidget(widgets.DOMWidget):\n",
507 "class DateWidget(widgets.DOMWidget):\n",
669 " _view_name = Unicode('DatePickerView', sync=True)\n",
508 " _view_name = Unicode('DatePickerView', sync=True)\n",
670 " value = Unicode(sync=True)\n",
509 " value = Unicode(sync=True)\n",
671 " description = Unicode(sync=True)"
510 " description = Unicode(sync=True)"
672 ]
511 ]
673 },
512 },
674 {
513 {
675 "cell_type": "markdown",
514 "cell_type": "markdown",
676 "metadata": {},
515 "metadata": {},
677 "source": [
516 "source": [
678 "The traitlet machinery searches the class that the trait is defined in for methods with \"`_changed`\" suffixed onto their names. Any method with the format \"`_X_changed`\" will be called when \"`X`\" is modified.\n",
517 "The traitlet machinery searches the class that the trait is defined in for methods with \"`_changed`\" suffixed onto their names. Any method with the format \"`_X_changed`\" will be called when \"`X`\" is modified.\n",
679 "We can take advantage of this to perform validation and parsing of different date string formats.\n",
518 "We can take advantage of this to perform validation and parsing of different date string formats.\n",
680 "Below, a method that listens to value has been added to the DateWidget."
519 "Below, a method that listens to value has been added to the DateWidget."
681 ]
520 ]
682 },
521 },
683 {
522 {
684 "cell_type": "code",
523 "cell_type": "code",
685 "execution_count": 16,
524 "execution_count": null,
686 "metadata": {
525 "metadata": {
687 "collapsed": false
526 "collapsed": false
688 },
527 },
689 "outputs": [],
528 "outputs": [],
690 "source": [
529 "source": [
691 "class DateWidget(widgets.DOMWidget):\n",
530 "class DateWidget(widgets.DOMWidget):\n",
692 " _view_name = Unicode('DatePickerView', sync=True)\n",
531 " _view_name = Unicode('DatePickerView', sync=True)\n",
693 " value = Unicode(sync=True)\n",
532 " value = Unicode(sync=True)\n",
694 " description = Unicode(sync=True)\n",
533 " description = Unicode(sync=True)\n",
695 "\n",
534 "\n",
696 " # This function automatically gets called by the traitlet machinery when\n",
535 " # This function automatically gets called by the traitlet machinery when\n",
697 " # value is modified because of this function's name.\n",
536 " # value is modified because of this function's name.\n",
698 " def _value_changed(self, name, old_value, new_value):\n",
537 " def _value_changed(self, name, old_value, new_value):\n",
699 " pass"
538 " pass"
700 ]
539 ]
701 },
540 },
702 {
541 {
703 "cell_type": "markdown",
542 "cell_type": "markdown",
704 "metadata": {},
543 "metadata": {},
705 "source": [
544 "source": [
706 "Now the function parses the date string,\n",
545 "Now the function parses the date string,\n",
707 "and only sets the value in the correct format."
546 "and only sets the value in the correct format."
708 ]
547 ]
709 },
548 },
710 {
549 {
711 "cell_type": "code",
550 "cell_type": "code",
712 "execution_count": 17,
551 "execution_count": null,
713 "metadata": {
552 "metadata": {
714 "collapsed": false
553 "collapsed": false
715 },
554 },
716 "outputs": [],
555 "outputs": [],
717 "source": [
556 "source": [
718 "class DateWidget(widgets.DOMWidget):\n",
557 "class DateWidget(widgets.DOMWidget):\n",
719 " _view_name = Unicode('DatePickerView', sync=True)\n",
558 " _view_name = Unicode('DatePickerView', sync=True)\n",
720 " value = Unicode(sync=True)\n",
559 " value = Unicode(sync=True)\n",
721 " description = Unicode(sync=True)\n",
560 " description = Unicode(sync=True)\n",
722 " \n",
561 " \n",
723 " # This function automatically gets called by the traitlet machinery when\n",
562 " # This function automatically gets called by the traitlet machinery when\n",
724 " # value is modified because of this function's name.\n",
563 " # value is modified because of this function's name.\n",
725 " def _value_changed(self, name, old_value, new_value):\n",
564 " def _value_changed(self, name, old_value, new_value):\n",
726 " \n",
565 " \n",
727 " # Parse the date time value.\n",
566 " # Parse the date time value.\n",
728 " try:\n",
567 " try:\n",
729 " parsed_date = parser.parse(new_value)\n",
568 " parsed_date = parser.parse(new_value)\n",
730 " parsed_date_string = parsed_date.strftime(\"%Y-%m-%d\")\n",
569 " parsed_date_string = parsed_date.strftime(\"%Y-%m-%d\")\n",
731 " except:\n",
570 " except:\n",
732 " parsed_date_string = ''\n",
571 " parsed_date_string = ''\n",
733 " \n",
572 " \n",
734 " # Set the parsed date string if the current date string is different.\n",
573 " # Set the parsed date string if the current date string is different.\n",
735 " if self.value != parsed_date_string:\n",
574 " if self.value != parsed_date_string:\n",
736 " self.value = parsed_date_string"
575 " self.value = parsed_date_string"
737 ]
576 ]
738 },
577 },
739 {
578 {
740 "cell_type": "markdown",
579 "cell_type": "markdown",
741 "metadata": {},
580 "metadata": {},
742 "source": [
581 "source": [
743 "Finally, a `CallbackDispatcher` is added so the user can perform custom validation.\n",
582 "Finally, a `CallbackDispatcher` is added so the user can perform custom validation.\n",
744 "If any one of the callbacks registered with the dispatcher returns False,\n",
583 "If any one of the callbacks registered with the dispatcher returns False,\n",
745 "the new date is not set.\n",
584 "the new date is not set.\n",
746 "\n",
585 "\n",
747 "**Final Python code below:**"
586 "**Final Python code below:**"
748 ]
587 ]
749 },
588 },
750 {
589 {
751 "cell_type": "code",
590 "cell_type": "code",
752 "execution_count": 18,
591 "execution_count": null,
753 "metadata": {
592 "metadata": {
754 "collapsed": false
593 "collapsed": false
755 },
594 },
756 "outputs": [],
595 "outputs": [],
757 "source": [
596 "source": [
758 "class DateWidget(widgets.DOMWidget):\n",
597 "class DateWidget(widgets.DOMWidget):\n",
759 " _view_name = Unicode('DatePickerView', sync=True)\n",
598 " _view_name = Unicode('DatePickerView', sync=True)\n",
760 " value = Unicode(sync=True)\n",
599 " value = Unicode(sync=True)\n",
761 " description = Unicode(sync=True)\n",
600 " description = Unicode(sync=True)\n",
762 " \n",
601 " \n",
763 " def __init__(self, **kwargs):\n",
602 " def __init__(self, **kwargs):\n",
764 " super(DateWidget, self).__init__(**kwargs)\n",
603 " super(DateWidget, self).__init__(**kwargs)\n",
765 " \n",
604 " \n",
766 " self.validate = widgets.CallbackDispatcher()\n",
605 " self.validate = widgets.CallbackDispatcher()\n",
767 " \n",
606 " \n",
768 " # This function automatically gets called by the traitlet machinery when\n",
607 " # This function automatically gets called by the traitlet machinery when\n",
769 " # value is modified because of this function's name.\n",
608 " # value is modified because of this function's name.\n",
770 " def _value_changed(self, name, old_value, new_value):\n",
609 " def _value_changed(self, name, old_value, new_value):\n",
771 " \n",
610 " \n",
772 " # Parse the date time value.\n",
611 " # Parse the date time value.\n",
773 " try:\n",
612 " try:\n",
774 " parsed_date = parser.parse(new_value)\n",
613 " parsed_date = parser.parse(new_value)\n",
775 " parsed_date_string = parsed_date.strftime(\"%Y-%m-%d\")\n",
614 " parsed_date_string = parsed_date.strftime(\"%Y-%m-%d\")\n",
776 " except:\n",
615 " except:\n",
777 " parsed_date_string = ''\n",
616 " parsed_date_string = ''\n",
778 " \n",
617 " \n",
779 " # Set the parsed date string if the current date string is different.\n",
618 " # Set the parsed date string if the current date string is different.\n",
780 " if old_value != new_value:\n",
619 " if old_value != new_value:\n",
781 " valid = self.validate(parsed_date)\n",
620 " valid = self.validate(parsed_date)\n",
782 " if valid in (None, True):\n",
621 " if valid in (None, True):\n",
783 " self.value = parsed_date_string\n",
622 " self.value = parsed_date_string\n",
784 " else:\n",
623 " else:\n",
785 " self.value = old_value\n",
624 " self.value = old_value\n",
786 " self.send_state() # The traitlet event won't fire since the value isn't changing.\n",
625 " self.send_state() # The traitlet event won't fire since the value isn't changing.\n",
787 " # We need to force the back-end to send the front-end the state\n",
626 " # We need to force the back-end to send the front-end the state\n",
788 " # to make sure that the date control date doesn't change."
627 " # to make sure that the date control date doesn't change."
789 ]
628 ]
790 },
629 },
791 {
630 {
792 "cell_type": "markdown",
631 "cell_type": "markdown",
793 "metadata": {},
632 "metadata": {},
794 "source": [
633 "source": [
795 "## JavaScript"
634 "## JavaScript"
796 ]
635 ]
797 },
636 },
798 {
637 {
799 "cell_type": "markdown",
638 "cell_type": "markdown",
800 "metadata": {},
639 "metadata": {},
801 "source": [
640 "source": [
802 "Using the Javascript code from the last section,\n",
641 "Using the Javascript code from the last section,\n",
803 "we add a label to the date time object.\n",
642 "we add a label to the date time object.\n",
804 "The label is a div with the `widget-hlabel` class applied to it.\n",
643 "The label is a div with the `widget-hlabel` class applied to it.\n",
805 "`widget-hlabel` is a class provided by the widget framework that applies special styling to a div to make it look like the rest of the horizontal labels used with the built-in widgets.\n",
644 "`widget-hlabel` is a class provided by the widget framework that applies special styling to a div to make it look like the rest of the horizontal labels used with the built-in widgets.\n",
806 "Similar to the `widget-hlabel` class is the `widget-hbox-single` class.\n",
645 "Similar to the `widget-hlabel` class is the `widget-hbox-single` class.\n",
807 "The `widget-hbox-single` class applies special styling to widget containers that store a single line horizontal widget.\n",
646 "The `widget-hbox-single` class applies special styling to widget containers that store a single line horizontal widget.\n",
808 "\n",
647 "\n",
809 "We hide the label if the description value is blank."
648 "We hide the label if the description value is blank."
810 ]
649 ]
811 },
650 },
812 {
651 {
813 "cell_type": "code",
652 "cell_type": "code",
814 "execution_count": 19,
653 "execution_count": null,
815 "metadata": {
654 "metadata": {
816 "collapsed": false
655 "collapsed": false
817 },
656 },
818 "outputs": [
657 "outputs": [],
819 {
820 "data": {
821 "application/javascript": [
822 "\n",
823 "require([\"widgets/js/widget\"], function(WidgetManager){\n",
824 " \n",
825 " // Define the DatePickerView\n",
826 " var DatePickerView = IPython.DOMWidgetView.extend({\n",
827 " render: function(){\n",
828 " this.$el.addClass('widget-hbox-single'); /* Apply this class to the widget container to make\n",
829 " it fit with the other built in widgets.*/\n",
830 " // Create a label.\n",
831 " this.$label = $('<div />')\n",
832 " .addClass('widget-hlabel')\n",
833 " .appendTo(this.$el)\n",
834 " .hide(); // Hide the label by default.\n",
835 " \n",
836 " // Create the date picker control.\n",
837 " this.$date = $('<input />')\n",
838 " .attr('type', 'date')\n",
839 " .appendTo(this.$el);\n",
840 " },\n",
841 " \n",
842 " update: function() {\n",
843 " \n",
844 " // Set the value of the date control and then call base.\n",
845 " this.$date.val(this.model.get('value')); // ISO format \"YYYY-MM-DDTHH:mm:ss.sssZ\" is required\n",
846 " \n",
847 " // Hide or show the label depending on the existance of a description.\n",
848 " var description = this.model.get('description');\n",
849 " if (description == undefined || description == '') {\n",
850 " this.$label.hide();\n",
851 " } else {\n",
852 " this.$label.show();\n",
853 " this.$label.text(description);\n",
854 " }\n",
855 " \n",
856 " return DatePickerView.__super__.update.apply(this);\n",
857 " },\n",
858 " \n",
859 " // Tell Backbone to listen to the change event of input controls (which the HTML date picker is)\n",
860 " events: {\"change\": \"handle_date_change\"},\n",
861 " \n",
862 " // Callback for when the date is changed.\n",
863 " handle_date_change: function(event) {\n",
864 " this.model.set('value', this.$date.val());\n",
865 " this.touch();\n",
866 " },\n",
867 " });\n",
868 " \n",
869 " // Register the DatePickerView with the widget manager.\n",
870 " WidgetManager.register_widget_view('DatePickerView', DatePickerView);\n",
871 "});"
872 ],
873 "text/plain": [
874 "<IPython.core.display.Javascript at 0x1094eef90>"
875 ]
876 },
877 "metadata": {},
878 "output_type": "display_data"
879 }
880 ],
881 "source": [
658 "source": [
882 "%%javascript\n",
659 "%%javascript\n",
883 "\n",
660 "\n",
884 "require([\"widgets/js/widget\"], function(WidgetManager){\n",
661 "require([\"widgets/js/widget\", \"widgets/js/manager\"], function(widget, manager){\n",
885 " \n",
662 " \n",
886 " // Define the DatePickerView\n",
663 " // Define the DatePickerView\n",
887 " var DatePickerView = IPython.DOMWidgetView.extend({\n",
664 " var DatePickerView = widget.DOMWidgetView.extend({\n",
888 " render: function(){\n",
665 " render: function(){\n",
889 " this.$el.addClass('widget-hbox-single'); /* Apply this class to the widget container to make\n",
666 " this.$el.addClass('widget-hbox-single'); /* Apply this class to the widget container to make\n",
890 " it fit with the other built in widgets.*/\n",
667 " it fit with the other built in widgets.*/\n",
891 " // Create a label.\n",
668 " // Create a label.\n",
892 " this.$label = $('<div />')\n",
669 " this.$label = $('<div />')\n",
893 " .addClass('widget-hlabel')\n",
670 " .addClass('widget-hlabel')\n",
894 " .appendTo(this.$el)\n",
671 " .appendTo(this.$el)\n",
895 " .hide(); // Hide the label by default.\n",
672 " .hide(); // Hide the label by default.\n",
896 " \n",
673 " \n",
897 " // Create the date picker control.\n",
674 " // Create the date picker control.\n",
898 " this.$date = $('<input />')\n",
675 " this.$date = $('<input />')\n",
899 " .attr('type', 'date')\n",
676 " .attr('type', 'date')\n",
900 " .appendTo(this.$el);\n",
677 " .appendTo(this.$el);\n",
901 " },\n",
678 " },\n",
902 " \n",
679 " \n",
903 " update: function() {\n",
680 " update: function() {\n",
904 " \n",
681 " \n",
905 " // Set the value of the date control and then call base.\n",
682 " // Set the value of the date control and then call base.\n",
906 " this.$date.val(this.model.get('value')); // ISO format \"YYYY-MM-DDTHH:mm:ss.sssZ\" is required\n",
683 " this.$date.val(this.model.get('value')); // ISO format \"YYYY-MM-DDTHH:mm:ss.sssZ\" is required\n",
907 " \n",
684 " \n",
908 " // Hide or show the label depending on the existance of a description.\n",
685 " // Hide or show the label depending on the existance of a description.\n",
909 " var description = this.model.get('description');\n",
686 " var description = this.model.get('description');\n",
910 " if (description == undefined || description == '') {\n",
687 " if (description == undefined || description == '') {\n",
911 " this.$label.hide();\n",
688 " this.$label.hide();\n",
912 " } else {\n",
689 " } else {\n",
913 " this.$label.show();\n",
690 " this.$label.show();\n",
914 " this.$label.text(description);\n",
691 " this.$label.text(description);\n",
915 " }\n",
692 " }\n",
916 " \n",
693 " \n",
917 " return DatePickerView.__super__.update.apply(this);\n",
694 " return DatePickerView.__super__.update.apply(this);\n",
918 " },\n",
695 " },\n",
919 " \n",
696 " \n",
920 " // Tell Backbone to listen to the change event of input controls (which the HTML date picker is)\n",
697 " // Tell Backbone to listen to the change event of input controls (which the HTML date picker is)\n",
921 " events: {\"change\": \"handle_date_change\"},\n",
698 " events: {\"change\": \"handle_date_change\"},\n",
922 " \n",
699 " \n",
923 " // Callback for when the date is changed.\n",
700 " // Callback for when the date is changed.\n",
924 " handle_date_change: function(event) {\n",
701 " handle_date_change: function(event) {\n",
925 " this.model.set('value', this.$date.val());\n",
702 " this.model.set('value', this.$date.val());\n",
926 " this.touch();\n",
703 " this.touch();\n",
927 " },\n",
704 " },\n",
928 " });\n",
705 " });\n",
929 " \n",
706 " \n",
930 " // Register the DatePickerView with the widget manager.\n",
707 " // Register the DatePickerView with the widget manager.\n",
931 " WidgetManager.register_widget_view('DatePickerView', DatePickerView);\n",
708 " manager.WidgetManager.register_widget_view('DatePickerView', DatePickerView);\n",
932 "});"
709 "});"
933 ]
710 ]
934 },
711 },
935 {
712 {
936 "cell_type": "markdown",
713 "cell_type": "markdown",
937 "metadata": {},
714 "metadata": {},
938 "source": [
715 "source": [
939 "## Test"
716 "## Test"
940 ]
717 ]
941 },
718 },
942 {
719 {
943 "cell_type": "markdown",
720 "cell_type": "markdown",
944 "metadata": {},
721 "metadata": {},
945 "source": [
722 "source": [
946 "To test the drawing of the label we create the widget like normal but supply the additional description property a value."
723 "To test the drawing of the label we create the widget like normal but supply the additional description property a value."
947 ]
724 ]
948 },
725 },
949 {
726 {
950 "cell_type": "code",
727 "cell_type": "code",
951 "execution_count": 20,
728 "execution_count": null,
952 "metadata": {
729 "metadata": {
953 "collapsed": false
730 "collapsed": false
954 },
731 },
955 "outputs": [],
732 "outputs": [],
956 "source": [
733 "source": [
957 "# Add some additional widgets for aesthetic purpose\n",
734 "# Add some additional widgets for aesthetic purpose\n",
958 "display(widgets.TextWidget(description=\"First:\"))\n",
735 "display(widgets.Text(description=\"First:\"))\n",
959 "display(widgets.TextWidget(description=\"Last:\"))\n",
736 "display(widgets.Text(description=\"Last:\"))\n",
960 "\n",
737 "\n",
961 "my_widget = DateWidget()\n",
738 "my_widget = DateWidget()\n",
962 "display(my_widget)\n",
739 "display(my_widget)\n",
963 "my_widget.description=\"DOB:\""
740 "my_widget.description=\"DOB:\""
964 ]
741 ]
965 },
742 },
966 {
743 {
967 "cell_type": "markdown",
744 "cell_type": "markdown",
968 "metadata": {},
745 "metadata": {},
969 "source": [
746 "source": [
970 "Now we will try to create a widget that only accepts dates in the year 2014. We render the widget without a description to verify that it can still render without a label."
747 "Now we will try to create a widget that only accepts dates in the year 2014. We render the widget without a description to verify that it can still render without a label."
971 ]
748 ]
972 },
749 },
973 {
750 {
974 "cell_type": "code",
751 "cell_type": "code",
975 "execution_count": 21,
752 "execution_count": null,
976 "metadata": {
753 "metadata": {
977 "collapsed": false
754 "collapsed": false
978 },
755 },
979 "outputs": [],
756 "outputs": [],
980 "source": [
757 "source": [
981 "my_widget = DateWidget()\n",
758 "my_widget = DateWidget()\n",
982 "display(my_widget)\n",
759 "display(my_widget)\n",
983 "\n",
760 "\n",
984 "def require_2014(date):\n",
761 "def require_2014(date):\n",
985 " return not date is None and date.year == 2014\n",
762 " return not date is None and date.year == 2014\n",
986 "my_widget.validate.register_callback(require_2014)"
763 "my_widget.validate.register_callback(require_2014)"
987 ]
764 ]
988 },
765 },
989 {
766 {
990 "cell_type": "code",
767 "cell_type": "code",
991 "execution_count": 22,
768 "execution_count": null,
992 "metadata": {
769 "metadata": {
993 "collapsed": false
770 "collapsed": false
994 },
771 },
995 "outputs": [],
772 "outputs": [],
996 "source": [
773 "source": [
997 "# Try setting a valid date\n",
774 "# Try setting a valid date\n",
998 "my_widget.value = \"December 2, 2014\""
775 "my_widget.value = \"December 2, 2014\""
999 ]
776 ]
1000 },
777 },
1001 {
778 {
1002 "cell_type": "code",
779 "cell_type": "code",
1003 "execution_count": 23,
780 "execution_count": null,
1004 "metadata": {
781 "metadata": {
1005 "collapsed": false
782 "collapsed": false
1006 },
783 },
1007 "outputs": [],
784 "outputs": [],
1008 "source": [
785 "source": [
1009 "# Try setting an invalid date\n",
786 "# Try setting an invalid date\n",
1010 "my_widget.value = \"June 12, 1999\""
787 "my_widget.value = \"June 12, 1999\""
1011 ]
788 ]
1012 },
789 },
1013 {
790 {
1014 "cell_type": "code",
791 "cell_type": "code",
1015 "execution_count": 24,
792 "execution_count": null,
1016 "metadata": {
793 "metadata": {
1017 "collapsed": false
794 "collapsed": false
1018 },
795 },
1019 "outputs": [
796 "outputs": [],
1020 {
1021 "data": {
1022 "text/plain": [
1023 "u'2014-12-02'"
1024 ]
1025 },
1026 "execution_count": 24,
1027 "metadata": {},
1028 "output_type": "execute_result"
1029 }
1030 ],
1031 "source": [
797 "source": [
1032 "my_widget.value"
798 "my_widget.value"
1033 ]
799 ]
800 },
801 {
802 "cell_type": "code",
803 "execution_count": null,
804 "metadata": {
805 "collapsed": true
806 },
807 "outputs": [],
808 "source": []
1034 }
809 }
1035 ],
810 ],
1036 "metadata": {
811 "metadata": {
1037 "cell_tags": [
812 "cell_tags": [
1038 [
813 [
1039 "<None>",
814 "<None>",
1040 null
815 null
1041 ]
816 ]
1042 ]
817 ],
818 "kernelspec": {
819 "display_name": "Python 3",
820 "name": "python3"
821 },
822 "language_info": {
823 "codemirror_mode": {
824 "name": "ipython",
825 "version": 3
826 },
827 "file_extension": ".py",
828 "mimetype": "text/x-python",
829 "name": "python",
830 "nbconvert_exporter": "python",
831 "pygments_lexer": "ipython3",
832 "version": "3.4.2"
833 }
1043 },
834 },
1044 "nbformat": 4,
835 "nbformat": 4,
1045 "nbformat_minor": 0
836 "nbformat_minor": 0
1046 } No newline at end of file
837 }
General Comments 0
You need to be logged in to leave comments. Login now