##// END OF EJS Templates
More updates
Jonathan Frederic -
Show More
@@ -1,1022 +1,1011 b''
1 1 {
2 2 "metadata": {
3 3 "celltoolbar": "Slideshow",
4 4 "name": "",
5 "signature": "sha256:9763f005a69fc65b7a7977011d9604b09b4959e7293cbbb2cf35cc482380e671"
5 "signature": "sha256:a53951979397cd38785846c18854053eaa5093f6f08246dbcce76b7d243e2153"
6 6 },
7 7 "nbformat": 3,
8 8 "nbformat_minor": 0,
9 9 "worksheets": [
10 10 {
11 11 "cells": [
12 12 {
13 13 "cell_type": "markdown",
14 14 "metadata": {},
15 15 "source": [
16 16 "[Index](Index.ipynb) - [Back](Widget Styling.ipynb)"
17 17 ]
18 18 },
19 19 {
20 20 "cell_type": "code",
21 21 "collapsed": false,
22 22 "input": [
23 23 "from __future__ import print_function # For py 2.7 compat"
24 24 ],
25 25 "language": "python",
26 26 "metadata": {},
27 27 "outputs": [],
28 28 "prompt_number": 3
29 29 },
30 30 {
31 31 "cell_type": "heading",
32 32 "level": 1,
33 33 "metadata": {
34 34 "slideshow": {
35 35 "slide_type": "slide"
36 36 }
37 37 },
38 38 "source": [
39 39 "Building a Custom Widget"
40 40 ]
41 41 },
42 42 {
43 43 "cell_type": "markdown",
44 44 "metadata": {},
45 45 "source": [
46 46 "The widget framework is built **on top of the Comm framework** (short for communication). The Comm framework is a framework that **allows you send/receive JSON messages** to/from the front-end (as seen below).\n",
47 47 "\n",
48 48 "** Insert framework layer image here. **\n",
49 49 "\n",
50 50 "To create a custom widget, you need to **define the widget both in the back-end and in the front-end**. "
51 51 ]
52 52 },
53 53 {
54 54 "cell_type": "heading",
55 55 "level": 1,
56 56 "metadata": {
57 57 "slideshow": {
58 58 "slide_type": "slide"
59 59 }
60 60 },
61 61 "source": [
62 62 "Building a Custom Widget"
63 63 ]
64 64 },
65 65 {
66 66 "cell_type": "markdown",
67 67 "metadata": {},
68 68 "source": [
69 69 "To get started, you'll create a **simple hello world widget**. Later you'll build on this foundation to make more complex widgets."
70 70 ]
71 71 },
72 72 {
73 73 "cell_type": "heading",
74 74 "level": 2,
75 75 "metadata": {
76 76 "slideshow": {
77 77 "slide_type": "slide"
78 78 }
79 79 },
80 80 "source": [
81 81 "Back-end (Python)"
82 82 ]
83 83 },
84 84 {
85 85 "cell_type": "heading",
86 86 "level": 3,
87 87 "metadata": {},
88 88 "source": [
89 89 "DOMWidget and Widget"
90 90 ]
91 91 },
92 92 {
93 93 "cell_type": "markdown",
94 94 "metadata": {},
95 95 "source": [
96 96 "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 is not 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 three.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."
97 97 ]
98 98 },
99 99 {
100 100 "cell_type": "heading",
101 101 "level": 3,
102 102 "metadata": {
103 103 "slideshow": {
104 104 "slide_type": "slide"
105 105 }
106 106 },
107 107 "source": [
108 108 "_view_name"
109 109 ]
110 110 },
111 111 {
112 112 "cell_type": "markdown",
113 113 "metadata": {},
114 114 "source": [
115 115 "Inheriting from the DOMWidget does not 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)."
116 116 ]
117 117 },
118 118 {
119 119 "cell_type": "code",
120 120 "collapsed": false,
121 121 "input": [
122 122 "from IPython.html import widgets\n",
123 123 "from IPython.utils.traitlets import Unicode\n",
124 124 "\n",
125 125 "class HelloWidget(widgets.DOMWidget):\n",
126 126 " _view_name = Unicode('HelloView', sync=True)"
127 127 ],
128 128 "language": "python",
129 129 "metadata": {},
130 130 "outputs": [],
131 "prompt_number": 2
131 "prompt_number": 1
132 132 },
133 133 {
134 134 "cell_type": "heading",
135 135 "level": 3,
136 136 "metadata": {
137 137 "slideshow": {
138 138 "slide_type": "slide"
139 139 }
140 140 },
141 141 "source": [
142 142 "sync=True traitlets"
143 143 ]
144 144 },
145 145 {
146 146 "cell_type": "markdown",
147 147 "metadata": {},
148 148 "source": [
149 149 "**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` keyword argument** tells the widget framework to **handle synchronizing that value to the front-end**. Without `sync=True`, the front-end would have no knowledge of `_view_name`."
150 150 ]
151 151 },
152 152 {
153 153 "cell_type": "heading",
154 154 "level": 3,
155 155 "metadata": {
156 156 "slideshow": {
157 157 "slide_type": "slide"
158 158 }
159 159 },
160 160 "source": [
161 161 "Other traitlet types"
162 162 ]
163 163 },
164 164 {
165 165 "cell_type": "markdown",
166 166 "metadata": {},
167 167 "source": [
168 168 "Unicode, used for _view_name, is not the only Traitlet type, there are many more some of which are listed below: \n",
169 169 "\n",
170 170 "- Any\n",
171 171 "- Bool\n",
172 172 "- Bytes\n",
173 173 "- CBool\n",
174 174 "- CBytes\n",
175 175 "- CComplex\n",
176 176 "- CFloat\n",
177 177 "- CInt\n",
178 178 "- CLong\n",
179 179 "- CRegExp\n",
180 180 "- CUnicode\n",
181 181 "- CaselessStrEnum\n",
182 182 "- Complex\n",
183 183 "- Dict\n",
184 184 "- DottedObjectName\n",
185 185 "- Enum\n",
186 186 "- Float\n",
187 187 "- FunctionType\n",
188 188 "- Instance\n",
189 189 "- InstanceType\n",
190 190 "- Int\n",
191 191 "- List\n",
192 192 "- Long\n",
193 193 "- Set\n",
194 194 "- TCPAddress\n",
195 195 "- Tuple\n",
196 196 "- Type\n",
197 197 "- Unicode\n",
198 198 "\n",
199 199 "**Not all of these traitlets can be synchronized** across the network, **only the JSON-able** traits and **Widget instances** will be synchronized."
200 200 ]
201 201 },
202 202 {
203 203 "cell_type": "heading",
204 204 "level": 2,
205 205 "metadata": {
206 206 "slideshow": {
207 207 "slide_type": "slide"
208 208 }
209 209 },
210 210 "source": [
211 211 "Front-end (JavaScript)"
212 212 ]
213 213 },
214 214 {
215 215 "cell_type": "heading",
216 216 "level": 3,
217 217 "metadata": {},
218 218 "source": [
219 219 "Models and views"
220 220 ]
221 221 },
222 222 {
223 223 "cell_type": "markdown",
224 224 "metadata": {},
225 225 "source": [
226 226 "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 model**."
227 227 ]
228 228 },
229 229 {
230 230 "cell_type": "heading",
231 231 "level": 3,
232 232 "metadata": {
233 233 "slideshow": {
234 234 "slide_type": "slide"
235 235 }
236 236 },
237 237 "source": [
238 238 "Import the WidgetManager"
239 239 ]
240 240 },
241 241 {
242 242 "cell_type": "markdown",
243 243 "metadata": {},
244 244 "source": [
245 245 "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 back-end). To import the widget manager, use the `require` method of [require.js](http://requirejs.org/) (as seen below)."
246 246 ]
247 247 },
248 248 {
249 249 "cell_type": "code",
250 250 "collapsed": false,
251 251 "input": [
252 252 "%%javascript\n",
253 253 "\n",
254 254 "require([\"widgets/js/widget\"], function(WidgetManager){\n",
255 255 " \n",
256 256 "});"
257 257 ],
258 258 "language": "python",
259 259 "metadata": {},
260 260 "outputs": [
261 261 {
262 262 "javascript": [
263 263 "\n",
264 264 "require([\"widgets/js/widget\"], function(WidgetManager){\n",
265 265 " \n",
266 266 "});"
267 267 ],
268 268 "metadata": {},
269 269 "output_type": "display_data",
270 270 "text": [
271 271 "<IPython.core.display.Javascript object>"
272 272 ]
273 273 }
274 274 ],
275 "prompt_number": 3
275 "prompt_number": 2
276 276 },
277 277 {
278 278 "cell_type": "heading",
279 279 "level": 3,
280 280 "metadata": {
281 281 "slideshow": {
282 282 "slide_type": "slide"
283 283 }
284 284 },
285 285 "source": [
286 286 "Define the view"
287 287 ]
288 288 },
289 289 {
290 290 "cell_type": "markdown",
291 291 "metadata": {},
292 292 "source": [
293 293 "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**."
294 294 ]
295 295 },
296 296 {
297 297 "cell_type": "code",
298 298 "collapsed": false,
299 299 "input": [
300 300 "%%javascript\n",
301 301 "\n",
302 302 "require([\"widgets/js/widget\"], function(WidgetManager){\n",
303 303 " \n",
304 304 " // Define the HelloView\n",
305 305 " var HelloView = IPython.DOMWidgetView.extend({\n",
306 306 " \n",
307 307 " });\n",
308 308 " \n",
309 309 " // Register the HelloView with the widget manager.\n",
310 310 " WidgetManager.register_widget_view('HelloView', HelloView);\n",
311 311 "});"
312 312 ],
313 313 "language": "python",
314 314 "metadata": {},
315 315 "outputs": [
316 316 {
317 317 "javascript": [
318 318 "\n",
319 319 "require([\"widgets/js/widget\"], function(WidgetManager){\n",
320 320 " \n",
321 321 " // Define the HelloView\n",
322 322 " var HelloView = IPython.DOMWidgetView.extend({\n",
323 323 " \n",
324 324 " });\n",
325 325 " \n",
326 326 " // Register the HelloView with the widget manager.\n",
327 327 " WidgetManager.register_widget_view('HelloView', HelloView);\n",
328 328 "});"
329 329 ],
330 330 "metadata": {},
331 331 "output_type": "display_data",
332 332 "text": [
333 333 "<IPython.core.display.Javascript object>"
334 334 ]
335 335 }
336 336 ],
337 "prompt_number": 2
337 "prompt_number": 3
338 338 },
339 339 {
340 340 "cell_type": "heading",
341 341 "level": 3,
342 342 "metadata": {
343 343 "slideshow": {
344 344 "slide_type": "slide"
345 345 }
346 346 },
347 347 "source": [
348 348 "Render method"
349 349 ]
350 350 },
351 351 {
352 352 "cell_type": "markdown",
353 353 "metadata": {},
354 354 "source": [
355 355 "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 acquired 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)."
356 356 ]
357 357 },
358 358 {
359 359 "cell_type": "code",
360 360 "collapsed": false,
361 361 "input": [
362 362 "%%javascript\n",
363 363 "\n",
364 364 "require([\"widgets/js/widget\"], function(WidgetManager){ \n",
365 365 " \n",
366 366 " var HelloView = IPython.DOMWidgetView.extend({\n",
367 367 " \n",
368 368 " // Render the view.\n",
369 369 " render: function(){ \n",
370 370 " this.$el.text('Hello World!'); \n",
371 371 " },\n",
372 372 " });\n",
373 373 " \n",
374 374 " WidgetManager.register_widget_view('HelloView', HelloView);\n",
375 375 "});"
376 376 ],
377 377 "language": "python",
378 378 "metadata": {},
379 379 "outputs": [
380 380 {
381 381 "javascript": [
382 382 "\n",
383 383 "require([\"widgets/js/widget\"], function(WidgetManager){ \n",
384 384 " \n",
385 385 " var HelloView = IPython.DOMWidgetView.extend({\n",
386 386 " \n",
387 387 " // Render the view.\n",
388 388 " render: function(){ \n",
389 389 " this.$el.text('Hello World!'); \n",
390 390 " },\n",
391 391 " });\n",
392 392 " \n",
393 393 " WidgetManager.register_widget_view('HelloView', HelloView);\n",
394 394 "});"
395 395 ],
396 396 "metadata": {},
397 397 "output_type": "display_data",
398 398 "text": [
399 399 "<IPython.core.display.Javascript object>"
400 400 ]
401 401 }
402 402 ],
403 "prompt_number": 5
403 "prompt_number": 4
404 404 },
405 405 {
406 406 "cell_type": "heading",
407 407 "level": 2,
408 408 "metadata": {
409 409 "slideshow": {
410 410 "slide_type": "slide"
411 411 }
412 412 },
413 413 "source": [
414 414 "Test"
415 415 ]
416 416 },
417 417 {
418 418 "cell_type": "markdown",
419 419 "metadata": {},
420 420 "source": [
421 421 "You should be able to display your widget just like any other widget now."
422 422 ]
423 423 },
424 424 {
425 425 "cell_type": "code",
426 426 "collapsed": false,
427 427 "input": [
428 428 "HelloWidget()"
429 429 ],
430 430 "language": "python",
431 431 "metadata": {},
432 432 "outputs": [],
433 "prompt_number": 6
433 "prompt_number": 5
434 434 },
435 435 {
436 436 "cell_type": "heading",
437 437 "level": 2,
438 438 "metadata": {
439 439 "slideshow": {
440 440 "slide_type": "slide"
441 441 }
442 442 },
443 443 "source": [
444 444 "Making the widget stateful"
445 445 ]
446 446 },
447 447 {
448 448 "cell_type": "markdown",
449 449 "metadata": {},
450 450 "source": [
451 451 "There is not 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 back-end**. First you need to **add a 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**."
452 452 ]
453 453 },
454 454 {
455 455 "cell_type": "code",
456 456 "collapsed": false,
457 457 "input": [
458 458 "class HelloWidget(widgets.DOMWidget):\n",
459 459 " _view_name = Unicode('HelloView', sync=True)\n",
460 460 " value = Unicode('Hello World!', sync=True)"
461 461 ],
462 462 "language": "python",
463 463 "metadata": {},
464 "outputs": [
465 {
466 "ename": "NameError",
467 "evalue": "name 'widgets' is not defined",
468 "output_type": "pyerr",
469 "traceback": [
470 "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
471 "\u001b[0;32m<ipython-input-5-18ff342e181d>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mclass\u001b[0m \u001b[0mHelloWidget\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mwidgets\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mDOMWidget\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0m_view_name\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mUnicode\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'HelloView'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msync\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mTrue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mvalue\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mUnicode\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Hello World!'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msync\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mTrue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
472 "\u001b[0;31mNameError\u001b[0m: name 'widgets' is not defined"
473 ]
474 }
475 ],
476 "prompt_number": 5
464 "outputs": [],
465 "prompt_number": 6
477 466 },
478 467 {
479 468 "cell_type": "heading",
480 469 "level": 3,
481 470 "metadata": {
482 471 "slideshow": {
483 472 "slide_type": "slide"
484 473 }
485 474 },
486 475 "source": [
487 476 "Accessing the model from the view"
488 477 ]
489 478 },
490 479 {
491 480 "cell_type": "markdown",
492 481 "metadata": {},
493 482 "source": [
494 483 "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 cell**. The model also has a **`on` method** which allows you to listen to events triggered by the model (like value changes)."
495 484 ]
496 485 },
497 486 {
498 487 "cell_type": "heading",
499 488 "level": 3,
500 489 "metadata": {
501 490 "slideshow": {
502 491 "slide_type": "slide"
503 492 }
504 493 },
505 494 "source": [
506 495 "Rendering model contents"
507 496 ]
508 497 },
509 498 {
510 499 "cell_type": "markdown",
511 500 "metadata": {},
512 501 "source": [
513 502 "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."
514 503 ]
515 504 },
516 505 {
517 506 "cell_type": "code",
518 507 "collapsed": false,
519 508 "input": [
520 509 "%%javascript\n",
521 510 "\n",
522 511 "require([\"widgets/js/widget\"], function(WidgetManager){ \n",
523 512 " \n",
524 513 " var HelloView = IPython.DOMWidgetView.extend({\n",
525 514 " \n",
526 515 " render: function(){ \n",
527 516 " this.$el.text(this.model.get('value')); \n",
528 517 " },\n",
529 518 " });\n",
530 519 " \n",
531 520 " WidgetManager.register_widget_view('HelloView', HelloView);\n",
532 521 "});"
533 522 ],
534 523 "language": "python",
535 524 "metadata": {},
536 525 "outputs": [
537 526 {
538 527 "javascript": [
539 528 "\n",
540 529 "require([\"widgets/js/widget\"], function(WidgetManager){ \n",
541 530 " \n",
542 531 " var HelloView = IPython.DOMWidgetView.extend({\n",
543 532 " \n",
544 533 " render: function(){ \n",
545 534 " this.$el.text(this.model.get('value')); \n",
546 535 " },\n",
547 536 " });\n",
548 537 " \n",
549 538 " WidgetManager.register_widget_view('HelloView', HelloView);\n",
550 539 "});"
551 540 ],
552 541 "metadata": {},
553 542 "output_type": "display_data",
554 543 "text": [
555 544 "<IPython.core.display.Javascript object>"
556 545 ]
557 546 }
558 547 ],
559 "prompt_number": 6
548 "prompt_number": 7
560 549 },
561 550 {
562 551 "cell_type": "heading",
563 552 "level": 3,
564 553 "metadata": {
565 554 "slideshow": {
566 555 "slide_type": "slide"
567 556 }
568 557 },
569 558 "source": [
570 559 "Dynamic updates"
571 560 ]
572 561 },
573 562 {
574 563 "cell_type": "markdown",
575 564 "metadata": {},
576 565 "source": [
577 566 "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)."
578 567 ]
579 568 },
580 569 {
581 570 "cell_type": "code",
582 571 "collapsed": false,
583 572 "input": [
584 573 "%%javascript\n",
585 574 "\n",
586 575 "require([\"widgets/js/widget\"], function(WidgetManager){ \n",
587 576 " \n",
588 577 " var HelloView = IPython.DOMWidgetView.extend({\n",
589 578 " \n",
590 579 " \n",
591 580 " render: function(){ \n",
592 581 " this.value_changed();\n",
593 582 " this.model.on('change:value', this.value_changed, this);\n",
594 583 " },\n",
595 584 " \n",
596 585 " value_changed: function() {\n",
597 586 " this.$el.text(this.model.get('value')); \n",
598 587 " },\n",
599 588 " });\n",
600 589 " \n",
601 590 " WidgetManager.register_widget_view('HelloView', HelloView);\n",
602 591 "});"
603 592 ],
604 593 "language": "python",
605 594 "metadata": {},
606 595 "outputs": [
607 596 {
608 597 "javascript": [
609 598 "\n",
610 599 "require([\"widgets/js/widget\"], function(WidgetManager){ \n",
611 600 " \n",
612 601 " var HelloView = IPython.DOMWidgetView.extend({\n",
613 602 " \n",
614 603 " \n",
615 604 " render: function(){ \n",
616 605 " this.value_changed();\n",
617 606 " this.model.on('change:value', this.value_changed, this);\n",
618 607 " },\n",
619 608 " \n",
620 609 " value_changed: function() {\n",
621 610 " this.$el.text(this.model.get('value')); \n",
622 611 " },\n",
623 612 " });\n",
624 613 " \n",
625 614 " WidgetManager.register_widget_view('HelloView', HelloView);\n",
626 615 "});"
627 616 ],
628 617 "metadata": {},
629 618 "output_type": "display_data",
630 619 "text": [
631 620 "<IPython.core.display.Javascript object>"
632 621 ]
633 622 }
634 623 ],
635 "prompt_number": 9
624 "prompt_number": 8
636 625 },
637 626 {
638 627 "cell_type": "heading",
639 628 "level": 2,
640 629 "metadata": {
641 630 "slideshow": {
642 631 "slide_type": "slide"
643 632 }
644 633 },
645 634 "source": [
646 635 "Test"
647 636 ]
648 637 },
649 638 {
650 639 "cell_type": "code",
651 640 "collapsed": false,
652 641 "input": [
653 642 "w = HelloWidget()\n",
654 643 "w"
655 644 ],
656 645 "language": "python",
657 646 "metadata": {},
658 647 "outputs": [],
659 "prompt_number": 10
648 "prompt_number": 9
660 649 },
661 650 {
662 651 "cell_type": "code",
663 652 "collapsed": false,
664 653 "input": [
665 654 "w.value = 'test'"
666 655 ],
667 656 "language": "python",
668 657 "metadata": {},
669 658 "outputs": [],
670 "prompt_number": 11
659 "prompt_number": 10
671 660 },
672 661 {
673 662 "cell_type": "heading",
674 663 "level": 1,
675 664 "metadata": {
676 665 "slideshow": {
677 666 "slide_type": "slide"
678 667 }
679 668 },
680 669 "source": [
681 670 "Finishing"
682 671 ]
683 672 },
684 673 {
685 674 "cell_type": "heading",
686 675 "level": 2,
687 676 "metadata": {},
688 677 "source": [
689 678 "Bidirectional communication"
690 679 ]
691 680 },
692 681 {
693 682 "cell_type": "markdown",
694 683 "metadata": {},
695 684 "source": [
696 685 "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. In 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."
697 686 ]
698 687 },
699 688 {
700 689 "cell_type": "heading",
701 690 "level": 3,
702 691 "metadata": {
703 692 "slideshow": {
704 693 "slide_type": "slide"
705 694 }
706 695 },
707 696 "source": [
708 697 "Update the Python code"
709 698 ]
710 699 },
711 700 {
712 701 "cell_type": "markdown",
713 702 "metadata": {},
714 703 "source": [
715 704 "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`."
716 705 ]
717 706 },
718 707 {
719 708 "cell_type": "code",
720 709 "collapsed": false,
721 710 "input": [
722 711 "from IPython.utils.traitlets import CInt\n",
723 712 "class SpinnerWidget(widgets.DOMWidget):\n",
724 713 " _view_name = Unicode('SpinnerView', sync=True)\n",
725 714 " value = CInt(0, sync=True)"
726 715 ],
727 716 "language": "python",
728 717 "metadata": {},
729 718 "outputs": [],
730 "prompt_number": 12
719 "prompt_number": 11
731 720 },
732 721 {
733 722 "cell_type": "heading",
734 723 "level": 3,
735 724 "metadata": {
736 725 "slideshow": {
737 726 "slide_type": "slide"
738 727 }
739 728 },
740 729 "source": [
741 730 "Updating the Javascript code"
742 731 ]
743 732 },
744 733 {
745 734 "cell_type": "markdown",
746 735 "metadata": {},
747 736 "source": [
748 737 "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."
749 738 ]
750 739 },
751 740 {
752 741 "cell_type": "code",
753 742 "collapsed": false,
754 743 "input": [
755 744 "%%javascript\n",
756 745 "\n",
757 746 "require([\"widgets/js/widget\"], function(WidgetManager){ \n",
758 747 " \n",
759 748 " var SpinnerView = IPython.DOMWidgetView.extend({\n",
760 749 " \n",
761 750 " render: function(){ \n",
762 751 " \n",
763 752 " // jQuery code to create a spinner and append it to $el\n",
764 753 " this.$input = $('<input />');\n",
765 754 " this.$el.append(this.$input);\n",
766 755 " this.$spinner = this.$input.spinner({\n",
767 756 " change: function( event, ui ) {}\n",
768 757 " });\n",
769 758 " \n",
770 759 " this.value_changed();\n",
771 760 " this.model.on('change:value', this.value_changed, this);\n",
772 761 " },\n",
773 762 " \n",
774 763 " value_changed: function() {\n",
775 764 " \n",
776 765 " },\n",
777 766 " });\n",
778 767 " \n",
779 768 " WidgetManager.register_widget_view('SpinnerView', SpinnerView);\n",
780 769 "});"
781 770 ],
782 771 "language": "python",
783 772 "metadata": {},
784 773 "outputs": [
785 774 {
786 775 "javascript": [
787 776 "\n",
788 777 "require([\"widgets/js/widget\"], function(WidgetManager){ \n",
789 778 " \n",
790 779 " var SpinnerView = IPython.DOMWidgetView.extend({\n",
791 780 " \n",
792 781 " render: function(){ \n",
793 782 " \n",
794 783 " // jQuery code to create a spinner and append it to $el\n",
795 784 " this.$input = $('<input />');\n",
796 785 " this.$el.append(this.$input);\n",
797 786 " this.$spinner = this.$input.spinner({\n",
798 787 " change: function( event, ui ) {}\n",
799 788 " });\n",
800 789 " \n",
801 790 " this.value_changed();\n",
802 791 " this.model.on('change:value', this.value_changed, this);\n",
803 792 " },\n",
804 793 " \n",
805 794 " value_changed: function() {\n",
806 795 " \n",
807 796 " },\n",
808 797 " });\n",
809 798 " \n",
810 799 " WidgetManager.register_widget_view('SpinnerView', SpinnerView);\n",
811 800 "});"
812 801 ],
813 802 "metadata": {},
814 803 "output_type": "display_data",
815 804 "text": [
816 805 "<IPython.core.display.Javascript object>"
817 806 ]
818 807 }
819 808 ],
820 "prompt_number": 13
809 "prompt_number": 12
821 810 },
822 811 {
823 812 "cell_type": "heading",
824 813 "level": 3,
825 814 "metadata": {
826 815 "slideshow": {
827 816 "slide_type": "slide"
828 817 }
829 818 },
830 819 "source": [
831 820 "Getting and setting the value"
832 821 ]
833 822 },
834 823 {
835 824 "cell_type": "markdown",
836 825 "metadata": {},
837 826 "source": [
838 827 "To **set the value of the spinner on update from the back-end**, 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.**"
839 828 ]
840 829 },
841 830 {
842 831 "cell_type": "code",
843 832 "collapsed": false,
844 833 "input": [
845 834 "%%javascript\n",
846 835 "\n",
847 836 "require([\"widgets/js/widget\"], function(WidgetManager){ \n",
848 837 " \n",
849 838 " var SpinnerView = IPython.DOMWidgetView.extend({\n",
850 839 " \n",
851 840 " render: function(){ \n",
852 841 "\n",
853 842 " var that = this;\n",
854 843 " this.$input = $('<input />');\n",
855 844 " this.$el.append(this.$input);\n",
856 845 " this.$spinner = this.$input.spinner({\n",
857 846 " change: function( event, ui ) {\n",
858 847 " that.handle_spin();\n",
859 848 " },\n",
860 849 " spin: function( event, ui ) {\n",
861 850 " that.handle_spin();\n",
862 851 " }\n",
863 852 " });\n",
864 853 " \n",
865 854 " this.value_changed();\n",
866 855 " this.model.on('change:value', this.value_changed, this);\n",
867 856 " },\n",
868 857 " \n",
869 858 " value_changed: function() {\n",
870 859 " this.$spinner.spinner('value', this.model.get('value'));\n",
871 860 " },\n",
872 861 " \n",
873 862 " handle_spin: function() {\n",
874 863 " this.model.set('value', this.$spinner.spinner('value'));\n",
875 864 " this.touch();\n",
876 865 " },\n",
877 866 " });\n",
878 867 " \n",
879 868 " WidgetManager.register_widget_view('SpinnerView', SpinnerView);\n",
880 869 "});"
881 870 ],
882 871 "language": "python",
883 872 "metadata": {},
884 873 "outputs": [
885 874 {
886 875 "javascript": [
887 876 "\n",
888 877 "require([\"widgets/js/widget\"], function(WidgetManager){ \n",
889 878 " \n",
890 879 " var SpinnerView = IPython.DOMWidgetView.extend({\n",
891 880 " \n",
892 881 " render: function(){ \n",
893 882 "\n",
894 883 " var that = this;\n",
895 884 " this.$input = $('<input />');\n",
896 885 " this.$el.append(this.$input);\n",
897 886 " this.$spinner = this.$input.spinner({\n",
898 887 " change: function( event, ui ) {\n",
899 888 " that.handle_spin();\n",
900 889 " },\n",
901 890 " spin: function( event, ui ) {\n",
902 891 " that.handle_spin();\n",
903 892 " }\n",
904 893 " });\n",
905 894 " \n",
906 895 " this.value_changed();\n",
907 896 " this.model.on('change:value', this.value_changed, this);\n",
908 897 " },\n",
909 898 " \n",
910 899 " value_changed: function() {\n",
911 900 " this.$spinner.spinner('value', this.model.get('value'));\n",
912 901 " },\n",
913 902 " \n",
914 903 " handle_spin: function() {\n",
915 904 " this.model.set('value', this.$spinner.spinner('value'));\n",
916 905 " this.touch();\n",
917 906 " },\n",
918 907 " });\n",
919 908 " \n",
920 909 " WidgetManager.register_widget_view('SpinnerView', SpinnerView);\n",
921 910 "});"
922 911 ],
923 912 "metadata": {},
924 913 "output_type": "display_data",
925 914 "text": [
926 915 "<IPython.core.display.Javascript object>"
927 916 ]
928 917 }
929 918 ],
930 "prompt_number": 14
919 "prompt_number": 13
931 920 },
932 921 {
933 922 "cell_type": "heading",
934 923 "level": 2,
935 924 "metadata": {
936 925 "slideshow": {
937 926 "slide_type": "slide"
938 927 }
939 928 },
940 929 "source": [
941 930 "Test"
942 931 ]
943 932 },
944 933 {
945 934 "cell_type": "code",
946 935 "collapsed": false,
947 936 "input": [
948 937 "w = SpinnerWidget(value=5)\n",
949 938 "w"
950 939 ],
951 940 "language": "python",
952 941 "metadata": {},
953 942 "outputs": [],
954 943 "prompt_number": 15
955 944 },
956 945 {
957 946 "cell_type": "code",
958 947 "collapsed": false,
959 948 "input": [
960 949 "w.value"
961 950 ],
962 951 "language": "python",
963 952 "metadata": {},
964 953 "outputs": [
965 954 {
966 955 "metadata": {},
967 956 "output_type": "pyout",
968 957 "prompt_number": 16,
969 958 "text": [
970 959 "5"
971 960 ]
972 961 }
973 962 ],
974 963 "prompt_number": 16
975 964 },
976 965 {
977 966 "cell_type": "code",
978 967 "collapsed": false,
979 968 "input": [
980 969 "w.value = 20"
981 970 ],
982 971 "language": "python",
983 972 "metadata": {},
984 973 "outputs": [],
985 974 "prompt_number": 17
986 975 },
987 976 {
988 977 "cell_type": "markdown",
989 978 "metadata": {},
990 979 "source": [
991 980 "Trying to **use the spinner with another widget**."
992 981 ]
993 982 },
994 983 {
995 984 "cell_type": "code",
996 985 "collapsed": false,
997 986 "input": [
998 987 "from IPython.display import display\n",
999 988 "w1 = SpinnerWidget(value=0)\n",
1000 989 "w2 = widgets.IntSliderWidget()\n",
1001 990 "display(w1,w2)\n",
1002 991 "\n",
1003 992 "from IPython.utils.traitlets import link\n",
1004 993 "mylink = link((w1, 'value'), (w2, 'value'))"
1005 994 ],
1006 995 "language": "python",
1007 996 "metadata": {},
1008 997 "outputs": [],
1009 998 "prompt_number": 18
1010 999 },
1011 1000 {
1012 1001 "cell_type": "markdown",
1013 1002 "metadata": {},
1014 1003 "source": [
1015 1004 "[Index](Index.ipynb) - [Back](Widget Styling.ipynb)"
1016 1005 ]
1017 1006 }
1018 1007 ],
1019 1008 "metadata": {}
1020 1009 }
1021 1010 ]
1022 1011 } No newline at end of file
@@ -1,456 +1,468 b''
1 1 {
2 2 "metadata": {
3 3 "celltoolbar": "Slideshow",
4 4 "name": "",
5 "signature": "sha256:f6c1dd624d35ab0c768f77dffdae914baf4d88a3c244047c00ffca8b9c96e81e"
5 "signature": "sha256:b8ade017615ae4e656f0740a85c77764ff451dd651110af0b5dc92fd5cb409ff"
6 6 },
7 7 "nbformat": 3,
8 8 "nbformat_minor": 0,
9 9 "worksheets": [
10 10 {
11 11 "cells": [
12 12 {
13 13 "cell_type": "markdown",
14 14 "metadata": {},
15 15 "source": [
16 16 "[Index](Index.ipynb) - [Next](Widget List.ipynb)"
17 17 ]
18 18 },
19 19 {
20 20 "cell_type": "heading",
21 21 "level": 1,
22 22 "metadata": {},
23 23 "source": [
24 24 "Simple Widget Introduction"
25 25 ]
26 26 },
27 27 {
28 28 "cell_type": "heading",
29 29 "level": 2,
30 30 "metadata": {},
31 31 "source": [
32 32 "What are widgets?"
33 33 ]
34 34 },
35 35 {
36 36 "cell_type": "markdown",
37 37 "metadata": {
38 38 "slideshow": {
39 39 "slide_type": "slide"
40 40 }
41 41 },
42 42 "source": [
43 43 "Widgets are elements that exists in both the front-end and the back-end.\n",
44 44 "\n",
45 45 "** Insert Frontend-Backend Picture **"
46 46 ]
47 47 },
48 48 {
49 49 "cell_type": "heading",
50 50 "level": 2,
51 51 "metadata": {},
52 52 "source": [
53 53 "What can they be used for?"
54 54 ]
55 55 },
56 56 {
57 57 "cell_type": "markdown",
58 58 "metadata": {
59 59 "slideshow": {
60 60 "slide_type": "slide"
61 61 }
62 62 },
63 63 "source": [
64 64 "You can use widgets to build **interactive GUIs** for your notebooks. \n",
65 65 "You can also use widgets to **synchronize stateful and stateless information** between Python and JavaScript."
66 66 ]
67 67 },
68 68 {
69 69 "cell_type": "heading",
70 70 "level": 2,
71 71 "metadata": {},
72 72 "source": [
73 73 "Using widgets "
74 74 ]
75 75 },
76 76 {
77 77 "cell_type": "markdown",
78 78 "metadata": {
79 79 "slideshow": {
80 80 "slide_type": "slide"
81 81 }
82 82 },
83 83 "source": [
84 84 "To use the widget framework, you need to **import `IPython.html.widgets`**."
85 85 ]
86 86 },
87 87 {
88 88 "cell_type": "code",
89 89 "collapsed": false,
90 90 "input": [
91 91 "from IPython.html.widgets import *"
92 92 ],
93 93 "language": "python",
94 94 "metadata": {},
95 95 "outputs": [],
96 96 "prompt_number": 1
97 97 },
98 98 {
99 99 "cell_type": "heading",
100 100 "level": 3,
101 101 "metadata": {
102 102 "slideshow": {
103 103 "slide_type": "slide"
104 104 }
105 105 },
106 106 "source": [
107 107 "repr"
108 108 ]
109 109 },
110 110 {
111 111 "cell_type": "markdown",
112 112 "metadata": {},
113 113 "source": [
114 114 "Widgets have their own display `repr` which allows them to be displayed using IPython's display framework. Constructing and returning an `IntSliderWidget` automatically displays the widget (as seen below). Widgets are **displayed inside the `widget area`**, which sits between the code cell and output. **You can hide all of the widgets** in the `widget area` by clicking the grey *x* in the margin."
115 115 ]
116 116 },
117 117 {
118 118 "cell_type": "code",
119 119 "collapsed": false,
120 120 "input": [
121 121 "IntSliderWidget()"
122 122 ],
123 123 "language": "python",
124 124 "metadata": {},
125 125 "outputs": [],
126 126 "prompt_number": 2
127 127 },
128 128 {
129 129 "cell_type": "heading",
130 130 "level": 3,
131 131 "metadata": {
132 132 "slideshow": {
133 133 "slide_type": "slide"
134 134 }
135 135 },
136 136 "source": [
137 137 "display()"
138 138 ]
139 139 },
140 140 {
141 141 "cell_type": "markdown",
142 142 "metadata": {},
143 143 "source": [
144 144 "You can also explicitly display the widget using `display(...)`."
145 145 ]
146 146 },
147 147 {
148 148 "cell_type": "code",
149 149 "collapsed": false,
150 150 "input": [
151 151 "from IPython.display import display\n",
152 152 "w = IntSliderWidget()\n",
153 153 "display(w)"
154 154 ],
155 155 "language": "python",
156 156 "metadata": {},
157 157 "outputs": [],
158 158 "prompt_number": 3
159 159 },
160 160 {
161 161 "cell_type": "heading",
162 162 "level": 3,
163 163 "metadata": {
164 164 "slideshow": {
165 165 "slide_type": "slide"
166 166 }
167 167 },
168 168 "source": [
169 169 "Multiple display() calls"
170 170 ]
171 171 },
172 172 {
173 173 "cell_type": "markdown",
174 174 "metadata": {},
175 175 "source": [
176 176 "If you display the same widget twice, the displayed instances in the front-end **will remain in sync** with each other."
177 177 ]
178 178 },
179 179 {
180 180 "cell_type": "code",
181 181 "collapsed": false,
182 182 "input": [
183 183 "display(w)"
184 184 ],
185 185 "language": "python",
186 186 "metadata": {},
187 187 "outputs": [],
188 188 "prompt_number": 4
189 189 },
190 190 {
191 191 "cell_type": "heading",
192 192 "level": 2,
193 193 "metadata": {},
194 194 "source": [
195 195 "Why does displaying the same widget twice work?"
196 196 ]
197 197 },
198 198 {
199 199 "cell_type": "markdown",
200 200 "metadata": {
201 201 "slideshow": {
202 202 "slide_type": "slide"
203 203 }
204 204 },
205 205 "source": [
206 206 "Widgets are **represented in the back-end by a single object**. Each time a widget is displayed, **a new representation** of that same object is created in the front-end. These representations are called **views**.\n",
207 207 "\n",
208 208 "** Insert Backend-Frontend Views Figure **"
209 209 ]
210 210 },
211 211 {
212 212 "cell_type": "heading",
213 213 "level": 3,
214 214 "metadata": {
215 215 "slideshow": {
216 216 "slide_type": "slide"
217 217 }
218 218 },
219 219 "source": [
220 220 "Closing widgets"
221 221 ]
222 222 },
223 223 {
224 224 "cell_type": "markdown",
225 225 "metadata": {},
226 226 "source": [
227 227 "You can close a widget by calling its `close()` method."
228 228 ]
229 229 },
230 230 {
231 231 "cell_type": "code",
232 232 "collapsed": false,
233 233 "input": [
234 234 "w.close()"
235 235 ],
236 236 "language": "python",
237 237 "metadata": {},
238 238 "outputs": [],
239 239 "prompt_number": 5
240 240 },
241 241 {
242 242 "cell_type": "heading",
243 243 "level": 2,
244 244 "metadata": {},
245 245 "source": [
246 246 "Widget properties"
247 247 ]
248 248 },
249 249 {
250 250 "cell_type": "markdown",
251 251 "metadata": {
252 252 "slideshow": {
253 253 "slide_type": "slide"
254 254 }
255 255 },
256 256 "source": [
257 257 "All of the IPython widgets **share a similar naming scheme**. To read the value of a widget, you can query its `value` property."
258 258 ]
259 259 },
260 260 {
261 261 "cell_type": "code",
262 262 "collapsed": false,
263 263 "input": [
264 "w = IntSliderWidget()\n",
265 "display(w)"
266 ],
267 "language": "python",
268 "metadata": {},
269 "outputs": [],
270 "prompt_number": 9
271 },
272 {
273 "cell_type": "code",
274 "collapsed": false,
275 "input": [
264 276 "w.value"
265 277 ],
266 278 "language": "python",
267 279 "metadata": {},
268 280 "outputs": [
269 281 {
270 282 "metadata": {},
271 283 "output_type": "pyout",
272 "prompt_number": 6,
284 "prompt_number": 11,
273 285 "text": [
274 "0"
286 "40"
275 287 ]
276 288 }
277 289 ],
278 "prompt_number": 6
290 "prompt_number": 11
279 291 },
280 292 {
281 293 "cell_type": "markdown",
282 294 "metadata": {},
283 295 "source": [
284 296 "Similarly, to set a widget's value, you can set its `value` property."
285 297 ]
286 298 },
287 299 {
288 300 "cell_type": "code",
289 301 "collapsed": false,
290 302 "input": [
291 303 "w.value = 100"
292 304 ],
293 305 "language": "python",
294 306 "metadata": {},
295 307 "outputs": [],
296 "prompt_number": 7
308 "prompt_number": 12
297 309 },
298 310 {
299 311 "cell_type": "heading",
300 312 "level": 3,
301 313 "metadata": {
302 314 "slideshow": {
303 315 "slide_type": "slide"
304 316 }
305 317 },
306 318 "source": [
307 319 "Keys"
308 320 ]
309 321 },
310 322 {
311 323 "cell_type": "markdown",
312 324 "metadata": {},
313 325 "source": [
314 326 "In addition to `value`, most widgets share `keys`, `description`, `disabled`, and `visible`. To see the entire list of synchronized, stateful properties, of any specific widget, you can **query the `keys` property**."
315 327 ]
316 328 },
317 329 {
318 330 "cell_type": "code",
319 331 "collapsed": false,
320 332 "input": [
321 333 "w.keys"
322 334 ],
323 335 "language": "python",
324 336 "metadata": {},
325 337 "outputs": [
326 338 {
327 339 "metadata": {},
328 340 "output_type": "pyout",
329 "prompt_number": 8,
341 "prompt_number": 13,
330 342 "text": [
331 343 "['_view_name',\n",
332 344 " 'orientation',\n",
333 345 " 'msg_throttle',\n",
334 346 " 'min',\n",
335 347 " 'max',\n",
336 348 " '_css',\n",
337 349 " 'value',\n",
338 350 " 'readout',\n",
339 351 " 'disabled',\n",
340 352 " 'visible',\n",
341 353 " 'step',\n",
342 354 " 'description']"
343 355 ]
344 356 }
345 357 ],
346 "prompt_number": 8
358 "prompt_number": 13
347 359 },
348 360 {
349 361 "cell_type": "heading",
350 362 "level": 3,
351 363 "metadata": {},
352 364 "source": [
353 365 "Shorthand for setting the initial values of widget properties"
354 366 ]
355 367 },
356 368 {
357 369 "cell_type": "markdown",
358 370 "metadata": {
359 371 "slideshow": {
360 372 "slide_type": "slide"
361 373 }
362 374 },
363 375 "source": [
364 376 "While creating a widget, you can set some or all of the initial values of that widget by **defining them as keyword arguments in the widget's constructor** (as seen below)."
365 377 ]
366 378 },
367 379 {
368 380 "cell_type": "code",
369 381 "collapsed": false,
370 382 "input": [
371 383 "TextWidget(value='Hello World!', disabled=True)"
372 384 ],
373 385 "language": "python",
374 386 "metadata": {},
375 387 "outputs": [],
376 "prompt_number": 9
388 "prompt_number": 14
377 389 },
378 390 {
379 391 "cell_type": "heading",
380 392 "level": 2,
381 393 "metadata": {},
382 394 "source": [
383 395 "Linking two similar widgets"
384 396 ]
385 397 },
386 398 {
387 399 "cell_type": "markdown",
388 400 "metadata": {
389 401 "slideshow": {
390 402 "slide_type": "slide"
391 403 }
392 404 },
393 405 "source": [
394 406 "If you need to display the same value two different ways, you'll have to use two different widgets. Instead of **attempting to manually synchronize the values** of the two widgets, you can use the `traitlet` `link` function **to link two properties together**. Below, the values of three widgets are linked together."
395 407 ]
396 408 },
397 409 {
398 410 "cell_type": "code",
399 411 "collapsed": false,
400 412 "input": [
401 413 "from IPython.utils.traitlets import link\n",
402 414 "a = FloatTextWidget()\n",
403 415 "b = FloatSliderWidget()\n",
404 416 "c = FloatProgressWidget()\n",
405 417 "display(a,b,c)\n",
406 418 "\n",
407 419 "\n",
408 420 "mylink = link((a, 'value'), (b, 'value'), (c, 'value'))"
409 421 ],
410 422 "language": "python",
411 423 "metadata": {},
412 424 "outputs": [],
413 "prompt_number": 10
425 "prompt_number": 15
414 426 },
415 427 {
416 428 "cell_type": "heading",
417 429 "level": 3,
418 430 "metadata": {},
419 431 "source": [
420 432 "Unlinking widgets"
421 433 ]
422 434 },
423 435 {
424 436 "cell_type": "markdown",
425 437 "metadata": {
426 438 "slideshow": {
427 439 "slide_type": "slide"
428 440 }
429 441 },
430 442 "source": [
431 443 "Unlinking the widgets is simple. All you have to do is call `.unlink` on the link object."
432 444 ]
433 445 },
434 446 {
435 447 "cell_type": "code",
436 448 "collapsed": false,
437 449 "input": [
438 450 "mylink.unlink()"
439 451 ],
440 452 "language": "python",
441 453 "metadata": {},
442 454 "outputs": [],
443 "prompt_number": 11
455 "prompt_number": 16
444 456 },
445 457 {
446 458 "cell_type": "markdown",
447 459 "metadata": {},
448 460 "source": [
449 461 "[Index](Index.ipynb) - [Next](Widget List.ipynb)"
450 462 ]
451 463 }
452 464 ],
453 465 "metadata": {}
454 466 }
455 467 ]
456 468 } No newline at end of file
@@ -1,279 +1,352 b''
1 1 {
2 2 "metadata": {
3 3 "cell_tags": [
4 4 [
5 5 "<None>",
6 6 null
7 7 ]
8 8 ],
9 9 "celltoolbar": "Slideshow",
10 10 "name": "",
11 "signature": "sha256:ac1b90929c7736b2261ba498b6f28dd89d3e7b7981c43f937879febe6841e4cd"
11 "signature": "sha256:00bfd86bbcdbb8dcaea27753885c4ee4727c9b56bd460c3ee0595641e4afe21f"
12 12 },
13 13 "nbformat": 3,
14 14 "nbformat_minor": 0,
15 15 "worksheets": [
16 16 {
17 17 "cells": [
18 18 {
19 19 "cell_type": "markdown",
20 20 "metadata": {},
21 21 "source": [
22 22 "[Index](Index.ipynb) - [Back](Widget List.ipynb) - [Next](Widget Styling.ipynb)"
23 23 ]
24 24 },
25 25 {
26 26 "cell_type": "heading",
27 27 "level": 1,
28 28 "metadata": {
29 29 "slideshow": {
30 30 "slide_type": "slide"
31 31 }
32 32 },
33 33 "source": [
34 34 "Widget Events"
35 35 ]
36 36 },
37 37 {
38 38 "cell_type": "heading",
39 39 "level": 2,
40 40 "metadata": {},
41 41 "source": [
42 42 "Special events"
43 43 ]
44 44 },
45 45 {
46 46 "cell_type": "code",
47 47 "collapsed": false,
48 48 "input": [
49 49 "from __future__ import print_function"
50 50 ],
51 51 "language": "python",
52 52 "metadata": {},
53 53 "outputs": [],
54 54 "prompt_number": 1
55 55 },
56 56 {
57 57 "cell_type": "markdown",
58 58 "metadata": {},
59 59 "source": [
60 60 "The `ButtonWidget` is not used to represent a data type. Instead the button widget is used to **handle mouse clicks**. The **`on_click` method** of the `ButtonWidget` can be used to register function to be called when the button is clicked. The doc string of the `on_click` can be seen below."
61 61 ]
62 62 },
63 63 {
64 64 "cell_type": "code",
65 65 "collapsed": false,
66 66 "input": [
67 67 "from IPython.html import widgets\n",
68 68 "print(widgets.ButtonWidget.on_click.__doc__)"
69 69 ],
70 70 "language": "python",
71 71 "metadata": {},
72 72 "outputs": [
73 73 {
74 74 "output_type": "stream",
75 75 "stream": "stdout",
76 76 "text": [
77 77 "Register a callback to execute when the button is clicked.\n",
78 78 "\n",
79 79 " The callback will be called with one argument,\n",
80 80 " the clicked button widget instance.\n",
81 81 "\n",
82 82 " Parameters\n",
83 83 " ----------\n",
84 84 " remove : bool (optional)\n",
85 85 " Set to true to remove the callback from the list of callbacks.\n"
86 86 ]
87 87 }
88 88 ],
89 89 "prompt_number": 2
90 90 },
91 91 {
92 92 "cell_type": "heading",
93 93 "level": 3,
94 94 "metadata": {
95 95 "slideshow": {
96 96 "slide_type": "slide"
97 97 }
98 98 },
99 99 "source": [
100 100 "Example"
101 101 ]
102 102 },
103 103 {
104 104 "cell_type": "markdown",
105 105 "metadata": {},
106 106 "source": [
107 107 "Since button clicks are **stateless**, they are **transmitted from the front-end to the back-end using custom messages**. By using the `on_click` method, a button that prints a message when it has been clicked is shown below."
108 108 ]
109 109 },
110 110 {
111 111 "cell_type": "code",
112 112 "collapsed": false,
113 113 "input": [
114 114 "from IPython.display import display\n",
115 115 "button = widgets.ButtonWidget(description=\"Click Me!\")\n",
116 116 "display(button)\n",
117 117 "\n",
118 118 "def on_button_clicked(b):\n",
119 119 " print(\"Button clicked.\")\n",
120 120 "\n",
121 121 "button.on_click(on_button_clicked)"
122 122 ],
123 123 "language": "python",
124 124 "metadata": {},
125 "outputs": [],
125 "outputs": [
126 {
127 "output_type": "stream",
128 "stream": "stdout",
129 "text": [
130 "Button clicked.\n"
131 ]
132 },
133 {
134 "output_type": "stream",
135 "stream": "stdout",
136 "text": [
137 "Button clicked.\n"
138 ]
139 },
140 {
141 "output_type": "stream",
142 "stream": "stdout",
143 "text": [
144 "Button clicked.\n"
145 ]
146 }
147 ],
126 148 "prompt_number": 3
127 149 },
128 150 {
129 151 "cell_type": "heading",
130 152 "level": 3,
131 153 "metadata": {
132 154 "slideshow": {
133 155 "slide_type": "slide"
134 156 }
135 157 },
136 158 "source": [
137 159 "on_sumbit"
138 160 ]
139 161 },
140 162 {
141 163 "cell_type": "markdown",
142 164 "metadata": {},
143 165 "source": [
144 166 "The **`TextWidget`** also has a special **`on_submit` event**. The `on_submit` event **fires when the user hits return**."
145 167 ]
146 168 },
147 169 {
148 170 "cell_type": "code",
149 171 "collapsed": false,
150 172 "input": [
151 173 "text = widgets.TextWidget()\n",
152 174 "display(text)\n",
153 175 "\n",
154 176 "def handle_submit(sender):\n",
155 177 " print(text.value)\n",
156 178 "\n",
157 179 "text.on_submit(handle_submit)"
158 180 ],
159 181 "language": "python",
160 182 "metadata": {},
161 "outputs": [],
183 "outputs": [
184 {
185 "output_type": "stream",
186 "stream": "stdout",
187 "text": [
188 "adsfasdf\n"
189 ]
190 }
191 ],
162 192 "prompt_number": 4
163 193 },
164 194 {
165 195 "cell_type": "heading",
166 196 "level": 2,
167 197 "metadata": {
168 198 "slideshow": {
169 199 "slide_type": "slide"
170 200 }
171 201 },
172 202 "source": [
173 203 "Traitlet events"
174 204 ]
175 205 },
176 206 {
177 207 "cell_type": "markdown",
178 208 "metadata": {},
179 209 "source": [
180 210 "**Widget properties are IPython traitlets** and **traitlets are eventful**. To handle changes, the **`on_trait_change` method** of the widget can be used to **register a callback**. The doc string for `on_trait_change` can be seen below."
181 211 ]
182 212 },
183 213 {
184 214 "cell_type": "code",
185 215 "collapsed": false,
186 216 "input": [
187 217 "print(widgets.Widget.on_trait_change.__doc__)"
188 218 ],
189 219 "language": "python",
190 220 "metadata": {},
191 221 "outputs": [
192 222 {
193 223 "output_type": "stream",
194 224 "stream": "stdout",
195 225 "text": [
196 226 "Setup a handler to be called when a trait changes.\n",
197 227 "\n",
198 228 " This is used to setup dynamic notifications of trait changes.\n",
199 229 "\n",
200 230 " Static handlers can be created by creating methods on a HasTraits\n",
201 231 " subclass with the naming convention '_[traitname]_changed'. Thus,\n",
202 232 " to create static handler for the trait 'a', create the method\n",
203 233 " _a_changed(self, name, old, new) (fewer arguments can be used, see\n",
204 234 " below).\n",
205 235 "\n",
206 236 " Parameters\n",
207 237 " ----------\n",
208 238 " handler : callable\n",
209 239 " A callable that is called when a trait changes. Its\n",
210 240 " signature can be handler(), handler(name), handler(name, new)\n",
211 241 " or handler(name, old, new).\n",
212 242 " name : list, str, None\n",
213 243 " If None, the handler will apply to all traits. If a list\n",
214 244 " of str, handler will apply to all names in the list. If a\n",
215 245 " str, the handler will apply just to that name.\n",
216 246 " remove : bool\n",
217 247 " If False (the default), then install the handler. If True\n",
218 248 " then unintall it.\n",
219 249 " \n"
220 250 ]
221 251 }
222 252 ],
223 253 "prompt_number": 5
224 254 },
225 255 {
226 256 "cell_type": "heading",
227 257 "level": 3,
228 258 "metadata": {
229 259 "slideshow": {
230 260 "slide_type": "slide"
231 261 }
232 262 },
233 263 "source": [
234 264 "Signatures"
235 265 ]
236 266 },
237 267 {
238 268 "cell_type": "markdown",
239 269 "metadata": {},
240 270 "source": [
241 271 "Mentioned in the doc string, the callback registered can have **4 possible signatures**:\n",
242 272 "\n",
243 273 "- callback()\n",
244 274 "- callback(trait_name)\n",
245 275 "- callback(trait_name, new_value)\n",
246 276 "- callback(trait_name, old_value, new_value)\n",
247 277 "\n",
248 278 "Using this method, an example of how to output an IntSliderWiget's value as it is changed can be seen below."
249 279 ]
250 280 },
251 281 {
252 282 "cell_type": "code",
253 283 "collapsed": false,
254 284 "input": [
255 285 "int_range = widgets.IntSliderWidget()\n",
256 286 "display(int_range)\n",
257 287 "\n",
258 288 "def on_value_change(name, value):\n",
259 289 " print(value)\n",
260 290 "\n",
261 291 "int_range.on_trait_change(on_value_change, 'value')"
262 292 ],
263 293 "language": "python",
264 294 "metadata": {},
265 "outputs": [],
295 "outputs": [
296 {
297 "output_type": "stream",
298 "stream": "stdout",
299 "text": [
300 "1\n"
301 ]
302 },
303 {
304 "output_type": "stream",
305 "stream": "stdout",
306 "text": [
307 "4\n"
308 ]
309 },
310 {
311 "output_type": "stream",
312 "stream": "stdout",
313 "text": [
314 "13\n"
315 ]
316 },
317 {
318 "output_type": "stream",
319 "stream": "stdout",
320 "text": [
321 "26\n"
322 ]
323 },
324 {
325 "output_type": "stream",
326 "stream": "stdout",
327 "text": [
328 "31\n"
329 ]
330 },
331 {
332 "output_type": "stream",
333 "stream": "stdout",
334 "text": [
335 "34\n"
336 ]
337 }
338 ],
266 339 "prompt_number": 6
267 340 },
268 341 {
269 342 "cell_type": "markdown",
270 343 "metadata": {},
271 344 "source": [
272 345 "[Index](Index.ipynb) - [Back](Widget List.ipynb) - [Next](Widget Styling.ipynb)"
273 346 ]
274 347 }
275 348 ],
276 349 "metadata": {}
277 350 }
278 351 ]
279 352 } No newline at end of file
@@ -1,651 +1,651 b''
1 1 {
2 2 "metadata": {
3 3 "celltoolbar": "Slideshow",
4 4 "name": "",
5 "signature": "sha256:4200cc7b2c7067f4c5391eeee6ba0d9be43b4faa16a245ba1634e7de66150372"
5 "signature": "sha256:5ded7796417476da43feb6a52919043719f504bd72c145163e3e8a1105c5b642"
6 6 },
7 7 "nbformat": 3,
8 8 "nbformat_minor": 0,
9 9 "worksheets": [
10 10 {
11 11 "cells": [
12 12 {
13 13 "cell_type": "markdown",
14 14 "metadata": {},
15 15 "source": [
16 16 "[Index](Index.ipynb) - [Back](Widget Basics.ipynb) - [Next](Widget Events.ipynb)"
17 17 ]
18 18 },
19 19 {
20 20 "cell_type": "heading",
21 21 "level": 1,
22 22 "metadata": {},
23 23 "source": [
24 24 "Widget List"
25 25 ]
26 26 },
27 27 {
28 28 "cell_type": "heading",
29 29 "level": 2,
30 30 "metadata": {},
31 31 "source": [
32 32 "Complete list"
33 33 ]
34 34 },
35 35 {
36 36 "cell_type": "markdown",
37 37 "metadata": {
38 38 "slideshow": {
39 39 "slide_type": "slide"
40 40 }
41 41 },
42 42 "source": [
43 43 "For a complete list of the widgets available to you, you can list the classes in the widget namespace (as seen below). Classes with the suffix `Widget` are widgets. `Widget` and `DOMWidget` are base classes."
44 44 ]
45 45 },
46 46 {
47 47 "cell_type": "code",
48 48 "collapsed": false,
49 49 "input": [
50 50 "from IPython.html import widgets\n",
51 51 "[w for w in dir(widgets) if w.endswith('Widget')]"
52 52 ],
53 53 "language": "python",
54 54 "metadata": {},
55 55 "outputs": [
56 56 {
57 57 "metadata": {},
58 58 "output_type": "pyout",
59 59 "prompt_number": 1,
60 60 "text": [
61 61 "['AccordionWidget',\n",
62 62 " 'BoundedFloatTextWidget',\n",
63 63 " 'BoundedIntTextWidget',\n",
64 64 " 'ButtonWidget',\n",
65 65 " 'CheckboxWidget',\n",
66 66 " 'ContainerWidget',\n",
67 67 " 'DOMWidget',\n",
68 68 " 'DropdownWidget',\n",
69 69 " 'FloatProgressWidget',\n",
70 70 " 'FloatSliderWidget',\n",
71 71 " 'FloatTextWidget',\n",
72 72 " 'HTMLWidget',\n",
73 73 " 'ImageWidget',\n",
74 74 " 'IntProgressWidget',\n",
75 75 " 'IntSliderWidget',\n",
76 76 " 'IntTextWidget',\n",
77 77 " 'LatexWidget',\n",
78 78 " 'PopupWidget',\n",
79 79 " 'RadioButtonsWidget',\n",
80 80 " 'SelectWidget',\n",
81 81 " 'TabWidget',\n",
82 82 " 'TextWidget',\n",
83 83 " 'TextareaWidget',\n",
84 84 " 'ToggleButtonWidget',\n",
85 85 " 'ToggleButtonsWidget',\n",
86 86 " 'Widget']"
87 87 ]
88 88 }
89 89 ],
90 90 "prompt_number": 1
91 91 },
92 92 {
93 93 "cell_type": "heading",
94 94 "level": 2,
95 95 "metadata": {
96 96 "slideshow": {
97 97 "slide_type": "slide"
98 98 }
99 99 },
100 100 "source": [
101 101 "Numeric widgets"
102 102 ]
103 103 },
104 104 {
105 105 "cell_type": "markdown",
106 106 "metadata": {},
107 107 "source": [
108 108 "There are 8 widgets distributed with IPython that are designed to display numeric values. Widgets exist for displaying integers and floats, both bounded and unbounded. The integer widgets share a similar naming scheme to their floating point counterparts. By replacing `Float` with `Int` in the widget name, you can find the Integer equivalent."
109 109 ]
110 110 },
111 111 {
112 112 "cell_type": "heading",
113 113 "level": 3,
114 114 "metadata": {
115 115 "slideshow": {
116 116 "slide_type": "slide"
117 117 }
118 118 },
119 119 "source": [
120 120 "FloatSliderWidget"
121 121 ]
122 122 },
123 123 {
124 124 "cell_type": "code",
125 125 "collapsed": false,
126 126 "input": [
127 127 "widgets.FloatSliderWidget(\n",
128 128 " value=7.5,\n",
129 129 " min=5.0,\n",
130 130 " max=10.0,\n",
131 131 " step=0.1,\n",
132 132 " description='Test:',\n",
133 133 ")"
134 134 ],
135 135 "language": "python",
136 136 "metadata": {},
137 137 "outputs": [],
138 138 "prompt_number": 2
139 139 },
140 140 {
141 141 "cell_type": "markdown",
142 142 "metadata": {},
143 143 "source": [
144 144 "Sliders can also be **displayed vertically**."
145 145 ]
146 146 },
147 147 {
148 148 "cell_type": "code",
149 149 "collapsed": false,
150 150 "input": [
151 151 "widgets.FloatSliderWidget(\n",
152 152 " value=7.5,\n",
153 153 " min=5.0,\n",
154 154 " max=10.0,\n",
155 155 " step=0.1,\n",
156 156 " description='Test',\n",
157 157 " orientation='vertical',\n",
158 158 ")"
159 159 ],
160 160 "language": "python",
161 161 "metadata": {},
162 162 "outputs": [],
163 163 "prompt_number": 3
164 164 },
165 165 {
166 166 "cell_type": "heading",
167 167 "level": 3,
168 168 "metadata": {
169 169 "slideshow": {
170 170 "slide_type": "slide"
171 171 }
172 172 },
173 173 "source": [
174 174 "FloatProgressWidget"
175 175 ]
176 176 },
177 177 {
178 178 "cell_type": "code",
179 179 "collapsed": false,
180 180 "input": [
181 181 "widgets.FloatProgressWidget(\n",
182 182 " value=7.5,\n",
183 183 " min=5.0,\n",
184 184 " max=10.0,\n",
185 185 " step=0.1,\n",
186 186 " description='Loading:',\n",
187 187 ")"
188 188 ],
189 189 "language": "python",
190 190 "metadata": {},
191 191 "outputs": [],
192 192 "prompt_number": 4
193 193 },
194 194 {
195 195 "cell_type": "heading",
196 196 "level": 3,
197 197 "metadata": {
198 198 "slideshow": {
199 199 "slide_type": "slide"
200 200 }
201 201 },
202 202 "source": [
203 203 "BoundedFloatTextWidget"
204 204 ]
205 205 },
206 206 {
207 207 "cell_type": "code",
208 208 "collapsed": false,
209 209 "input": [
210 210 "widgets.BoundedFloatTextWidget(\n",
211 211 " value=7.5,\n",
212 212 " min=5.0,\n",
213 213 " max=10.0,\n",
214 214 " description='Text:',\n",
215 215 ")"
216 216 ],
217 217 "language": "python",
218 218 "metadata": {},
219 219 "outputs": [],
220 220 "prompt_number": 5
221 221 },
222 222 {
223 223 "cell_type": "heading",
224 224 "level": 3,
225 225 "metadata": {
226 226 "slideshow": {
227 227 "slide_type": "slide"
228 228 }
229 229 },
230 230 "source": [
231 231 "FloatTextWidget"
232 232 ]
233 233 },
234 234 {
235 235 "cell_type": "code",
236 236 "collapsed": false,
237 237 "input": [
238 238 "widgets.FloatTextWidget(\n",
239 239 " value=7.5,\n",
240 240 " description='Any:',\n",
241 241 ")"
242 242 ],
243 243 "language": "python",
244 244 "metadata": {},
245 245 "outputs": [],
246 246 "prompt_number": 6
247 247 },
248 248 {
249 249 "cell_type": "heading",
250 250 "level": 2,
251 251 "metadata": {
252 252 "slideshow": {
253 253 "slide_type": "slide"
254 254 }
255 255 },
256 256 "source": [
257 257 "Boolean widgets"
258 258 ]
259 259 },
260 260 {
261 261 "cell_type": "markdown",
262 262 "metadata": {},
263 263 "source": [
264 264 "There are two widgets that are designed to display a boolean value."
265 265 ]
266 266 },
267 267 {
268 268 "cell_type": "heading",
269 269 "level": 3,
270 270 "metadata": {},
271 271 "source": [
272 272 "ToggleButtonWidget"
273 273 ]
274 274 },
275 275 {
276 276 "cell_type": "code",
277 277 "collapsed": false,
278 278 "input": [
279 279 "widgets.ToggleButtonWidget(\n",
280 280 " description='Click me',\n",
281 281 " value=False,\n",
282 282 ")"
283 283 ],
284 284 "language": "python",
285 285 "metadata": {},
286 286 "outputs": [],
287 287 "prompt_number": 7
288 288 },
289 289 {
290 290 "cell_type": "heading",
291 291 "level": 3,
292 292 "metadata": {
293 293 "slideshow": {
294 294 "slide_type": "slide"
295 295 }
296 296 },
297 297 "source": [
298 298 "CheckboxWidget"
299 299 ]
300 300 },
301 301 {
302 302 "cell_type": "code",
303 303 "collapsed": false,
304 304 "input": [
305 305 "widgets.CheckboxWidget(\n",
306 306 " description='Check me',\n",
307 307 " value=True,\n",
308 308 ")"
309 309 ],
310 310 "language": "python",
311 311 "metadata": {},
312 312 "outputs": [],
313 313 "prompt_number": 8
314 314 },
315 315 {
316 316 "cell_type": "heading",
317 317 "level": 2,
318 318 "metadata": {
319 319 "slideshow": {
320 320 "slide_type": "slide"
321 321 }
322 322 },
323 323 "source": [
324 324 "Selection widgets"
325 325 ]
326 326 },
327 327 {
328 328 "cell_type": "markdown",
329 329 "metadata": {},
330 330 "source": [
331 331 "There are four widgets that can be used to display single selection lists. All four inherit from the same base class. You can specify the **enumeration of selectables by passing a list**. You can **also specify the enumeration as a dictionary**, in which case the **keys will be used as the item displayed** in the list and the corresponding **value will be returned** when an item is selected."
332 332 ]
333 333 },
334 334 {
335 335 "cell_type": "heading",
336 336 "level": 3,
337 337 "metadata": {
338 338 "slideshow": {
339 339 "slide_type": "slide"
340 340 }
341 341 },
342 342 "source": [
343 343 "DropdownWidget"
344 344 ]
345 345 },
346 346 {
347 347 "cell_type": "code",
348 348 "collapsed": false,
349 349 "input": [
350 350 "from IPython.display import display\n",
351 351 "w = widgets.DropdownWidget(\n",
352 352 " values=[1, 2, 3],\n",
353 353 " value=2,\n",
354 354 " description='Number:',\n",
355 355 ")\n",
356 356 "display(w)"
357 357 ],
358 358 "language": "python",
359 359 "metadata": {},
360 360 "outputs": [],
361 361 "prompt_number": 9
362 362 },
363 363 {
364 364 "cell_type": "code",
365 365 "collapsed": false,
366 366 "input": [
367 367 "w.value"
368 368 ],
369 369 "language": "python",
370 370 "metadata": {},
371 371 "outputs": [
372 372 {
373 373 "metadata": {},
374 374 "output_type": "pyout",
375 375 "prompt_number": 10,
376 376 "text": [
377 377 "2"
378 378 ]
379 379 }
380 380 ],
381 381 "prompt_number": 10
382 382 },
383 383 {
384 384 "cell_type": "markdown",
385 385 "metadata": {},
386 386 "source": [
387 387 "The following is also valid:"
388 388 ]
389 389 },
390 390 {
391 391 "cell_type": "code",
392 392 "collapsed": false,
393 393 "input": [
394 394 "w = widgets.DropdownWidget(\n",
395 395 " values={'One': 1, 'Two': 2, 'Three': 3},\n",
396 396 " value=2,\n",
397 397 " description='Number:',\n",
398 398 ")\n",
399 399 "display(w)"
400 400 ],
401 401 "language": "python",
402 402 "metadata": {},
403 403 "outputs": [],
404 404 "prompt_number": 11
405 405 },
406 406 {
407 407 "cell_type": "code",
408 408 "collapsed": false,
409 409 "input": [
410 410 "w.value"
411 411 ],
412 412 "language": "python",
413 413 "metadata": {},
414 414 "outputs": [
415 415 {
416 416 "metadata": {},
417 417 "output_type": "pyout",
418 418 "prompt_number": 12,
419 419 "text": [
420 420 "2"
421 421 ]
422 422 }
423 423 ],
424 424 "prompt_number": 12
425 425 },
426 426 {
427 427 "cell_type": "heading",
428 428 "level": 3,
429 429 "metadata": {
430 430 "slideshow": {
431 431 "slide_type": "slide"
432 432 }
433 433 },
434 434 "source": [
435 435 "RadioButtonsWidget"
436 436 ]
437 437 },
438 438 {
439 439 "cell_type": "code",
440 440 "collapsed": false,
441 441 "input": [
442 442 "widgets.RadioButtonsWidget(\n",
443 443 " description='Pizza topping:',\n",
444 444 " values=['pepperoni', 'pineapple', 'anchovies'],\n",
445 445 ")"
446 446 ],
447 447 "language": "python",
448 448 "metadata": {},
449 449 "outputs": [],
450 450 "prompt_number": 13
451 451 },
452 452 {
453 453 "cell_type": "heading",
454 454 "level": 3,
455 455 "metadata": {
456 456 "slideshow": {
457 457 "slide_type": "slide"
458 458 }
459 459 },
460 460 "source": [
461 461 "SelectWidget"
462 462 ]
463 463 },
464 464 {
465 465 "cell_type": "code",
466 466 "collapsed": false,
467 467 "input": [
468 468 "widgets.SelectWidget(\n",
469 469 " description='OS:',\n",
470 470 " values=['Linux', 'Windows', 'OSX'],\n",
471 471 ")"
472 472 ],
473 473 "language": "python",
474 474 "metadata": {},
475 475 "outputs": [],
476 476 "prompt_number": 14
477 477 },
478 478 {
479 479 "cell_type": "heading",
480 480 "level": 3,
481 481 "metadata": {
482 482 "slideshow": {
483 483 "slide_type": "slide"
484 484 }
485 485 },
486 486 "source": [
487 487 "ToggleButtonsWidget"
488 488 ]
489 489 },
490 490 {
491 491 "cell_type": "code",
492 492 "collapsed": false,
493 493 "input": [
494 494 "widgets.ToggleButtonsWidget(\n",
495 495 " description='Speed:',\n",
496 496 " values=['Slow', 'Regular', 'Fast'],\n",
497 497 ")"
498 498 ],
499 499 "language": "python",
500 500 "metadata": {},
501 501 "outputs": [],
502 502 "prompt_number": 15
503 503 },
504 504 {
505 505 "cell_type": "heading",
506 506 "level": 2,
507 507 "metadata": {
508 508 "slideshow": {
509 509 "slide_type": "slide"
510 510 }
511 511 },
512 512 "source": [
513 513 "String widgets"
514 514 ]
515 515 },
516 516 {
517 517 "cell_type": "markdown",
518 518 "metadata": {},
519 519 "source": [
520 520 "There are 4 widgets that can be used to display a string value. Of those, the **`TextWidget` and `TextareaWidget` accept input**. The **`LatexWidget` and `HTMLWidget` display the string** as either Latex or HTML respectively, but **do not accept input**."
521 521 ]
522 522 },
523 523 {
524 524 "cell_type": "heading",
525 525 "level": 3,
526 526 "metadata": {
527 527 "slideshow": {
528 528 "slide_type": "slide"
529 529 }
530 530 },
531 531 "source": [
532 532 "TextWidget"
533 533 ]
534 534 },
535 535 {
536 536 "cell_type": "code",
537 537 "collapsed": false,
538 538 "input": [
539 539 "widgets.TextWidget(\n",
540 540 " description='String:',\n",
541 541 " value='Hello World',\n",
542 542 ")"
543 543 ],
544 544 "language": "python",
545 545 "metadata": {},
546 546 "outputs": [],
547 547 "prompt_number": 16
548 548 },
549 549 {
550 550 "cell_type": "heading",
551 551 "level": 3,
552 552 "metadata": {},
553 553 "source": [
554 554 "TextareaWidget"
555 555 ]
556 556 },
557 557 {
558 558 "cell_type": "code",
559 559 "collapsed": false,
560 560 "input": [
561 561 "widgets.TextareaWidget(\n",
562 562 " description='String:',\n",
563 563 " value='Hello World',\n",
564 564 ")"
565 565 ],
566 566 "language": "python",
567 567 "metadata": {},
568 568 "outputs": [],
569 569 "prompt_number": 17
570 570 },
571 571 {
572 572 "cell_type": "heading",
573 573 "level": 3,
574 574 "metadata": {
575 575 "slideshow": {
576 576 "slide_type": "slide"
577 577 }
578 578 },
579 579 "source": [
580 580 "LatexWidget"
581 581 ]
582 582 },
583 583 {
584 584 "cell_type": "code",
585 585 "collapsed": false,
586 586 "input": [
587 587 "widgets.LatexWidget(\n",
588 588 " value=\"$$\\\\frac{n!}{k!(n-k)!} = \\\\binom{n}{k}$$\",\n",
589 589 ")"
590 590 ],
591 591 "language": "python",
592 592 "metadata": {},
593 593 "outputs": [],
594 594 "prompt_number": 18
595 595 },
596 596 {
597 597 "cell_type": "heading",
598 598 "level": 3,
599 599 "metadata": {},
600 600 "source": [
601 601 "HTMLWidget"
602 602 ]
603 603 },
604 604 {
605 605 "cell_type": "code",
606 606 "collapsed": false,
607 607 "input": [
608 608 "widgets.HTMLWidget(\n",
609 609 " value=\"Hello <b>World</b>\"\n",
610 610 ")"
611 611 ],
612 612 "language": "python",
613 613 "metadata": {},
614 614 "outputs": [],
615 "prompt_number": 20
615 "prompt_number": 19
616 616 },
617 617 {
618 618 "cell_type": "heading",
619 619 "level": 2,
620 620 "metadata": {
621 621 "slideshow": {
622 622 "slide_type": "slide"
623 623 }
624 624 },
625 625 "source": [
626 626 "ButtonWidget"
627 627 ]
628 628 },
629 629 {
630 630 "cell_type": "code",
631 631 "collapsed": false,
632 632 "input": [
633 633 "widgets.ButtonWidget(description='Click me')"
634 634 ],
635 635 "language": "python",
636 636 "metadata": {},
637 637 "outputs": [],
638 "prompt_number": 21
638 "prompt_number": 20
639 639 },
640 640 {
641 641 "cell_type": "markdown",
642 642 "metadata": {},
643 643 "source": [
644 644 "[Index](Index.ipynb) - [Back](Widget Basics.ipynb) - [Next](Widget Events.ipynb)"
645 645 ]
646 646 }
647 647 ],
648 648 "metadata": {}
649 649 }
650 650 ]
651 651 } No newline at end of file
@@ -1,1465 +1,1465 b''
1 1 {
2 2 "metadata": {
3 3 "cell_tags": [
4 4 [
5 5 "<None>",
6 6 null
7 7 ]
8 8 ],
9 9 "celltoolbar": "Slideshow",
10 10 "name": "",
11 "signature": "sha256:6ee07cf9871c5a2431610eb8a2a5057f9e669769932696690f70967b255fbea7"
11 "signature": "sha256:ea0c2f71869ec3d5fb1b007e1f13023fa5fc580ad7bee737d5cbbce8a1490169"
12 12 },
13 13 "nbformat": 3,
14 14 "nbformat_minor": 0,
15 15 "worksheets": [
16 16 {
17 17 "cells": [
18 18 {
19 19 "cell_type": "markdown",
20 20 "metadata": {},
21 21 "source": [
22 22 "[Index](Index.ipynb) - [Back](Widget Events.ipynb) - [Next](Custom Widget - Hello World.ipynb)"
23 23 ]
24 24 },
25 25 {
26 26 "cell_type": "code",
27 27 "collapsed": false,
28 28 "input": [
29 29 "%%html\n",
30 30 "<style>\n",
31 31 ".example-container { background: #999999; padding: 2px; min-height: 100px; }\n",
32 32 ".example-container.sm { min-height: 50px; }\n",
33 33 ".example-box { background: #9999FF; width: 50px; height: 50px; text-align: center; vertical-align: middle; color: white; font-weight: bold; margin: 2px;}\n",
34 34 ".example-box.med { width: 65px; height: 65px; } \n",
35 35 ".example-box.lrg { width: 80px; height: 80px; } \n",
36 36 "</style>"
37 37 ],
38 38 "language": "python",
39 39 "metadata": {},
40 40 "outputs": [
41 41 {
42 42 "html": [
43 43 "<style>\n",
44 44 ".example-container { background: #999999; padding: 2px; min-height: 100px; }\n",
45 45 ".example-container.sm { min-height: 50px; }\n",
46 46 ".example-box { background: #9999FF; width: 50px; height: 50px; text-align: center; vertical-align: middle; color: white; font-weight: bold; margin: 2px;}\n",
47 47 ".example-box.med { width: 65px; height: 65px; } \n",
48 48 ".example-box.lrg { width: 80px; height: 80px; } \n",
49 49 "</style>"
50 50 ],
51 51 "metadata": {},
52 52 "output_type": "display_data",
53 53 "text": [
54 54 "<IPython.core.display.HTML object>"
55 55 ]
56 56 }
57 57 ],
58 "prompt_number": 6
58 "prompt_number": 1
59 59 },
60 60 {
61 61 "cell_type": "heading",
62 62 "level": 1,
63 63 "metadata": {
64 64 "slideshow": {
65 65 "slide_type": "slide"
66 66 }
67 67 },
68 68 "source": [
69 69 "Widget Styling"
70 70 ]
71 71 },
72 72 {
73 73 "cell_type": "heading",
74 74 "level": 2,
75 75 "metadata": {},
76 76 "source": [
77 77 "CSS"
78 78 ]
79 79 },
80 80 {
81 81 "cell_type": "markdown",
82 82 "metadata": {},
83 83 "source": [
84 84 "Since the representation of the widget you see is a **browser element**, **Cascading Style Sheets (CSS)** are used for styling. Widgets have a **`set_css` method** that allows you to **add and remove CSS properties** from your elements. The following example shows had `set_css` **can be used to set the background color** of a `TextWidget`."
85 85 ]
86 86 },
87 87 {
88 88 "cell_type": "code",
89 89 "collapsed": false,
90 90 "input": [
91 91 "from IPython.html import widgets\n",
92 "text = widgets.TextWidget()\n",
92 "text = widgets.TextWidget(value=\"Hello World!\")\n",
93 93 "text.set_css('background', 'lime')\n",
94 94 "text "
95 95 ],
96 96 "language": "python",
97 97 "metadata": {},
98 98 "outputs": [],
99 "prompt_number": 7
99 "prompt_number": 5
100 100 },
101 101 {
102 102 "cell_type": "heading",
103 103 "level": 3,
104 104 "metadata": {
105 105 "slideshow": {
106 106 "slide_type": "slide"
107 107 }
108 108 },
109 109 "source": [
110 110 "Color codes"
111 111 ]
112 112 },
113 113 {
114 114 "cell_type": "markdown",
115 115 "metadata": {},
116 116 "source": [
117 117 "In the example above, **the color `lime` is specified by name**. CSS also supports specifying colors by a **3 byte hexadecimal string**. The first byte is red, second green, and third blue (**RGB**). The following example sets the `TextWidget`'s background to blue."
118 118 ]
119 119 },
120 120 {
121 121 "cell_type": "code",
122 122 "collapsed": false,
123 123 "input": [
124 124 "text.set_css('background', '#0000FF')"
125 125 ],
126 126 "language": "python",
127 127 "metadata": {},
128 128 "outputs": [],
129 "prompt_number": 8
129 "prompt_number": 6
130 130 },
131 131 {
132 132 "cell_type": "heading",
133 133 "level": 3,
134 134 "metadata": {
135 135 "slideshow": {
136 136 "slide_type": "slide"
137 137 }
138 138 },
139 139 "source": [
140 140 "Forecolor"
141 141 ]
142 142 },
143 143 {
144 144 "cell_type": "markdown",
145 145 "metadata": {},
146 146 "source": [
147 147 "In CSS the **font color is `color`.**"
148 148 ]
149 149 },
150 150 {
151 151 "cell_type": "code",
152 152 "collapsed": false,
153 153 "input": [
154 154 "text.set_css('color', '#FFFFFF')"
155 155 ],
156 156 "language": "python",
157 157 "metadata": {},
158 158 "outputs": [],
159 "prompt_number": 9
159 "prompt_number": 7
160 160 },
161 161 {
162 162 "cell_type": "heading",
163 163 "level": 3,
164 164 "metadata": {
165 165 "slideshow": {
166 166 "slide_type": "slide"
167 167 }
168 168 },
169 169 "source": [
170 170 "Size"
171 171 ]
172 172 },
173 173 {
174 174 "cell_type": "markdown",
175 175 "metadata": {},
176 176 "source": [
177 177 "CSS is also used to set the **height and width** of controls. The `set_css` method also **can accept a single dictionary with multiple CSS properties** (as seen below)."
178 178 ]
179 179 },
180 180 {
181 181 "cell_type": "code",
182 182 "collapsed": false,
183 183 "input": [
184 184 "btn = widgets.ButtonWidget()\n",
185 185 "btn.set_css({\n",
186 186 " 'width': '100px',\n",
187 187 " 'height': '100px',\n",
188 188 " 'background': 'red',\n",
189 189 "})\n",
190 190 "btn"
191 191 ],
192 192 "language": "python",
193 193 "metadata": {},
194 194 "outputs": [],
195 "prompt_number": 12
195 "prompt_number": 8
196 196 },
197 197 {
198 198 "cell_type": "heading",
199 199 "level": 3,
200 200 "metadata": {
201 201 "slideshow": {
202 202 "slide_type": "slide"
203 203 }
204 204 },
205 205 "source": [
206 206 "Removing"
207 207 ]
208 208 },
209 209 {
210 210 "cell_type": "markdown",
211 211 "metadata": {},
212 212 "source": [
213 213 "To remove the styling, you can call `set_css` again, but use an empty string instead of a color value."
214 214 ]
215 215 },
216 216 {
217 217 "cell_type": "code",
218 218 "collapsed": false,
219 219 "input": [
220 220 "btn.set_css('background', '')"
221 221 ],
222 222 "language": "python",
223 223 "metadata": {},
224 224 "outputs": [],
225 "prompt_number": 13
225 "prompt_number": 9
226 226 },
227 227 {
228 228 "cell_type": "markdown",
229 229 "metadata": {},
230 230 "source": [
231 231 "For more information about what can be done with CSS, please refer to the [Mozilla Developer Network's series on it](https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Getting_started).\n"
232 232 ]
233 233 },
234 234 {
235 235 "cell_type": "heading",
236 236 "level": 2,
237 237 "metadata": {
238 238 "slideshow": {
239 239 "slide_type": "slide"
240 240 }
241 241 },
242 242 "source": [
243 243 "Parent/child relationships"
244 244 ]
245 245 },
246 246 {
247 247 "cell_type": "markdown",
248 248 "metadata": {},
249 249 "source": [
250 250 "To display widget A inside widget B, widget A must be a child of widget B. **Only one instance of any particular widget can be child of another (this limitation will be removed in IPython 3.0).** In other words, *widget A* cannot have *widget B* listed twice in it's list of children.\n",
251 251 "\n",
252 252 "Widgets that can contain other widgets have a **`children` attribute**. This attribute can be **set via a keyword argument** in the widget's constructor **or after construction**. Calling display on an **object with children automatically displays those children**, too."
253 253 ]
254 254 },
255 255 {
256 256 "cell_type": "code",
257 257 "collapsed": false,
258 258 "input": [
259 259 "\n",
260 260 "from IPython.display import display\n",
261 261 "\n",
262 262 "float_range = widgets.FloatSliderWidget()\n",
263 263 "string = widgets.TextWidget(value='hi')\n",
264 264 "container = widgets.ContainerWidget(children=[float_range, string])\n",
265 265 "\n",
266 266 "container.set_css('border', '3px dotted red')\n",
267 267 "display(container) # Displays the `container` and all of it's children."
268 268 ],
269 269 "language": "python",
270 270 "metadata": {},
271 271 "outputs": [],
272 "prompt_number": 14
272 "prompt_number": 10
273 273 },
274 274 {
275 275 "cell_type": "heading",
276 276 "level": 3,
277 277 "metadata": {},
278 278 "source": [
279 279 "After the parent is displayed"
280 280 ]
281 281 },
282 282 {
283 283 "cell_type": "markdown",
284 284 "metadata": {
285 285 "slideshow": {
286 286 "slide_type": "slide"
287 287 }
288 288 },
289 289 "source": [
290 290 "Children **can be added to parents** after the parent has been displayed. The **parent is responsible for rendering its children**."
291 291 ]
292 292 },
293 293 {
294 294 "cell_type": "code",
295 295 "collapsed": false,
296 296 "input": [
297 297 "container = widgets.ContainerWidget()\n",
298 298 "container.set_css('border', '3px dotted red')\n",
299 299 "display(container)\n",
300 300 "\n",
301 301 "int_range = widgets.IntSliderWidget()\n",
302 302 "container.children=[int_range]"
303 303 ],
304 304 "language": "python",
305 305 "metadata": {},
306 306 "outputs": [],
307 "prompt_number": 15
307 "prompt_number": 11
308 308 },
309 309 {
310 310 "cell_type": "heading",
311 311 "level": 2,
312 312 "metadata": {
313 313 "slideshow": {
314 314 "slide_type": "slide"
315 315 }
316 316 },
317 317 "source": [
318 318 "Fancy containers"
319 319 ]
320 320 },
321 321 {
322 322 "cell_type": "markdown",
323 323 "metadata": {},
324 324 "source": [
325 325 "If you need to display a more complicated set of widgets, there are **specialized containers** that you can use. To display **multiple sets of widgets**, you can use an **`AccordionWidget` or a `TabWidget` in combination with one `ContainerWidget` per set of widgets** (as seen below). The \"pages\" of these widgets are their children. To set the titles of the pages, one must **call `set_title` after the widget has been displayed**."
326 326 ]
327 327 },
328 328 {
329 329 "cell_type": "heading",
330 330 "level": 3,
331 331 "metadata": {},
332 332 "source": [
333 333 "AccordionWidget"
334 334 ]
335 335 },
336 336 {
337 337 "cell_type": "code",
338 338 "collapsed": false,
339 339 "input": [
340 340 "name1 = widgets.TextWidget(description='Location:')\n",
341 341 "zip1 = widgets.BoundedIntTextWidget(description='Zip:', min=0, max=99999)\n",
342 342 "page1 = widgets.ContainerWidget(children=[name1, zip1])\n",
343 343 "\n",
344 344 "name2 = widgets.TextWidget(description='Location:')\n",
345 345 "zip2 = widgets.BoundedIntTextWidget(description='Zip:', min=0, max=99999)\n",
346 346 "page2 = widgets.ContainerWidget(children=[name2, zip2])\n",
347 347 "\n",
348 348 "accord = widgets.AccordionWidget(children=[page1, page2])\n",
349 349 "display(accord)\n",
350 350 "\n",
351 351 "accord.set_title(0, 'From')\n",
352 352 "accord.set_title(1, 'To')"
353 353 ],
354 354 "language": "python",
355 355 "metadata": {},
356 356 "outputs": [],
357 "prompt_number": 16
357 "prompt_number": 12
358 358 },
359 359 {
360 360 "cell_type": "heading",
361 361 "level": 3,
362 362 "metadata": {
363 363 "slideshow": {
364 364 "slide_type": "slide"
365 365 }
366 366 },
367 367 "source": [
368 368 "TabWidget"
369 369 ]
370 370 },
371 371 {
372 372 "cell_type": "code",
373 373 "collapsed": false,
374 374 "input": [
375 375 "name = widgets.TextWidget(description='Name:')\n",
376 376 "color = widgets.DropdownWidget(description='Color:', values=['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet'])\n",
377 377 "page1 = widgets.ContainerWidget(children=[name, color])\n",
378 378 "\n",
379 379 "age = widgets.IntSliderWidget(description='Age:', min=0, max=120, value=50)\n",
380 380 "gender = widgets.RadioButtonsWidget(description='Gender:', values=['male', 'female'])\n",
381 381 "page2 = widgets.ContainerWidget(children=[age, gender])\n",
382 382 "\n",
383 383 "tabs = widgets.TabWidget(children=[page1, page2])\n",
384 384 "display(tabs)\n",
385 385 "\n",
386 386 "tabs.set_title(0, 'Name')\n",
387 387 "tabs.set_title(1, 'Details')"
388 388 ],
389 389 "language": "python",
390 390 "metadata": {},
391 391 "outputs": [],
392 "prompt_number": 17
392 "prompt_number": 13
393 393 },
394 394 {
395 395 "cell_type": "heading",
396 396 "level": 3,
397 397 "metadata": {
398 398 "slideshow": {
399 399 "slide_type": "slide"
400 400 }
401 401 },
402 402 "source": [
403 403 "PopupWidget"
404 404 ]
405 405 },
406 406 {
407 407 "cell_type": "markdown",
408 408 "metadata": {},
409 409 "source": [
410 410 "Unlike the other two special containers, the `PopupWidget` is only **designed to display one set of widgets**. The `PopupWidget` can be used to **display widgets outside of the widget area**. "
411 411 ]
412 412 },
413 413 {
414 414 "cell_type": "code",
415 415 "collapsed": false,
416 416 "input": [
417 417 "counter = widgets.IntTextWidget(description='Counter:')\n",
418 418 "popup = widgets.PopupWidget(children=[counter], description='Popup Demo', button_text='Popup Button')\n",
419 419 "display(popup)"
420 420 ],
421 421 "language": "python",
422 422 "metadata": {},
423 423 "outputs": [],
424 "prompt_number": 18
424 "prompt_number": 14
425 425 },
426 426 {
427 427 "cell_type": "code",
428 428 "collapsed": false,
429 429 "input": [
430 430 "counter.value += 1"
431 431 ],
432 432 "language": "python",
433 433 "metadata": {},
434 434 "outputs": [],
435 "prompt_number": 19
435 "prompt_number": 15
436 436 },
437 437 {
438 438 "cell_type": "code",
439 439 "collapsed": false,
440 440 "input": [],
441 441 "language": "python",
442 442 "metadata": {},
443 443 "outputs": [],
444 "prompt_number": 19
444 "prompt_number": 15
445 445 },
446 446 {
447 447 "cell_type": "code",
448 448 "collapsed": false,
449 449 "input": [],
450 450 "language": "python",
451 451 "metadata": {},
452 452 "outputs": [],
453 "prompt_number": 19
453 "prompt_number": 15
454 454 },
455 455 {
456 456 "cell_type": "code",
457 457 "collapsed": false,
458 458 "input": [],
459 459 "language": "python",
460 460 "metadata": {},
461 461 "outputs": [],
462 "prompt_number": 19
462 "prompt_number": 15
463 463 },
464 464 {
465 465 "cell_type": "code",
466 466 "collapsed": false,
467 467 "input": [],
468 468 "language": "python",
469 469 "metadata": {},
470 470 "outputs": [],
471 "prompt_number": 19
471 "prompt_number": 15
472 472 },
473 473 {
474 474 "cell_type": "code",
475 475 "collapsed": false,
476 476 "input": [],
477 477 "language": "python",
478 478 "metadata": {},
479 479 "outputs": [],
480 "prompt_number": 19
480 "prompt_number": 15
481 481 },
482 482 {
483 483 "cell_type": "code",
484 484 "collapsed": false,
485 485 "input": [],
486 486 "language": "python",
487 487 "metadata": {},
488 488 "outputs": [],
489 "prompt_number": 19
489 "prompt_number": 15
490 490 },
491 491 {
492 492 "cell_type": "code",
493 493 "collapsed": false,
494 494 "input": [],
495 495 "language": "python",
496 496 "metadata": {},
497 497 "outputs": [],
498 "prompt_number": 19
498 "prompt_number": 15
499 499 },
500 500 {
501 501 "cell_type": "code",
502 502 "collapsed": false,
503 503 "input": [],
504 504 "language": "python",
505 505 "metadata": {},
506 506 "outputs": [],
507 "prompt_number": 19
507 "prompt_number": 15
508 508 },
509 509 {
510 510 "cell_type": "code",
511 511 "collapsed": false,
512 512 "input": [],
513 513 "language": "python",
514 514 "metadata": {},
515 515 "outputs": [],
516 "prompt_number": 19
516 "prompt_number": 15
517 517 },
518 518 {
519 519 "cell_type": "code",
520 520 "collapsed": false,
521 521 "input": [],
522 522 "language": "python",
523 523 "metadata": {},
524 524 "outputs": [],
525 "prompt_number": 19
525 "prompt_number": 15
526 526 },
527 527 {
528 528 "cell_type": "code",
529 529 "collapsed": false,
530 530 "input": [],
531 531 "language": "python",
532 532 "metadata": {},
533 533 "outputs": [],
534 "prompt_number": 19
534 "prompt_number": 15
535 535 },
536 536 {
537 537 "cell_type": "code",
538 538 "collapsed": false,
539 539 "input": [],
540 540 "language": "python",
541 541 "metadata": {},
542 542 "outputs": [],
543 "prompt_number": 19
543 "prompt_number": 15
544 544 },
545 545 {
546 546 "cell_type": "code",
547 547 "collapsed": false,
548 548 "input": [],
549 549 "language": "python",
550 550 "metadata": {},
551 551 "outputs": [],
552 "prompt_number": 19
552 "prompt_number": 15
553 553 },
554 554 {
555 555 "cell_type": "code",
556 556 "collapsed": false,
557 557 "input": [],
558 558 "language": "python",
559 559 "metadata": {},
560 560 "outputs": [],
561 "prompt_number": 19
561 "prompt_number": 15
562 562 },
563 563 {
564 564 "cell_type": "code",
565 565 "collapsed": false,
566 566 "input": [
567 567 "counter.value += 1"
568 568 ],
569 569 "language": "python",
570 570 "metadata": {},
571 571 "outputs": [],
572 "prompt_number": 20
572 "prompt_number": 16
573 573 },
574 574 {
575 575 "cell_type": "code",
576 576 "collapsed": false,
577 577 "input": [
578 578 "popup.close()"
579 579 ],
580 580 "language": "python",
581 581 "metadata": {},
582 582 "outputs": [],
583 "prompt_number": 21
583 "prompt_number": 17
584 584 },
585 585 {
586 586 "cell_type": "heading",
587 587 "level": 1,
588 588 "metadata": {
589 589 "slideshow": {
590 590 "slide_type": "slide"
591 591 }
592 592 },
593 593 "source": [
594 594 "Alignment"
595 595 ]
596 596 },
597 597 {
598 598 "cell_type": "markdown",
599 599 "metadata": {},
600 600 "source": [
601 601 "Most widgets have a **`description` attribute**, which allows a label for the widget to be defined.\n",
602 602 "The label of the widget **has a fixed minimum width**.\n",
603 603 "The text of the label is **always right aligned and the widget is left aligned**:"
604 604 ]
605 605 },
606 606 {
607 607 "cell_type": "code",
608 608 "collapsed": false,
609 609 "input": [
610 610 "display(widgets.TextWidget(description=\"a:\"))\n",
611 611 "display(widgets.TextWidget(description=\"aa:\"))\n",
612 612 "display(widgets.TextWidget(description=\"aaa:\"))"
613 613 ],
614 614 "language": "python",
615 615 "metadata": {},
616 616 "outputs": [],
617 "prompt_number": 22
617 "prompt_number": 18
618 618 },
619 619 {
620 620 "cell_type": "markdown",
621 621 "metadata": {
622 622 "slideshow": {
623 623 "slide_type": "slide"
624 624 }
625 625 },
626 626 "source": [
627 627 "If a **label is longer** than the minimum width, the **widget is shifted to the right**:"
628 628 ]
629 629 },
630 630 {
631 631 "cell_type": "code",
632 632 "collapsed": false,
633 633 "input": [
634 634 "display(widgets.TextWidget(description=\"a:\"))\n",
635 635 "display(widgets.TextWidget(description=\"aa:\"))\n",
636 636 "display(widgets.TextWidget(description=\"aaa:\"))\n",
637 637 "display(widgets.TextWidget(description=\"aaaaaaaaaaaaaaaaaa:\"))"
638 638 ],
639 639 "language": "python",
640 640 "metadata": {},
641 641 "outputs": [],
642 "prompt_number": 23
642 "prompt_number": 19
643 643 },
644 644 {
645 645 "cell_type": "markdown",
646 646 "metadata": {
647 647 "slideshow": {
648 648 "slide_type": "slide"
649 649 }
650 650 },
651 651 "source": [
652 652 "If a `description` is **not set** for the widget, the **label is not displayed**:"
653 653 ]
654 654 },
655 655 {
656 656 "cell_type": "code",
657 657 "collapsed": false,
658 658 "input": [
659 659 "display(widgets.TextWidget(description=\"a:\"))\n",
660 660 "display(widgets.TextWidget(description=\"aa:\"))\n",
661 661 "display(widgets.TextWidget(description=\"aaa:\"))\n",
662 662 "display(widgets.TextWidget())"
663 663 ],
664 664 "language": "python",
665 665 "metadata": {},
666 666 "outputs": [],
667 "prompt_number": 24
667 "prompt_number": 20
668 668 },
669 669 {
670 670 "cell_type": "heading",
671 671 "level": 1,
672 672 "metadata": {
673 673 "slideshow": {
674 674 "slide_type": "slide"
675 675 }
676 676 },
677 677 "source": [
678 678 "DOM Classes"
679 679 ]
680 680 },
681 681 {
682 682 "cell_type": "markdown",
683 683 "metadata": {},
684 684 "source": [
685 685 "IPython defines a large number of **DOM (document object model) classes** that you can apply to your widgets. Applying a DOM class causes all of the **CSS associated with that class** to be applied to the element. Classes can be applied and removed using the **`add_class` and `remove_class`** methods **after a widget has been displayed**. The majority of DOM classes defined by IPython are actually **Bootstrap classes**. For more information on Bootstrap classes and CSS, please refer to [Bootstrap's website](http://getbootstrap.com/2.3.2/)."
686 686 ]
687 687 },
688 688 {
689 689 "cell_type": "heading",
690 690 "level": 2,
691 691 "metadata": {
692 692 "slideshow": {
693 693 "slide_type": "slide"
694 694 }
695 695 },
696 696 "source": [
697 697 "Path dependent"
698 698 ]
699 699 },
700 700 {
701 701 "cell_type": "markdown",
702 702 "metadata": {},
703 703 "source": [
704 704 "Both `add_class` and `remove_class` allow you to use **CSS selectors** to pick which sub elements of your widget get styled. Because of this, the `add_class` and `remove_class` methods are **path dependent (order specific)**. The following example shows the **same three calls** made in three **different orders** and the resulting output. **All three differ.**"
705 705 ]
706 706 },
707 707 {
708 708 "cell_type": "code",
709 709 "collapsed": false,
710 710 "input": [
711 711 "%%html\n",
712 712 "<style>\n",
713 713 " div.cube { display: inline; padding: 5px; }\n",
714 714 " div.red { background: red; }\n",
715 715 " div.blue { background: blue; }\n",
716 716 "</style>"
717 717 ],
718 718 "language": "python",
719 719 "metadata": {},
720 720 "outputs": [
721 721 {
722 722 "html": [
723 723 "<style>\n",
724 724 " div.cube { display: inline; padding: 5px; }\n",
725 725 " div.red { background: red; }\n",
726 726 " div.blue { background: blue; }\n",
727 727 "</style>"
728 728 ],
729 729 "metadata": {},
730 730 "output_type": "display_data",
731 731 "text": [
732 732 "<IPython.core.display.HTML object>"
733 733 ]
734 734 }
735 735 ],
736 "prompt_number": 25
736 "prompt_number": 21
737 737 },
738 738 {
739 739 "cell_type": "code",
740 740 "collapsed": false,
741 741 "input": [
742 742 "from IPython.html import widgets\n",
743 743 "from IPython.display import display\n",
744 744 "html = '<br />'.join([''.join(['<div class=\"cube\">x</div>' for i in range(8)]) for j in range(8)])\n",
745 745 "widget = [widgets.HTMLWidget(value=html) for i in range(3)]\n",
746 746 "\n",
747 747 "display(widget[0])\n",
748 748 "widget[0].add_class('red', 'div.cube:nth-child(even)')\n",
749 749 "widget[0].remove_class('red', 'div.red:nth-child(7n+1)')\n",
750 750 "widget[0].add_class('blue', 'div.red:nth-child(3n+1)')"
751 751 ],
752 752 "language": "python",
753 753 "metadata": {},
754 754 "outputs": [],
755 "prompt_number": 26
755 "prompt_number": 22
756 756 },
757 757 {
758 758 "cell_type": "code",
759 759 "collapsed": false,
760 760 "input": [
761 761 "display(widget[1])\n",
762 762 "widget[1].remove_class('red', 'div.red:nth-child(7n+1)')\n",
763 763 "widget[1].add_class('blue', 'div.red:nth-child(3n+1)')\n",
764 764 "widget[1].add_class('red', 'div.cube:nth-child(even)')"
765 765 ],
766 766 "language": "python",
767 767 "metadata": {},
768 768 "outputs": [],
769 "prompt_number": 27
769 "prompt_number": 23
770 770 },
771 771 {
772 772 "cell_type": "code",
773 773 "collapsed": false,
774 774 "input": [
775 775 "display(widget[2])\n",
776 776 "widget[2].add_class('red', 'div.cube:nth-child(even)')\n",
777 777 "widget[2].add_class('blue', 'div.red:nth-child(3n+1)')\n",
778 778 "widget[2].remove_class('red', 'div.red:nth-child(7n+1)')"
779 779 ],
780 780 "language": "python",
781 781 "metadata": {},
782 782 "outputs": [],
783 "prompt_number": 28
783 "prompt_number": 24
784 784 },
785 785 {
786 786 "cell_type": "heading",
787 787 "level": 2,
788 788 "metadata": {
789 789 "slideshow": {
790 790 "slide_type": "slide"
791 791 }
792 792 },
793 793 "source": [
794 794 "Alignment classes"
795 795 ]
796 796 },
797 797 {
798 798 "cell_type": "markdown",
799 799 "metadata": {},
800 800 "source": [
801 801 "Widgets can be aligned using IPython **alignment classes**. These classes should work with most widgets, but were **designed to be applied to `ContainerWidget`s**. Examples of these classes follow:"
802 802 ]
803 803 },
804 804 {
805 805 "cell_type": "heading",
806 806 "level": 3,
807 807 "metadata": {},
808 808 "source": [
809 809 "Orientation classes"
810 810 ]
811 811 },
812 812 {
813 813 "cell_type": "heading",
814 814 "level": 4,
815 815 "metadata": {},
816 816 "source": [
817 817 "\"vbox\""
818 818 ]
819 819 },
820 820 {
821 821 "cell_type": "markdown",
822 822 "metadata": {},
823 823 "source": [
824 824 "Widget containers default to this orientation.\n",
825 825 "<div class=\"example-container vbox\">\n",
826 826 "<div class=\"example-box\">A</div>\n",
827 827 "<div class=\"example-box med\">B</div>\n",
828 828 "<div class=\"example-box lrg\">C</div>\n",
829 829 "</div>"
830 830 ]
831 831 },
832 832 {
833 833 "cell_type": "heading",
834 834 "level": 4,
835 835 "metadata": {},
836 836 "source": [
837 837 "\"hbox\""
838 838 ]
839 839 },
840 840 {
841 841 "cell_type": "markdown",
842 842 "metadata": {},
843 843 "source": [
844 844 "<div class=\"example-container hbox\">\n",
845 845 "<div class=\"example-box\">A</div>\n",
846 846 "<div class=\"example-box med\">B</div>\n",
847 847 "<div class=\"example-box lrg\">C</div>\n",
848 848 "</div>"
849 849 ]
850 850 },
851 851 {
852 852 "cell_type": "heading",
853 853 "level": 3,
854 854 "metadata": {},
855 855 "source": [
856 856 "Packing classes"
857 857 ]
858 858 },
859 859 {
860 860 "cell_type": "markdown",
861 861 "metadata": {},
862 862 "source": [
863 863 "These examples use the **hbox layout** to show packing. Packing is the alignment of the widgets along the the **axis that they are displayed on**."
864 864 ]
865 865 },
866 866 {
867 867 "cell_type": "heading",
868 868 "level": 4,
869 869 "metadata": {},
870 870 "source": [
871 871 "\"start\""
872 872 ]
873 873 },
874 874 {
875 875 "cell_type": "markdown",
876 876 "metadata": {},
877 877 "source": [
878 878 "<div class=\"example-container hbox start\">\n",
879 879 "<div class=\"example-box\">A</div>\n",
880 880 "<div class=\"example-box med\">B</div>\n",
881 881 "<div class=\"example-box lrg\">C</div>\n",
882 882 "</div>"
883 883 ]
884 884 },
885 885 {
886 886 "cell_type": "heading",
887 887 "level": 4,
888 888 "metadata": {},
889 889 "source": [
890 890 "\"center\""
891 891 ]
892 892 },
893 893 {
894 894 "cell_type": "markdown",
895 895 "metadata": {},
896 896 "source": [
897 897 "<div class=\"example-container hbox center\">\n",
898 898 "<div class=\"example-box\">A</div>\n",
899 899 "<div class=\"example-box med\">B</div>\n",
900 900 "<div class=\"example-box lrg\">C</div>\n",
901 901 "</div>"
902 902 ]
903 903 },
904 904 {
905 905 "cell_type": "heading",
906 906 "level": 4,
907 907 "metadata": {},
908 908 "source": [
909 909 "\"end\""
910 910 ]
911 911 },
912 912 {
913 913 "cell_type": "markdown",
914 914 "metadata": {
915 915 "slideshow": {
916 916 "slide_type": "slide"
917 917 }
918 918 },
919 919 "source": [
920 920 "<div class=\"example-container hbox end\">\n",
921 921 "<div class=\"example-box\">A</div>\n",
922 922 "<div class=\"example-box med\">B</div>\n",
923 923 "<div class=\"example-box lrg\">C</div>\n",
924 924 "</div>"
925 925 ]
926 926 },
927 927 {
928 928 "cell_type": "heading",
929 929 "level": 3,
930 930 "metadata": {},
931 931 "source": [
932 932 "Aligning classes"
933 933 ]
934 934 },
935 935 {
936 936 "cell_type": "markdown",
937 937 "metadata": {},
938 938 "source": [
939 939 "These examples use the **hbox layout** to show alignment. Packing is the alignment of the widgets along the the **axis perpendicular to the one that they are displayed on**."
940 940 ]
941 941 },
942 942 {
943 943 "cell_type": "heading",
944 944 "level": 4,
945 945 "metadata": {},
946 946 "source": [
947 947 "\"align-start\""
948 948 ]
949 949 },
950 950 {
951 951 "cell_type": "markdown",
952 952 "metadata": {},
953 953 "source": [
954 954 "<div class=\"example-container hbox align-start\">\n",
955 955 "<div class=\"example-box\">A</div>\n",
956 956 "<div class=\"example-box med\">B</div>\n",
957 957 "<div class=\"example-box lrg\">C</div>\n",
958 958 "</div>"
959 959 ]
960 960 },
961 961 {
962 962 "cell_type": "heading",
963 963 "level": 4,
964 964 "metadata": {},
965 965 "source": [
966 966 "\"align-center\""
967 967 ]
968 968 },
969 969 {
970 970 "cell_type": "markdown",
971 971 "metadata": {},
972 972 "source": [
973 973 "<div class=\"example-container hbox align-center\">\n",
974 974 "<div class=\"example-box\">A</div>\n",
975 975 "<div class=\"example-box med\">B</div>\n",
976 976 "<div class=\"example-box lrg\">C</div>\n",
977 977 "</div>"
978 978 ]
979 979 },
980 980 {
981 981 "cell_type": "heading",
982 982 "level": 4,
983 983 "metadata": {},
984 984 "source": [
985 985 "\"align-end\""
986 986 ]
987 987 },
988 988 {
989 989 "cell_type": "markdown",
990 990 "metadata": {
991 991 "slideshow": {
992 992 "slide_type": "slide"
993 993 }
994 994 },
995 995 "source": [
996 996 "<div class=\"example-container hbox align-end\">\n",
997 997 "<div class=\"example-box\">A</div>\n",
998 998 "<div class=\"example-box med\">B</div>\n",
999 999 "<div class=\"example-box lrg\">C</div>\n",
1000 1000 "</div>"
1001 1001 ]
1002 1002 },
1003 1003 {
1004 1004 "cell_type": "heading",
1005 1005 "level": 3,
1006 1006 "metadata": {},
1007 1007 "source": [
1008 1008 "Flex classes"
1009 1009 ]
1010 1010 },
1011 1011 {
1012 1012 "cell_type": "markdown",
1013 1013 "metadata": {},
1014 1014 "source": [
1015 1015 "To specify **how \"greedy\" a container is** when filling in the remaining space of its parent, the **`box-flexN`** classes are used (where N is 0, 1, or 2). The **higher the value of N, the more greedy** the child is. **`box-flex0` is the default behavior**, which is to not fill the parent."
1016 1016 ]
1017 1017 },
1018 1018 {
1019 1019 "cell_type": "heading",
1020 1020 "level": 4,
1021 1021 "metadata": {},
1022 1022 "source": [
1023 1023 "Example 1"
1024 1024 ]
1025 1025 },
1026 1026 {
1027 1027 "cell_type": "markdown",
1028 1028 "metadata": {},
1029 1029 "source": [
1030 1030 "<div class=\"example-container sm hbox center\">\n",
1031 1031 "<div class=\"example-box box-flex0\">box-flex0</div>\n",
1032 1032 "<div class=\"example-box box-flex0\">box-flex0</div>\n",
1033 1033 "<div class=\"example-box box-flex0\">box-flex0</div>\n",
1034 1034 "</div>"
1035 1035 ]
1036 1036 },
1037 1037 {
1038 1038 "cell_type": "heading",
1039 1039 "level": 4,
1040 1040 "metadata": {},
1041 1041 "source": [
1042 1042 "Example 2"
1043 1043 ]
1044 1044 },
1045 1045 {
1046 1046 "cell_type": "markdown",
1047 1047 "metadata": {},
1048 1048 "source": [
1049 1049 "<div class=\"example-container sm hbox center\">\n",
1050 1050 "<div class=\"example-box box-flex0\">box-flex0</div>\n",
1051 1051 "<div class=\"example-box box-flex1\">box-flex1</div>\n",
1052 1052 "<div class=\"example-box box-flex0\">box-flex0</div>\n",
1053 1053 "</div>"
1054 1054 ]
1055 1055 },
1056 1056 {
1057 1057 "cell_type": "heading",
1058 1058 "level": 4,
1059 1059 "metadata": {},
1060 1060 "source": [
1061 1061 "Example 3"
1062 1062 ]
1063 1063 },
1064 1064 {
1065 1065 "cell_type": "markdown",
1066 1066 "metadata": {},
1067 1067 "source": [
1068 1068 "<div class=\"example-container sm hbox center\">\n",
1069 1069 "<div class=\"example-box box-flex0\">box-flex0</div>\n",
1070 1070 "<div class=\"example-box box-flex1\">box-flex1</div>\n",
1071 1071 "<div class=\"example-box box-flex1\">box-flex1</div>\n",
1072 1072 "</div>"
1073 1073 ]
1074 1074 },
1075 1075 {
1076 1076 "cell_type": "heading",
1077 1077 "level": 4,
1078 1078 "metadata": {},
1079 1079 "source": [
1080 1080 "Example 4"
1081 1081 ]
1082 1082 },
1083 1083 {
1084 1084 "cell_type": "markdown",
1085 1085 "metadata": {},
1086 1086 "source": [
1087 1087 "<div class=\"example-container sm hbox center\">\n",
1088 1088 "<div class=\"example-box box-flex1\">box-flex1</div>\n",
1089 1089 "<div class=\"example-box box-flex1\">box-flex1</div>\n",
1090 1090 "<div class=\"example-box box-flex1\">box-flex1</div>\n",
1091 1091 "</div>"
1092 1092 ]
1093 1093 },
1094 1094 {
1095 1095 "cell_type": "heading",
1096 1096 "level": 4,
1097 1097 "metadata": {},
1098 1098 "source": [
1099 1099 "Example 5"
1100 1100 ]
1101 1101 },
1102 1102 {
1103 1103 "cell_type": "markdown",
1104 1104 "metadata": {},
1105 1105 "source": [
1106 1106 "<div class=\"example-container sm hbox center\">\n",
1107 1107 "<div class=\"example-box box-flex2\">box-flex2</div>\n",
1108 1108 "<div class=\"example-box box-flex1\">box-flex1</div>\n",
1109 1109 "<div class=\"example-box box-flex1\">box-flex1</div>\n",
1110 1110 "</div>"
1111 1111 ]
1112 1112 },
1113 1113 {
1114 1114 "cell_type": "heading",
1115 1115 "level": 4,
1116 1116 "metadata": {},
1117 1117 "source": [
1118 1118 "Example 6"
1119 1119 ]
1120 1120 },
1121 1121 {
1122 1122 "cell_type": "markdown",
1123 1123 "metadata": {
1124 1124 "slideshow": {
1125 1125 "slide_type": "slide"
1126 1126 }
1127 1127 },
1128 1128 "source": [
1129 1129 "<div class=\"example-container sm hbox center\">\n",
1130 1130 "<div class=\"example-box box-flex0\">box-flex0</div>\n",
1131 1131 "<div class=\"example-box box-flex1\">box-flex1</div>\n",
1132 1132 "<div class=\"example-box box-flex2\">box-flex2</div>\n",
1133 1133 "</div>"
1134 1134 ]
1135 1135 },
1136 1136 {
1137 1137 "cell_type": "heading",
1138 1138 "level": 3,
1139 1139 "metadata": {
1140 1140 "slideshow": {
1141 1141 "slide_type": "slide"
1142 1142 }
1143 1143 },
1144 1144 "source": [
1145 1145 "Application to widgets"
1146 1146 ]
1147 1147 },
1148 1148 {
1149 1149 "cell_type": "markdown",
1150 1150 "metadata": {},
1151 1151 "source": [
1152 1152 "Widget containers **default to vbox** alignment."
1153 1153 ]
1154 1154 },
1155 1155 {
1156 1156 "cell_type": "code",
1157 1157 "collapsed": false,
1158 1158 "input": [
1159 1159 "buttons = [widgets.ButtonWidget(description=str(i)) for i in range(3)]\n",
1160 1160 "\n",
1161 1161 "container = widgets.ContainerWidget(children=buttons)\n",
1162 1162 "display(container)"
1163 1163 ],
1164 1164 "language": "python",
1165 1165 "metadata": {},
1166 1166 "outputs": [],
1167 "prompt_number": 29
1167 "prompt_number": 25
1168 1168 },
1169 1169 {
1170 1170 "cell_type": "heading",
1171 1171 "level": 3,
1172 1172 "metadata": {
1173 1173 "slideshow": {
1174 1174 "slide_type": "slide"
1175 1175 }
1176 1176 },
1177 1177 "source": [
1178 1178 "Using hbox"
1179 1179 ]
1180 1180 },
1181 1181 {
1182 1182 "cell_type": "markdown",
1183 1183 "metadata": {},
1184 1184 "source": [
1185 1185 "To make a widget container display its widgets horizontally, you need to **remove the `vbox` class** from the container and **add the `hbox` class** in its place."
1186 1186 ]
1187 1187 },
1188 1188 {
1189 1189 "cell_type": "code",
1190 1190 "collapsed": false,
1191 1191 "input": [
1192 1192 "container = widgets.ContainerWidget(children=buttons)\n",
1193 1193 "display(container)\n",
1194 1194 "container.remove_class('vbox')\n",
1195 1195 "container.add_class('hbox')"
1196 1196 ],
1197 1197 "language": "python",
1198 1198 "metadata": {},
1199 1199 "outputs": [],
1200 "prompt_number": 30
1200 "prompt_number": 26
1201 1201 },
1202 1202 {
1203 1203 "cell_type": "markdown",
1204 1204 "metadata": {},
1205 1205 "source": [
1206 1206 "By setting the width of the container to 100% and adding the `center` class to it, you can center the buttons."
1207 1207 ]
1208 1208 },
1209 1209 {
1210 1210 "cell_type": "code",
1211 1211 "collapsed": false,
1212 1212 "input": [
1213 1213 "container.set_css('width', '100%')\n",
1214 1214 "container.add_class('center')"
1215 1215 ],
1216 1216 "language": "python",
1217 1217 "metadata": {},
1218 1218 "outputs": [],
1219 "prompt_number": 31
1219 "prompt_number": 27
1220 1220 },
1221 1221 {
1222 1222 "cell_type": "heading",
1223 1223 "level": 2,
1224 1224 "metadata": {
1225 1225 "slideshow": {
1226 1226 "slide_type": "slide"
1227 1227 }
1228 1228 },
1229 1229 "source": [
1230 1230 "Style classes"
1231 1231 ]
1232 1232 },
1233 1233 {
1234 1234 "cell_type": "markdown",
1235 1235 "metadata": {},
1236 1236 "source": [
1237 1237 "In addition to alignment classes, the classes defined by Bootstrap can also be used. This tutorial will only cover a few of the most common classes. For a full list of Bootstrap classes, please refer to [Bootstrap's website](http://getbootstrap.com/2.3.2/)."
1238 1238 ]
1239 1239 },
1240 1240 {
1241 1241 "cell_type": "heading",
1242 1242 "level": 3,
1243 1243 "metadata": {},
1244 1244 "source": [
1245 1245 "ButtonWidgets"
1246 1246 ]
1247 1247 },
1248 1248 {
1249 1249 "cell_type": "code",
1250 1250 "collapsed": false,
1251 1251 "input": [
1252 1252 "# List of the bootstrap button styles\n",
1253 1253 "classes = [\n",
1254 1254 " 'btn', \n",
1255 1255 " 'btn-primary', \n",
1256 1256 " 'btn-info', \n",
1257 1257 " 'btn-success', \n",
1258 1258 " 'btn-warning', \n",
1259 1259 " 'btn-danger', \n",
1260 1260 " 'btn-inverse', \n",
1261 1261 " 'btn-link'\n",
1262 1262 "]\n",
1263 1263 "\n",
1264 1264 "# Display the buttons in a hbox\n",
1265 1265 "container = widgets.ContainerWidget(children=[widgets.ButtonWidget(description=c) for c in classes])\n",
1266 1266 "display(container)\n",
1267 1267 "\n",
1268 1268 "# Apply classes after display\n",
1269 1269 "container.remove_class('vbox')\n",
1270 1270 "container.add_class('hbox')\n",
1271 1271 "ret = [container.children[i].add_class(c) for i, c in enumerate(classes)]"
1272 1272 ],
1273 1273 "language": "python",
1274 1274 "metadata": {},
1275 1275 "outputs": [],
1276 "prompt_number": 32
1276 "prompt_number": 28
1277 1277 },
1278 1278 {
1279 1279 "cell_type": "heading",
1280 1280 "level": 3,
1281 1281 "metadata": {
1282 1282 "slideshow": {
1283 1283 "slide_type": "slide"
1284 1284 }
1285 1285 },
1286 1286 "source": [
1287 1287 "ContainerWidgets"
1288 1288 ]
1289 1289 },
1290 1290 {
1291 1291 "cell_type": "code",
1292 1292 "collapsed": false,
1293 1293 "input": [
1294 1294 "def create_label(cls):\n",
1295 1295 " class_name = widgets.HTMLWidget(value=cls)\n",
1296 1296 " container = widgets.ContainerWidget(children=[class_name])\n",
1297 1297 " display(container)\n",
1298 1298 " container.add_class(cls)\n",
1299 1299 "\n",
1300 1300 "ret = [create_label(c) for c in [\n",
1301 1301 " 'alert', \n",
1302 1302 " 'alert alert-error', \n",
1303 1303 " 'alert alert-success', \n",
1304 1304 " 'alert alert-info'\n",
1305 1305 "]]"
1306 1306 ],
1307 1307 "language": "python",
1308 1308 "metadata": {},
1309 1309 "outputs": [],
1310 "prompt_number": 33
1310 "prompt_number": 29
1311 1311 },
1312 1312 {
1313 1313 "cell_type": "heading",
1314 1314 "level": 3,
1315 1315 "metadata": {
1316 1316 "slideshow": {
1317 1317 "slide_type": "slide"
1318 1318 }
1319 1319 },
1320 1320 "source": [
1321 1321 "ProgressWidgets"
1322 1322 ]
1323 1323 },
1324 1324 {
1325 1325 "cell_type": "code",
1326 1326 "collapsed": false,
1327 1327 "input": [
1328 1328 "classes = [\n",
1329 " 'progress-bar-info', \n",
1330 " 'progress-bar-success', \n",
1331 " 'progress-bar-warning', \n",
1332 " 'progress-bar-danger',\n",
1333 " 'progress-bar-info progress-striped', \n",
1334 " 'progress-bar-success progress-striped', \n",
1335 " 'progress-bar-warning progress-striped', \n",
1336 " 'progress-bar-danger progress-striped',\n",
1337 " 'active progress-bar-info progress-striped', \n",
1338 " 'active progress-bar-success progress-striped', \n",
1339 " 'active progress-bar-warning progress-striped', \n",
1340 " 'active progress-bar-danger progress-striped',\n",
1329 " 'progress-info', \n",
1330 " 'progress-success', \n",
1331 " 'progress-warning', \n",
1332 " 'progress-danger',\n",
1333 " 'progress-info progress-striped', \n",
1334 " 'progress-success progress-striped', \n",
1335 " 'progress-warning progress-striped', \n",
1336 " 'progress-danger progress-striped',\n",
1337 " 'active progress-info progress-striped', \n",
1338 " 'active progress-success progress-striped', \n",
1339 " 'active progress-warning progress-striped', \n",
1340 " 'active progress-danger progress-striped',\n",
1341 1341 "]\n",
1342 1342 "ws = [widgets.IntProgressWidget(value=50, description=c) for c in classes]\n",
1343 1343 "ret = [display(w) for w in ws]\n",
1344 "ret = [ws[i].add_class(c, selector=\"\" if c == \"progress-striped\" or c == \"active\" else \".progress-bar\") for i, cs in enumerate(classes) for c in cs.split(' ')]"
1344 "ret = [ws[i].add_class(c) for i, cs in enumerate(classes) for c in cs.split(' ')]"
1345 1345 ],
1346 1346 "language": "python",
1347 1347 "metadata": {},
1348 1348 "outputs": [],
1349 "prompt_number": 48
1349 "prompt_number": 31
1350 1350 },
1351 1351 {
1352 1352 "cell_type": "heading",
1353 1353 "level": 2,
1354 1354 "metadata": {
1355 1355 "slideshow": {
1356 1356 "slide_type": "slide"
1357 1357 }
1358 1358 },
1359 1359 "source": [
1360 1360 "Visibility"
1361 1361 ]
1362 1362 },
1363 1363 {
1364 1364 "cell_type": "markdown",
1365 1365 "metadata": {},
1366 1366 "source": [
1367 1367 "Sometimes it is necessary to **hide or show widgets** in place, **without having to re-display** the widget.\n",
1368 1368 "The `visibility` property of widgets can be used to hide or show **widgets that have already been displayed** (as seen below)."
1369 1369 ]
1370 1370 },
1371 1371 {
1372 1372 "cell_type": "code",
1373 1373 "collapsed": false,
1374 1374 "input": [
1375 1375 "string = widgets.LatexWidget(value=\"Hello World!\")\n",
1376 1376 "display(string) "
1377 1377 ],
1378 1378 "language": "python",
1379 1379 "metadata": {},
1380 1380 "outputs": [],
1381 "prompt_number": 49
1381 "prompt_number": 32
1382 1382 },
1383 1383 {
1384 1384 "cell_type": "code",
1385 1385 "collapsed": false,
1386 1386 "input": [
1387 1387 "string.visible=False"
1388 1388 ],
1389 1389 "language": "python",
1390 1390 "metadata": {},
1391 1391 "outputs": [],
1392 "prompt_number": 50
1392 "prompt_number": 33
1393 1393 },
1394 1394 {
1395 1395 "cell_type": "code",
1396 1396 "collapsed": false,
1397 1397 "input": [
1398 1398 "string.visible=True"
1399 1399 ],
1400 1400 "language": "python",
1401 1401 "metadata": {},
1402 1402 "outputs": [],
1403 "prompt_number": 51
1403 "prompt_number": 34
1404 1404 },
1405 1405 {
1406 1406 "cell_type": "heading",
1407 1407 "level": 3,
1408 1408 "metadata": {
1409 1409 "slideshow": {
1410 1410 "slide_type": "slide"
1411 1411 }
1412 1412 },
1413 1413 "source": [
1414 1414 "Another example"
1415 1415 ]
1416 1416 },
1417 1417 {
1418 1418 "cell_type": "markdown",
1419 1419 "metadata": {},
1420 1420 "source": [
1421 1421 "In the example below, a form is rendered, which conditionally displays widgets depending on the state of other widgets. Try toggling the student checkbox."
1422 1422 ]
1423 1423 },
1424 1424 {
1425 1425 "cell_type": "code",
1426 1426 "collapsed": false,
1427 1427 "input": [
1428 1428 "form = widgets.ContainerWidget()\n",
1429 1429 "first = widgets.TextWidget(description=\"First Name:\")\n",
1430 1430 "last = widgets.TextWidget(description=\"Last Name:\")\n",
1431 1431 "\n",
1432 1432 "student = widgets.CheckboxWidget(description=\"Student:\", value=False)\n",
1433 1433 "school_info = widgets.ContainerWidget(visible=False, children=[\n",
1434 1434 " widgets.TextWidget(description=\"School:\"),\n",
1435 1435 " widgets.IntTextWidget(description=\"Grade:\", min=0, max=12)\n",
1436 1436 " ])\n",
1437 1437 "\n",
1438 1438 "pet = widgets.TextWidget(description=\"Pet's Name:\")\n",
1439 1439 "form.children = [first, last, student, school_info, pet]\n",
1440 1440 "display(form)\n",
1441 1441 "\n",
1442 1442 "def on_student_toggle(name, value):\n",
1443 1443 " if value:\n",
1444 1444 " school_info.visible = True\n",
1445 1445 " else:\n",
1446 1446 " school_info.visible = False\n",
1447 1447 "student.on_trait_change(on_student_toggle, 'value')\n"
1448 1448 ],
1449 1449 "language": "python",
1450 1450 "metadata": {},
1451 1451 "outputs": [],
1452 "prompt_number": 52
1452 "prompt_number": 35
1453 1453 },
1454 1454 {
1455 1455 "cell_type": "markdown",
1456 1456 "metadata": {},
1457 1457 "source": [
1458 1458 "[Index](Index.ipynb) - [Back](Widget Events.ipynb) - [Next](Custom Widget - Hello World.ipynb)"
1459 1459 ]
1460 1460 }
1461 1461 ],
1462 1462 "metadata": {}
1463 1463 }
1464 1464 ]
1465 1465 } No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now