##// END OF EJS Templates
Update examples, fixing bugs found in review.
Jonathan Frederic -
Show More
@@ -1,797 +1,796 b''
1 {
1 {
2 "metadata": {
2 "metadata": {
3 "celltoolbar": "Slideshow",
4 "kernelspec": {
3 "kernelspec": {
5 "codemirror_mode": {
4 "codemirror_mode": {
6 "name": "python",
5 "name": "python",
7 "version": 2
6 "version": 2
8 },
7 },
9 "display_name": "Python 2",
8 "display_name": "Python 2",
10 "language": "python",
9 "language": "python",
11 "name": "python2"
10 "name": "python2"
12 },
11 },
13 "name": "",
12 "name": "",
14 "signature": "sha256:ebbb9b368e8b07ec14fd5ee494586e605be36631657f2a9d9a6475ead41a3e04"
13 "signature": "sha256:608e0df06fe91ef1c013fd236dda33f7ae11019453f72ae446e8ce15bce82eb2"
15 },
14 },
16 "nbformat": 3,
15 "nbformat": 3,
17 "nbformat_minor": 0,
16 "nbformat_minor": 0,
18 "worksheets": [
17 "worksheets": [
19 {
18 {
20 "cells": [
19 "cells": [
21 {
20 {
22 "cell_type": "markdown",
21 "cell_type": "markdown",
23 "metadata": {},
22 "metadata": {},
24 "source": [
23 "source": [
25 "[Index](Index.ipynb) - [Back](Widget Styling.ipynb)"
24 "[Index](Index.ipynb) - [Back](Widget Styling.ipynb)"
26 ]
25 ]
27 },
26 },
28 {
27 {
29 "cell_type": "code",
28 "cell_type": "code",
30 "collapsed": false,
29 "collapsed": false,
31 "input": [
30 "input": [
32 "from __future__ import print_function # For py 2.7 compat"
31 "from __future__ import print_function # For py 2.7 compat"
33 ],
32 ],
34 "language": "python",
33 "language": "python",
35 "metadata": {},
34 "metadata": {},
36 "outputs": []
35 "outputs": []
37 },
36 },
38 {
37 {
39 "cell_type": "heading",
38 "cell_type": "heading",
40 "level": 1,
39 "level": 1,
41 "metadata": {
40 "metadata": {
42 "slideshow": {
41 "slideshow": {
43 "slide_type": "slide"
42 "slide_type": "slide"
44 }
43 }
45 },
44 },
46 "source": [
45 "source": [
47 "Building a Custom Widget"
46 "Building a Custom Widget"
48 ]
47 ]
49 },
48 },
50 {
49 {
51 "cell_type": "markdown",
50 "cell_type": "markdown",
52 "metadata": {},
51 "metadata": {},
53 "source": [
52 "source": [
54 "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",
53 "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",
55 "\n",
54 "\n",
56 "![Widget layer](images/WidgetArch.png)\n",
55 "![Widget layer](images/WidgetArch.png)\n",
57 "\n",
56 "\n",
58 "To create a custom widget, you need to **define the widget both in the back-end and in the front-end**. "
57 "To create a custom widget, you need to **define the widget both in the back-end and in the front-end**. "
59 ]
58 ]
60 },
59 },
61 {
60 {
62 "cell_type": "heading",
61 "cell_type": "heading",
63 "level": 1,
62 "level": 1,
64 "metadata": {
63 "metadata": {
65 "slideshow": {
64 "slideshow": {
66 "slide_type": "slide"
65 "slide_type": "slide"
67 }
66 }
68 },
67 },
69 "source": [
68 "source": [
70 "Building a Custom Widget"
69 "Building a Custom Widget"
71 ]
70 ]
72 },
71 },
73 {
72 {
74 "cell_type": "markdown",
73 "cell_type": "markdown",
75 "metadata": {},
74 "metadata": {},
76 "source": [
75 "source": [
77 "To get started, you'll create a **simple hello world widget**. Later you'll build on this foundation to make more complex widgets."
76 "To get started, you'll create a **simple hello world widget**. Later you'll build on this foundation to make more complex widgets."
78 ]
77 ]
79 },
78 },
80 {
79 {
81 "cell_type": "heading",
80 "cell_type": "heading",
82 "level": 2,
81 "level": 2,
83 "metadata": {
82 "metadata": {
84 "slideshow": {
83 "slideshow": {
85 "slide_type": "slide"
84 "slide_type": "slide"
86 }
85 }
87 },
86 },
88 "source": [
87 "source": [
89 "Back-end (Python)"
88 "Back-end (Python)"
90 ]
89 ]
91 },
90 },
92 {
91 {
93 "cell_type": "heading",
92 "cell_type": "heading",
94 "level": 3,
93 "level": 3,
95 "metadata": {},
94 "metadata": {},
96 "source": [
95 "source": [
97 "DOMWidget and Widget"
96 "DOMWidget and Widget"
98 ]
97 ]
99 },
98 },
100 {
99 {
101 "cell_type": "markdown",
100 "cell_type": "markdown",
102 "metadata": {},
101 "metadata": {},
103 "source": [
102 "source": [
104 "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."
103 "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."
105 ]
104 ]
106 },
105 },
107 {
106 {
108 "cell_type": "heading",
107 "cell_type": "heading",
109 "level": 3,
108 "level": 3,
110 "metadata": {
109 "metadata": {
111 "slideshow": {
110 "slideshow": {
112 "slide_type": "slide"
111 "slide_type": "slide"
113 }
112 }
114 },
113 },
115 "source": [
114 "source": [
116 "_view_name"
115 "_view_name"
117 ]
116 ]
118 },
117 },
119 {
118 {
120 "cell_type": "markdown",
119 "cell_type": "markdown",
121 "metadata": {},
120 "metadata": {},
122 "source": [
121 "source": [
123 "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)."
122 "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)."
124 ]
123 ]
125 },
124 },
126 {
125 {
127 "cell_type": "code",
126 "cell_type": "code",
128 "collapsed": false,
127 "collapsed": false,
129 "input": [
128 "input": [
130 "from IPython.html import widgets\n",
129 "from IPython.html import widgets\n",
131 "from IPython.utils.traitlets import Unicode\n",
130 "from IPython.utils.traitlets import Unicode\n",
132 "\n",
131 "\n",
133 "class HelloWidget(widgets.DOMWidget):\n",
132 "class HelloWidget(widgets.DOMWidget):\n",
134 " _view_name = Unicode('HelloView', sync=True)"
133 " _view_name = Unicode('HelloView', sync=True)"
135 ],
134 ],
136 "language": "python",
135 "language": "python",
137 "metadata": {},
136 "metadata": {},
138 "outputs": []
137 "outputs": []
139 },
138 },
140 {
139 {
141 "cell_type": "heading",
140 "cell_type": "heading",
142 "level": 3,
141 "level": 3,
143 "metadata": {
142 "metadata": {
144 "slideshow": {
143 "slideshow": {
145 "slide_type": "slide"
144 "slide_type": "slide"
146 }
145 }
147 },
146 },
148 "source": [
147 "source": [
149 "sync=True traitlets"
148 "sync=True traitlets"
150 ]
149 ]
151 },
150 },
152 {
151 {
153 "cell_type": "markdown",
152 "cell_type": "markdown",
154 "metadata": {},
153 "metadata": {},
155 "source": [
154 "source": [
156 "**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`."
155 "**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`."
157 ]
156 ]
158 },
157 },
159 {
158 {
160 "cell_type": "heading",
159 "cell_type": "heading",
161 "level": 3,
160 "level": 3,
162 "metadata": {
161 "metadata": {
163 "slideshow": {
162 "slideshow": {
164 "slide_type": "slide"
163 "slide_type": "slide"
165 }
164 }
166 },
165 },
167 "source": [
166 "source": [
168 "Other traitlet types"
167 "Other traitlet types"
169 ]
168 ]
170 },
169 },
171 {
170 {
172 "cell_type": "markdown",
171 "cell_type": "markdown",
173 "metadata": {},
172 "metadata": {},
174 "source": [
173 "source": [
175 "Unicode, used for _view_name, is not the only Traitlet type, there are many more some of which are listed below: \n",
174 "Unicode, used for _view_name, is not the only Traitlet type, there are many more some of which are listed below: \n",
176 "\n",
175 "\n",
177 "- Any\n",
176 "- Any\n",
178 "- Bool\n",
177 "- Bool\n",
179 "- Bytes\n",
178 "- Bytes\n",
180 "- CBool\n",
179 "- CBool\n",
181 "- CBytes\n",
180 "- CBytes\n",
182 "- CComplex\n",
181 "- CComplex\n",
183 "- CFloat\n",
182 "- CFloat\n",
184 "- CInt\n",
183 "- CInt\n",
185 "- CLong\n",
184 "- CLong\n",
186 "- CRegExp\n",
185 "- CRegExp\n",
187 "- CUnicode\n",
186 "- CUnicode\n",
188 "- CaselessStrEnum\n",
187 "- CaselessStrEnum\n",
189 "- Complex\n",
188 "- Complex\n",
190 "- Dict\n",
189 "- Dict\n",
191 "- DottedObjectName\n",
190 "- DottedObjectName\n",
192 "- Enum\n",
191 "- Enum\n",
193 "- Float\n",
192 "- Float\n",
194 "- FunctionType\n",
193 "- FunctionType\n",
195 "- Instance\n",
194 "- Instance\n",
196 "- InstanceType\n",
195 "- InstanceType\n",
197 "- Int\n",
196 "- Int\n",
198 "- List\n",
197 "- List\n",
199 "- Long\n",
198 "- Long\n",
200 "- Set\n",
199 "- Set\n",
201 "- TCPAddress\n",
200 "- TCPAddress\n",
202 "- Tuple\n",
201 "- Tuple\n",
203 "- Type\n",
202 "- Type\n",
204 "- Unicode\n",
203 "- Unicode\n",
205 "\n",
204 "\n",
206 "**Not all of these traitlets can be synchronized** across the network, **only the JSON-able** traits and **Widget instances** will be synchronized."
205 "**Not all of these traitlets can be synchronized** across the network, **only the JSON-able** traits and **Widget instances** will be synchronized."
207 ]
206 ]
208 },
207 },
209 {
208 {
210 "cell_type": "heading",
209 "cell_type": "heading",
211 "level": 2,
210 "level": 2,
212 "metadata": {
211 "metadata": {
213 "slideshow": {
212 "slideshow": {
214 "slide_type": "slide"
213 "slide_type": "slide"
215 }
214 }
216 },
215 },
217 "source": [
216 "source": [
218 "Front-end (JavaScript)"
217 "Front-end (JavaScript)"
219 ]
218 ]
220 },
219 },
221 {
220 {
222 "cell_type": "heading",
221 "cell_type": "heading",
223 "level": 3,
222 "level": 3,
224 "metadata": {},
223 "metadata": {},
225 "source": [
224 "source": [
226 "Models and views"
225 "Models and views"
227 ]
226 ]
228 },
227 },
229 {
228 {
230 "cell_type": "markdown",
229 "cell_type": "markdown",
231 "metadata": {},
230 "metadata": {},
232 "source": [
231 "source": [
233 "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**."
232 "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**."
234 ]
233 ]
235 },
234 },
236 {
235 {
237 "cell_type": "heading",
236 "cell_type": "heading",
238 "level": 3,
237 "level": 3,
239 "metadata": {
238 "metadata": {
240 "slideshow": {
239 "slideshow": {
241 "slide_type": "slide"
240 "slide_type": "slide"
242 }
241 }
243 },
242 },
244 "source": [
243 "source": [
245 "Import the WidgetManager"
244 "Import the WidgetManager"
246 ]
245 ]
247 },
246 },
248 {
247 {
249 "cell_type": "markdown",
248 "cell_type": "markdown",
250 "metadata": {},
249 "metadata": {},
251 "source": [
250 "source": [
252 "You first need to **import the `widget` and `manager` modules**. You will use it later to register your view by name (the same name you used in the back-end). To import the modules, use the `require` method of [require.js](http://requirejs.org/) (as seen below)."
251 "You first need to **import the `widget` and `manager` modules**. You will use it later to register your view by name (the same name you used in the back-end). To import the modules, use the `require` method of [require.js](http://requirejs.org/) (as seen below)."
253 ]
252 ]
254 },
253 },
255 {
254 {
256 "cell_type": "code",
255 "cell_type": "code",
257 "collapsed": false,
256 "collapsed": false,
258 "input": [
257 "input": [
259 "%%javascript\n",
258 "%%javascript\n",
260 "require([\"widgets/js/widget\", \"widgets/js/manager\"], function(widget, manager){\n",
259 "require([\"widgets/js/widget\", \"widgets/js/manager\"], function(widget, manager){\n",
261 " \n",
260 " \n",
262 "});"
261 "});"
263 ],
262 ],
264 "language": "python",
263 "language": "python",
265 "metadata": {},
264 "metadata": {},
266 "outputs": []
265 "outputs": []
267 },
266 },
268 {
267 {
269 "cell_type": "heading",
268 "cell_type": "heading",
270 "level": 3,
269 "level": 3,
271 "metadata": {
270 "metadata": {
272 "slideshow": {
271 "slideshow": {
273 "slide_type": "slide"
272 "slide_type": "slide"
274 }
273 }
275 },
274 },
276 "source": [
275 "source": [
277 "Define the view"
276 "Define the view"
278 ]
277 ]
279 },
278 },
280 {
279 {
281 "cell_type": "markdown",
280 "cell_type": "markdown",
282 "metadata": {},
281 "metadata": {},
283 "source": [
282 "source": [
284 "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**."
283 "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**."
285 ]
284 ]
286 },
285 },
287 {
286 {
288 "cell_type": "code",
287 "cell_type": "code",
289 "collapsed": false,
288 "collapsed": false,
290 "input": [
289 "input": [
291 "%%javascript\n",
290 "%%javascript\n",
292 "require([\"widgets/js/widget\", \"widgets/js/manager\"], function(widget, manager){\n",
291 "require([\"widgets/js/widget\", \"widgets/js/manager\"], function(widget, manager){\n",
293 " \n",
292 " \n",
294 " // Define the HelloView\n",
293 " // Define the HelloView\n",
295 " var HelloView = widget.DOMWidgetView.extend({\n",
294 " var HelloView = widget.DOMWidgetView.extend({\n",
296 " \n",
295 " \n",
297 " });\n",
296 " });\n",
298 " \n",
297 " \n",
299 " // Register the HelloView with the widget manager.\n",
298 " // Register the HelloView with the widget manager.\n",
300 " manager.WidgetManager.register_widget_view('HelloView', HelloView);\n",
299 " manager.WidgetManager.register_widget_view('HelloView', HelloView);\n",
301 "});"
300 "});"
302 ],
301 ],
303 "language": "python",
302 "language": "python",
304 "metadata": {},
303 "metadata": {},
305 "outputs": []
304 "outputs": []
306 },
305 },
307 {
306 {
308 "cell_type": "heading",
307 "cell_type": "heading",
309 "level": 3,
308 "level": 3,
310 "metadata": {
309 "metadata": {
311 "slideshow": {
310 "slideshow": {
312 "slide_type": "slide"
311 "slide_type": "slide"
313 }
312 }
314 },
313 },
315 "source": [
314 "source": [
316 "Render method"
315 "Render method"
317 ]
316 ]
318 },
317 },
319 {
318 {
320 "cell_type": "markdown",
319 "cell_type": "markdown",
321 "metadata": {},
320 "metadata": {},
322 "source": [
321 "source": [
323 "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)."
322 "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)."
324 ]
323 ]
325 },
324 },
326 {
325 {
327 "cell_type": "code",
326 "cell_type": "code",
328 "collapsed": false,
327 "collapsed": false,
329 "input": [
328 "input": [
330 "%%javascript\n",
329 "%%javascript\n",
331 "require([\"widgets/js/widget\", \"widgets/js/manager\"], function(widget, manager){\n",
330 "require([\"widgets/js/widget\", \"widgets/js/manager\"], function(widget, manager){\n",
332 " \n",
331 " \n",
333 " var HelloView = widget.DOMWidgetView.extend({\n",
332 " var HelloView = widget.DOMWidgetView.extend({\n",
334 " \n",
333 " \n",
335 " // Render the view.\n",
334 " // Render the view.\n",
336 " render: function(){ \n",
335 " render: function(){ \n",
337 " this.$el.text('Hello World!'); \n",
336 " this.$el.text('Hello World!'); \n",
338 " },\n",
337 " },\n",
339 " });\n",
338 " });\n",
340 " \n",
339 " \n",
341 " manager.WidgetManager.register_widget_view('HelloView', HelloView);\n",
340 " manager.WidgetManager.register_widget_view('HelloView', HelloView);\n",
342 "});"
341 "});"
343 ],
342 ],
344 "language": "python",
343 "language": "python",
345 "metadata": {},
344 "metadata": {},
346 "outputs": []
345 "outputs": []
347 },
346 },
348 {
347 {
349 "cell_type": "heading",
348 "cell_type": "heading",
350 "level": 2,
349 "level": 2,
351 "metadata": {
350 "metadata": {
352 "slideshow": {
351 "slideshow": {
353 "slide_type": "slide"
352 "slide_type": "slide"
354 }
353 }
355 },
354 },
356 "source": [
355 "source": [
357 "Test"
356 "Test"
358 ]
357 ]
359 },
358 },
360 {
359 {
361 "cell_type": "markdown",
360 "cell_type": "markdown",
362 "metadata": {},
361 "metadata": {},
363 "source": [
362 "source": [
364 "You should be able to display your widget just like any other widget now."
363 "You should be able to display your widget just like any other widget now."
365 ]
364 ]
366 },
365 },
367 {
366 {
368 "cell_type": "code",
367 "cell_type": "code",
369 "collapsed": false,
368 "collapsed": false,
370 "input": [
369 "input": [
371 "HelloWidget()"
370 "HelloWidget()"
372 ],
371 ],
373 "language": "python",
372 "language": "python",
374 "metadata": {},
373 "metadata": {},
375 "outputs": []
374 "outputs": []
376 },
375 },
377 {
376 {
378 "cell_type": "heading",
377 "cell_type": "heading",
379 "level": 2,
378 "level": 2,
380 "metadata": {
379 "metadata": {
381 "slideshow": {
380 "slideshow": {
382 "slide_type": "slide"
381 "slide_type": "slide"
383 }
382 }
384 },
383 },
385 "source": [
384 "source": [
386 "Making the widget stateful"
385 "Making the widget stateful"
387 ]
386 ]
388 },
387 },
389 {
388 {
390 "cell_type": "markdown",
389 "cell_type": "markdown",
391 "metadata": {},
390 "metadata": {},
392 "source": [
391 "source": [
393 "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**."
392 "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**."
394 ]
393 ]
395 },
394 },
396 {
395 {
397 "cell_type": "code",
396 "cell_type": "code",
398 "collapsed": false,
397 "collapsed": false,
399 "input": [
398 "input": [
400 "class HelloWidget(widgets.DOMWidget):\n",
399 "class HelloWidget(widgets.DOMWidget):\n",
401 " _view_name = Unicode('HelloView', sync=True)\n",
400 " _view_name = Unicode('HelloView', sync=True)\n",
402 " value = Unicode('Hello World!', sync=True)"
401 " value = Unicode('Hello World!', sync=True)"
403 ],
402 ],
404 "language": "python",
403 "language": "python",
405 "metadata": {},
404 "metadata": {},
406 "outputs": []
405 "outputs": []
407 },
406 },
408 {
407 {
409 "cell_type": "heading",
408 "cell_type": "heading",
410 "level": 3,
409 "level": 3,
411 "metadata": {
410 "metadata": {
412 "slideshow": {
411 "slideshow": {
413 "slide_type": "slide"
412 "slide_type": "slide"
414 }
413 }
415 },
414 },
416 "source": [
415 "source": [
417 "Accessing the model from the view"
416 "Accessing the model from the view"
418 ]
417 ]
419 },
418 },
420 {
419 {
421 "cell_type": "markdown",
420 "cell_type": "markdown",
422 "metadata": {},
421 "metadata": {},
423 "source": [
422 "source": [
424 "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)."
423 "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)."
425 ]
424 ]
426 },
425 },
427 {
426 {
428 "cell_type": "heading",
427 "cell_type": "heading",
429 "level": 3,
428 "level": 3,
430 "metadata": {
429 "metadata": {
431 "slideshow": {
430 "slideshow": {
432 "slide_type": "slide"
431 "slide_type": "slide"
433 }
432 }
434 },
433 },
435 "source": [
434 "source": [
436 "Rendering model contents"
435 "Rendering model contents"
437 ]
436 ]
438 },
437 },
439 {
438 {
440 "cell_type": "markdown",
439 "cell_type": "markdown",
441 "metadata": {},
440 "metadata": {},
442 "source": [
441 "source": [
443 "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."
442 "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."
444 ]
443 ]
445 },
444 },
446 {
445 {
447 "cell_type": "code",
446 "cell_type": "code",
448 "collapsed": false,
447 "collapsed": false,
449 "input": [
448 "input": [
450 "%%javascript\n",
449 "%%javascript\n",
451 "require([\"widgets/js/widget\", \"widgets/js/manager\"], function(widget, manager){\n",
450 "require([\"widgets/js/widget\", \"widgets/js/manager\"], function(widget, manager){\n",
452 " \n",
451 " \n",
453 " var HelloView = widget.DOMWidgetView.extend({\n",
452 " var HelloView = widget.DOMWidgetView.extend({\n",
454 " \n",
453 " \n",
455 " render: function(){ \n",
454 " render: function(){ \n",
456 " this.$el.text(this.model.get('value')); \n",
455 " this.$el.text(this.model.get('value')); \n",
457 " },\n",
456 " },\n",
458 " });\n",
457 " });\n",
459 " \n",
458 " \n",
460 " manager.WidgetManager.register_widget_view('HelloView', HelloView);\n",
459 " manager.WidgetManager.register_widget_view('HelloView', HelloView);\n",
461 "});"
460 "});"
462 ],
461 ],
463 "language": "python",
462 "language": "python",
464 "metadata": {},
463 "metadata": {},
465 "outputs": []
464 "outputs": []
466 },
465 },
467 {
466 {
468 "cell_type": "heading",
467 "cell_type": "heading",
469 "level": 3,
468 "level": 3,
470 "metadata": {
469 "metadata": {
471 "slideshow": {
470 "slideshow": {
472 "slide_type": "slide"
471 "slide_type": "slide"
473 }
472 }
474 },
473 },
475 "source": [
474 "source": [
476 "Dynamic updates"
475 "Dynamic updates"
477 ]
476 ]
478 },
477 },
479 {
478 {
480 "cell_type": "markdown",
479 "cell_type": "markdown",
481 "metadata": {},
480 "metadata": {},
482 "source": [
481 "source": [
483 "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)."
482 "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)."
484 ]
483 ]
485 },
484 },
486 {
485 {
487 "cell_type": "code",
486 "cell_type": "code",
488 "collapsed": false,
487 "collapsed": false,
489 "input": [
488 "input": [
490 "%%javascript\n",
489 "%%javascript\n",
491 "require([\"widgets/js/widget\", \"widgets/js/manager\"], function(widget, manager){\n",
490 "require([\"widgets/js/widget\", \"widgets/js/manager\"], function(widget, manager){\n",
492 " \n",
491 " \n",
493 " var HelloView = widget.DOMWidgetView.extend({\n",
492 " var HelloView = widget.DOMWidgetView.extend({\n",
494 " \n",
493 " \n",
495 " render: function(){ \n",
494 " render: function(){ \n",
496 " this.value_changed();\n",
495 " this.value_changed();\n",
497 " this.model.on('change:value', this.value_changed, this);\n",
496 " this.model.on('change:value', this.value_changed, this);\n",
498 " },\n",
497 " },\n",
499 " \n",
498 " \n",
500 " value_changed: function() {\n",
499 " value_changed: function() {\n",
501 " this.$el.text(this.model.get('value')); \n",
500 " this.$el.text(this.model.get('value')); \n",
502 " },\n",
501 " },\n",
503 " });\n",
502 " });\n",
504 " \n",
503 " \n",
505 " manager.WidgetManager.register_widget_view('HelloView', HelloView);\n",
504 " manager.WidgetManager.register_widget_view('HelloView', HelloView);\n",
506 "});"
505 "});"
507 ],
506 ],
508 "language": "python",
507 "language": "python",
509 "metadata": {},
508 "metadata": {},
510 "outputs": []
509 "outputs": []
511 },
510 },
512 {
511 {
513 "cell_type": "heading",
512 "cell_type": "heading",
514 "level": 2,
513 "level": 2,
515 "metadata": {
514 "metadata": {
516 "slideshow": {
515 "slideshow": {
517 "slide_type": "slide"
516 "slide_type": "slide"
518 }
517 }
519 },
518 },
520 "source": [
519 "source": [
521 "Test"
520 "Test"
522 ]
521 ]
523 },
522 },
524 {
523 {
525 "cell_type": "code",
524 "cell_type": "code",
526 "collapsed": false,
525 "collapsed": false,
527 "input": [
526 "input": [
528 "w = HelloWidget()\n",
527 "w = HelloWidget()\n",
529 "w"
528 "w"
530 ],
529 ],
531 "language": "python",
530 "language": "python",
532 "metadata": {},
531 "metadata": {},
533 "outputs": []
532 "outputs": []
534 },
533 },
535 {
534 {
536 "cell_type": "code",
535 "cell_type": "code",
537 "collapsed": false,
536 "collapsed": false,
538 "input": [
537 "input": [
539 "w.value = 'test'"
538 "w.value = 'test'"
540 ],
539 ],
541 "language": "python",
540 "language": "python",
542 "metadata": {},
541 "metadata": {},
543 "outputs": []
542 "outputs": []
544 },
543 },
545 {
544 {
546 "cell_type": "heading",
545 "cell_type": "heading",
547 "level": 1,
546 "level": 1,
548 "metadata": {
547 "metadata": {
549 "slideshow": {
548 "slideshow": {
550 "slide_type": "slide"
549 "slide_type": "slide"
551 }
550 }
552 },
551 },
553 "source": [
552 "source": [
554 "Finishing"
553 "Finishing"
555 ]
554 ]
556 },
555 },
557 {
556 {
558 "cell_type": "heading",
557 "cell_type": "heading",
559 "level": 2,
558 "level": 2,
560 "metadata": {},
559 "metadata": {},
561 "source": [
560 "source": [
562 "Bidirectional communication"
561 "Bidirectional communication"
563 ]
562 ]
564 },
563 },
565 {
564 {
566 "cell_type": "markdown",
565 "cell_type": "markdown",
567 "metadata": {},
566 "metadata": {},
568 "source": [
567 "source": [
569 "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."
568 "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."
570 ]
569 ]
571 },
570 },
572 {
571 {
573 "cell_type": "heading",
572 "cell_type": "heading",
574 "level": 3,
573 "level": 3,
575 "metadata": {
574 "metadata": {
576 "slideshow": {
575 "slideshow": {
577 "slide_type": "slide"
576 "slide_type": "slide"
578 }
577 }
579 },
578 },
580 "source": [
579 "source": [
581 "Update the Python code"
580 "Update the Python code"
582 ]
581 ]
583 },
582 },
584 {
583 {
585 "cell_type": "markdown",
584 "cell_type": "markdown",
586 "metadata": {},
585 "metadata": {},
587 "source": [
586 "source": [
588 "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`."
587 "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`."
589 ]
588 ]
590 },
589 },
591 {
590 {
592 "cell_type": "code",
591 "cell_type": "code",
593 "collapsed": false,
592 "collapsed": false,
594 "input": [
593 "input": [
595 "from IPython.utils.traitlets import CInt\n",
594 "from IPython.utils.traitlets import CInt\n",
596 "class SpinnerWidget(widgets.DOMWidget):\n",
595 "class SpinnerWidget(widgets.DOMWidget):\n",
597 " _view_name = Unicode('SpinnerView', sync=True)\n",
596 " _view_name = Unicode('SpinnerView', sync=True)\n",
598 " value = CInt(0, sync=True)"
597 " value = CInt(0, sync=True)"
599 ],
598 ],
600 "language": "python",
599 "language": "python",
601 "metadata": {},
600 "metadata": {},
602 "outputs": []
601 "outputs": []
603 },
602 },
604 {
603 {
605 "cell_type": "heading",
604 "cell_type": "heading",
606 "level": 3,
605 "level": 3,
607 "metadata": {
606 "metadata": {
608 "slideshow": {
607 "slideshow": {
609 "slide_type": "slide"
608 "slide_type": "slide"
610 }
609 }
611 },
610 },
612 "source": [
611 "source": [
613 "Updating the Javascript code"
612 "Updating the Javascript code"
614 ]
613 ]
615 },
614 },
616 {
615 {
617 "cell_type": "markdown",
616 "cell_type": "markdown",
618 "metadata": {},
617 "metadata": {},
619 "source": [
618 "source": [
620 "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."
619 "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."
621 ]
620 ]
622 },
621 },
623 {
622 {
624 "cell_type": "code",
623 "cell_type": "code",
625 "collapsed": false,
624 "collapsed": false,
626 "input": [
625 "input": [
627 "%%javascript\n",
626 "%%javascript\n",
628 "require([\"widgets/js/widget\", \"widgets/js/manager\"], function(widget, manager){\n",
627 "require([\"widgets/js/widget\", \"widgets/js/manager\"], function(widget, manager){\n",
629 " \n",
628 " \n",
630 " var SpinnerView = widget.DOMWidgetView.extend({\n",
629 " var SpinnerView = widget.DOMWidgetView.extend({\n",
631 " \n",
630 " \n",
632 " render: function(){ \n",
631 " render: function(){ \n",
633 " \n",
632 " \n",
634 " // jQuery code to create a spinner and append it to $el\n",
633 " // jQuery code to create a spinner and append it to $el\n",
635 " this.$input = $('<input />');\n",
634 " this.$input = $('<input />');\n",
636 " this.$el.append(this.$input);\n",
635 " this.$el.append(this.$input);\n",
637 " this.$spinner = this.$input.spinner({\n",
636 " this.$spinner = this.$input.spinner({\n",
638 " change: function( event, ui ) {}\n",
637 " change: function( event, ui ) {}\n",
639 " });\n",
638 " });\n",
640 " \n",
639 " \n",
641 " this.value_changed();\n",
640 " this.value_changed();\n",
642 " this.model.on('change:value', this.value_changed, this);\n",
641 " this.model.on('change:value', this.value_changed, this);\n",
643 " },\n",
642 " },\n",
644 " \n",
643 " \n",
645 " value_changed: function() {\n",
644 " value_changed: function() {\n",
646 " \n",
645 " \n",
647 " },\n",
646 " },\n",
648 " });\n",
647 " });\n",
649 " \n",
648 " \n",
650 " manager.WidgetManager.register_widget_view('SpinnerView', SpinnerView);\n",
649 " manager.WidgetManager.register_widget_view('SpinnerView', SpinnerView);\n",
651 "});"
650 "});"
652 ],
651 ],
653 "language": "python",
652 "language": "python",
654 "metadata": {},
653 "metadata": {},
655 "outputs": []
654 "outputs": []
656 },
655 },
657 {
656 {
658 "cell_type": "heading",
657 "cell_type": "heading",
659 "level": 3,
658 "level": 3,
660 "metadata": {
659 "metadata": {
661 "slideshow": {
660 "slideshow": {
662 "slide_type": "slide"
661 "slide_type": "slide"
663 }
662 }
664 },
663 },
665 "source": [
664 "source": [
666 "Getting and setting the value"
665 "Getting and setting the value"
667 ]
666 ]
668 },
667 },
669 {
668 {
670 "cell_type": "markdown",
669 "cell_type": "markdown",
671 "metadata": {},
670 "metadata": {},
672 "source": [
671 "source": [
673 "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.**"
672 "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.**"
674 ]
673 ]
675 },
674 },
676 {
675 {
677 "cell_type": "code",
676 "cell_type": "code",
678 "collapsed": false,
677 "collapsed": false,
679 "input": [
678 "input": [
680 "%%javascript\n",
679 "%%javascript\n",
681 "require([\"widgets/js/widget\", \"widgets/js/manager\"], function(widget, manager){\n",
680 "require([\"widgets/js/widget\", \"widgets/js/manager\"], function(widget, manager){\n",
682 " \n",
681 " \n",
683 " var SpinnerView = widget.DOMWidgetView.extend({\n",
682 " var SpinnerView = widget.DOMWidgetView.extend({\n",
684 " \n",
683 " \n",
685 " render: function(){ \n",
684 " render: function(){ \n",
686 "\n",
685 "\n",
687 " var that = this;\n",
686 " var that = this;\n",
688 " this.$input = $('<input />');\n",
687 " this.$input = $('<input />');\n",
689 " this.$el.append(this.$input);\n",
688 " this.$el.append(this.$input);\n",
690 " this.$spinner = this.$input.spinner({\n",
689 " this.$spinner = this.$input.spinner({\n",
691 " change: function( event, ui ) {\n",
690 " change: function( event, ui ) {\n",
692 " that.handle_spin();\n",
691 " that.handle_spin();\n",
693 " },\n",
692 " },\n",
694 " spin: function( event, ui ) {\n",
693 " spin: function( event, ui ) {\n",
695 " that.handle_spin();\n",
694 " that.handle_spin();\n",
696 " }\n",
695 " }\n",
697 " });\n",
696 " });\n",
698 " \n",
697 " \n",
699 " this.value_changed();\n",
698 " this.value_changed();\n",
700 " this.model.on('change:value', this.value_changed, this);\n",
699 " this.model.on('change:value', this.value_changed, this);\n",
701 " },\n",
700 " },\n",
702 " \n",
701 " \n",
703 " value_changed: function() {\n",
702 " value_changed: function() {\n",
704 " this.$spinner.spinner('value', this.model.get('value'));\n",
703 " this.$spinner.spinner('value', this.model.get('value'));\n",
705 " },\n",
704 " },\n",
706 " \n",
705 " \n",
707 " handle_spin: function() {\n",
706 " handle_spin: function() {\n",
708 " this.model.set('value', this.$spinner.spinner('value'));\n",
707 " this.model.set('value', this.$spinner.spinner('value'));\n",
709 " this.touch();\n",
708 " this.touch();\n",
710 " },\n",
709 " },\n",
711 " });\n",
710 " });\n",
712 " \n",
711 " \n",
713 " manager.WidgetManager.register_widget_view('SpinnerView', SpinnerView);\n",
712 " manager.WidgetManager.register_widget_view('SpinnerView', SpinnerView);\n",
714 "});"
713 "});"
715 ],
714 ],
716 "language": "python",
715 "language": "python",
717 "metadata": {},
716 "metadata": {},
718 "outputs": []
717 "outputs": []
719 },
718 },
720 {
719 {
721 "cell_type": "heading",
720 "cell_type": "heading",
722 "level": 2,
721 "level": 2,
723 "metadata": {
722 "metadata": {
724 "slideshow": {
723 "slideshow": {
725 "slide_type": "slide"
724 "slide_type": "slide"
726 }
725 }
727 },
726 },
728 "source": [
727 "source": [
729 "Test"
728 "Test"
730 ]
729 ]
731 },
730 },
732 {
731 {
733 "cell_type": "code",
732 "cell_type": "code",
734 "collapsed": false,
733 "collapsed": false,
735 "input": [
734 "input": [
736 "w = SpinnerWidget(value=5)\n",
735 "w = SpinnerWidget(value=5)\n",
737 "w"
736 "w"
738 ],
737 ],
739 "language": "python",
738 "language": "python",
740 "metadata": {},
739 "metadata": {},
741 "outputs": []
740 "outputs": []
742 },
741 },
743 {
742 {
744 "cell_type": "code",
743 "cell_type": "code",
745 "collapsed": false,
744 "collapsed": false,
746 "input": [
745 "input": [
747 "w.value"
746 "w.value"
748 ],
747 ],
749 "language": "python",
748 "language": "python",
750 "metadata": {},
749 "metadata": {},
751 "outputs": []
750 "outputs": []
752 },
751 },
753 {
752 {
754 "cell_type": "code",
753 "cell_type": "code",
755 "collapsed": false,
754 "collapsed": false,
756 "input": [
755 "input": [
757 "w.value = 20"
756 "w.value = 20"
758 ],
757 ],
759 "language": "python",
758 "language": "python",
760 "metadata": {},
759 "metadata": {},
761 "outputs": []
760 "outputs": []
762 },
761 },
763 {
762 {
764 "cell_type": "markdown",
763 "cell_type": "markdown",
765 "metadata": {},
764 "metadata": {},
766 "source": [
765 "source": [
767 "Trying to **use the spinner with another widget**."
766 "Trying to **use the spinner with another widget**."
768 ]
767 ]
769 },
768 },
770 {
769 {
771 "cell_type": "code",
770 "cell_type": "code",
772 "collapsed": false,
771 "collapsed": false,
773 "input": [
772 "input": [
774 "from IPython.display import display\n",
773 "from IPython.display import display\n",
775 "w1 = SpinnerWidget(value=0)\n",
774 "w1 = SpinnerWidget(value=0)\n",
776 "w2 = widgets.IntSlider()\n",
775 "w2 = widgets.IntSlider()\n",
777 "display(w1,w2)\n",
776 "display(w1,w2)\n",
778 "\n",
777 "\n",
779 "from IPython.utils.traitlets import link\n",
778 "from IPython.utils.traitlets import link\n",
780 "mylink = link((w1, 'value'), (w2, 'value'))"
779 "mylink = link((w1, 'value'), (w2, 'value'))"
781 ],
780 ],
782 "language": "python",
781 "language": "python",
783 "metadata": {},
782 "metadata": {},
784 "outputs": []
783 "outputs": []
785 },
784 },
786 {
785 {
787 "cell_type": "markdown",
786 "cell_type": "markdown",
788 "metadata": {},
787 "metadata": {},
789 "source": [
788 "source": [
790 "[Index](Index.ipynb) - [Back](Widget Styling.ipynb)"
789 "[Index](Index.ipynb) - [Back](Widget Styling.ipynb)"
791 ]
790 ]
792 }
791 }
793 ],
792 ],
794 "metadata": {}
793 "metadata": {}
795 }
794 }
796 ]
795 ]
797 } No newline at end of file
796 }
@@ -1,111 +1,111 b''
1 {
1 {
2 "metadata": {
2 "metadata": {
3 "kernelspec": {
3 "kernelspec": {
4 "codemirror_mode": {
4 "codemirror_mode": {
5 "name": "python",
5 "name": "python",
6 "version": 2
6 "version": 2
7 },
7 },
8 "display_name": "Python 2",
8 "display_name": "Python 2",
9 "language": "python",
9 "language": "python",
10 "name": "python2"
10 "name": "python2"
11 },
11 },
12 "name": "",
12 "name": "",
13 "signature": "sha256:cbc67230b7f7d3d8c7ab3a3c5910aaa3e0caf736d0d963892cb2139683caf5fb"
13 "signature": "sha256:8e469f292b096d750dc8eeb71caa8c08b26bc722708314a26a7684c380ccd20f"
14 },
14 },
15 "nbformat": 3,
15 "nbformat": 3,
16 "nbformat_minor": 0,
16 "nbformat_minor": 0,
17 "worksheets": [
17 "worksheets": [
18 {
18 {
19 "cells": [
19 "cells": [
20 {
20 {
21 "cell_type": "markdown",
21 "cell_type": "markdown",
22 "metadata": {},
22 "metadata": {},
23 "source": [
23 "source": [
24 "<img src=\"../images/ipython_logo.png\">"
24 "<img src=\"../images/ipython_logo.png\">"
25 ]
25 ]
26 },
26 },
27 {
27 {
28 "cell_type": "markdown",
28 "cell_type": "markdown",
29 "metadata": {},
29 "metadata": {},
30 "source": [
30 "source": [
31 "Back to the main [Index](../Index.ipynb)"
31 "Back to the main [Index](../Index.ipynb)"
32 ]
32 ]
33 },
33 },
34 {
34 {
35 "cell_type": "heading",
35 "cell_type": "heading",
36 "level": 1,
36 "level": 1,
37 "metadata": {},
37 "metadata": {},
38 "source": [
38 "source": [
39 "Interactive Widgets"
39 "Interactive Widgets"
40 ]
40 ]
41 },
41 },
42 {
42 {
43 "cell_type": "markdown",
43 "cell_type": "markdown",
44 "metadata": {},
44 "metadata": {},
45 "source": [
45 "source": [
46 "IPython includes an architecture for interactive widgets that tie together Python code running in the kernel and JavaScript/HTML/CSS running in the browser. These widgets enable users to explore their code and data interactively."
46 "IPython includes an architecture for interactive widgets that tie together Python code running in the kernel and JavaScript/HTML/CSS running in the browser. These widgets enable users to explore their code and data interactively."
47 ]
47 ]
48 },
48 },
49 {
49 {
50 "cell_type": "heading",
50 "cell_type": "heading",
51 "level": 2,
51 "level": 2,
52 "metadata": {},
52 "metadata": {},
53 "source": [
53 "source": [
54 "Tutorials"
54 "Tutorials"
55 ]
55 ]
56 },
56 },
57 {
57 {
58 "cell_type": "markdown",
58 "cell_type": "markdown",
59 "metadata": {},
59 "metadata": {},
60 "source": [
60 "source": [
61 "- [Using Interact](Using Interact.ipynb)\n",
61 "- [Using Interact](Using Interact.ipynb)\n",
62 "- [Widget Basics](Widget Basics.ipynb) \n",
62 "- [Widget Basics](Widget Basics.ipynb) \n",
63 "- [Widget Events](Widget Events.ipynb) \n",
63 "- [Widget Events](Widget Events.ipynb) \n",
64 "- [Widget Placement](Widget Placement.ipynb) \n",
64 "- [Widget Placement](Widget Placement.ipynb) \n",
65 "- [Widget Styles](Widget Styles.ipynb) \n",
65 "- [Widget Styling](Widget Styling.ipynb) \n",
66 "- [Custom Widget](Custom Widget - Hello World.ipynb)"
66 "- [Custom Widget](Custom Widget - Hello World.ipynb)"
67 ]
67 ]
68 },
68 },
69 {
69 {
70 "cell_type": "heading",
70 "cell_type": "heading",
71 "level": 2,
71 "level": 2,
72 "metadata": {},
72 "metadata": {},
73 "source": [
73 "source": [
74 "Examples of custom widgets"
74 "Examples of custom widgets"
75 ]
75 ]
76 },
76 },
77 {
77 {
78 "cell_type": "markdown",
78 "cell_type": "markdown",
79 "metadata": {},
79 "metadata": {},
80 "source": [
80 "source": [
81 "- [Variable Inspector](Variable Inspector.ipynb) \n",
81 "- [Variable Inspector](Variable Inspector.ipynb) \n",
82 "- [Export As (nbconvert)](Export As (nbconvert%29.ipynb) \n",
82 "- [Export As (nbconvert)](Export As (nbconvert%29.ipynb) \n",
83 "- [Nonblocking Console](Nonblocking Console.ipynb) \n",
83 "- [Nonblocking Console](Nonblocking Console.ipynb) \n",
84 "- [File Upload Widget](File Upload Widget.ipynb)"
84 "- [File Upload Widget](File Upload Widget.ipynb)"
85 ]
85 ]
86 },
86 },
87 {
87 {
88 "cell_type": "heading",
88 "cell_type": "heading",
89 "level": 2,
89 "level": 2,
90 "metadata": {},
90 "metadata": {},
91 "source": [
91 "source": [
92 "Examples using `interact`/`interactive`"
92 "Examples using `interact`/`interactive`"
93 ]
93 ]
94 },
94 },
95 {
95 {
96 "cell_type": "markdown",
96 "cell_type": "markdown",
97 "metadata": {},
97 "metadata": {},
98 "source": [
98 "source": [
99 "* [Beat Frequencies](Beat Frequencies.ipynb)\n",
99 "* [Beat Frequencies](Beat Frequencies.ipynb)\n",
100 "* [Exploring Graphs](Exploring Graphs.ipynb)\n",
100 "* [Exploring Graphs](Exploring Graphs.ipynb)\n",
101 "* [Factoring](Factoring.ipynb)\n",
101 "* [Factoring](Factoring.ipynb)\n",
102 "* [Image Browser](Image Browser.ipynb)\n",
102 "* [Image Browser](Image Browser.ipynb)\n",
103 "* [Image Processing](Image Processing.ipynb)\n",
103 "* [Image Processing](Image Processing.ipynb)\n",
104 "* [Lorenz Differential Equations](Lorenz Differential Equations.ipynb)"
104 "* [Lorenz Differential Equations](Lorenz Differential Equations.ipynb)"
105 ]
105 ]
106 }
106 }
107 ],
107 ],
108 "metadata": {}
108 "metadata": {}
109 }
109 }
110 ]
110 ]
111 } No newline at end of file
111 }
@@ -1,233 +1,233 b''
1 {
1 {
2 "metadata": {
2 "metadata": {
3 "kernelspec": {
3 "kernelspec": {
4 "codemirror_mode": {
4 "codemirror_mode": {
5 "name": "python",
5 "name": "python",
6 "version": 2
6 "version": 2
7 },
7 },
8 "display_name": "Python 2",
8 "display_name": "Python 2",
9 "language": "python",
9 "language": "python",
10 "name": "python2"
10 "name": "python2"
11 },
11 },
12 "name": "",
12 "name": "",
13 "signature": "sha256:044aa64f06487b5a08eb26102a46538bfb4e00d37a4cc14edcdf54986a9ef68e"
13 "signature": "sha256:3dfbc0dcb1fefc9ef028022760916b0300e14f71bf8e27589e61800841d5839c"
14 },
14 },
15 "nbformat": 3,
15 "nbformat": 3,
16 "nbformat_minor": 0,
16 "nbformat_minor": 0,
17 "worksheets": [
17 "worksheets": [
18 {
18 {
19 "cells": [
19 "cells": [
20 {
20 {
21 "cell_type": "code",
21 "cell_type": "code",
22 "collapsed": false,
22 "collapsed": false,
23 "input": [
23 "input": [
24 "# Console related imports.\n",
24 "# Console related imports.\n",
25 "from subprocess import Popen, PIPE\n",
25 "from subprocess import Popen, PIPE\n",
26 "import os\n",
26 "import os\n",
27 "from IPython.utils.py3compat import bytes_to_str, string_types\n",
27 "from IPython.utils.py3compat import bytes_to_str, string_types\n",
28 "\n",
28 "\n",
29 "# Widget related imports.\n",
29 "# Widget related imports.\n",
30 "from IPython.html import widgets\n",
30 "from IPython.html import widgets\n",
31 "from IPython.display import display"
31 "from IPython.display import display"
32 ],
32 ],
33 "language": "python",
33 "language": "python",
34 "metadata": {},
34 "metadata": {},
35 "outputs": []
35 "outputs": []
36 },
36 },
37 {
37 {
38 "cell_type": "markdown",
38 "cell_type": "markdown",
39 "metadata": {},
39 "metadata": {},
40 "source": [
40 "source": [
41 "Define function to run a process without blocking the input."
41 "Define function to run a process without blocking the input."
42 ]
42 ]
43 },
43 },
44 {
44 {
45 "cell_type": "code",
45 "cell_type": "code",
46 "collapsed": false,
46 "collapsed": false,
47 "input": [
47 "input": [
48 "def read_process(process, append_output):\n",
48 "def read_process(process, append_output):\n",
49 " \"\"\" Try to read the stdout and stderr of a process and render it using \n",
49 " \"\"\" Try to read the stdout and stderr of a process and render it using \n",
50 " the append_output method provided\n",
50 " the append_output method provided\n",
51 " \n",
51 " \n",
52 " Parameters\n",
52 " Parameters\n",
53 " ----------\n",
53 " ----------\n",
54 " process: Popen handle\n",
54 " process: Popen handle\n",
55 " append_output: method handle\n",
55 " append_output: method handle\n",
56 " Callback to render output. Signature of\n",
56 " Callback to render output. Signature of\n",
57 " append_output(output, [prefix=])\"\"\"\n",
57 " append_output(output, [prefix=])\"\"\"\n",
58 " \n",
58 " \n",
59 " try:\n",
59 " try:\n",
60 " stdout = process.stdout.read()\n",
60 " stdout = process.stdout.read()\n",
61 " if stdout is not None and len(stdout) > 0:\n",
61 " if stdout is not None and len(stdout) > 0:\n",
62 " append_output(stdout, prefix=' ')\n",
62 " append_output(stdout, prefix=' ')\n",
63 " except:\n",
63 " except:\n",
64 " pass\n",
64 " pass\n",
65 " \n",
65 " \n",
66 " try:\n",
66 " try:\n",
67 " stderr = process.stderr.read()\n",
67 " stderr = process.stderr.read()\n",
68 " if stderr is not None and len(stderr) > 0:\n",
68 " if stderr is not None and len(stderr) > 0:\n",
69 " append_output(stderr, prefix='ERR ')\n",
69 " append_output(stderr, prefix='ERR ')\n",
70 " except:\n",
70 " except:\n",
71 " pass\n",
71 " pass\n",
72 "\n",
72 "\n",
73 "\n",
73 "\n",
74 "def set_pipe_nonblocking(pipe):\n",
74 "def set_pipe_nonblocking(pipe):\n",
75 " \"\"\"Set a pipe as non-blocking\"\"\"\n",
75 " \"\"\"Set a pipe as non-blocking\"\"\"\n",
76 " try:\n",
76 " try:\n",
77 " import fcntl\n",
77 " import fcntl\n",
78 " fl = fcntl.fcntl(pipe, fcntl.F_GETFL)\n",
78 " fl = fcntl.fcntl(pipe, fcntl.F_GETFL)\n",
79 " fcntl.fcntl(pipe, fcntl.F_SETFL, fl | os.O_NONBLOCK)\n",
79 " fcntl.fcntl(pipe, fcntl.F_SETFL, fl | os.O_NONBLOCK)\n",
80 " except:\n",
80 " except:\n",
81 " pass\n",
81 " pass\n",
82 "\n",
82 "\n",
83 "kernel = get_ipython().kernel\n",
83 "kernel = get_ipython().kernel\n",
84 "def run_command(command, append_output, has_user_exited=None):\n",
84 "def run_command(command, append_output, has_user_exited=None):\n",
85 " \"\"\"Run a command asyncronously\n",
85 " \"\"\"Run a command asyncronously\n",
86 " \n",
86 " \n",
87 " Parameters\n",
87 " Parameters\n",
88 " ----------\n",
88 " ----------\n",
89 " command: str\n",
89 " command: str\n",
90 " Shell command to launch a process with.\n",
90 " Shell command to launch a process with.\n",
91 " append_output: method handle\n",
91 " append_output: method handle\n",
92 " Callback to render output. Signature of\n",
92 " Callback to render output. Signature of\n",
93 " append_output(output, [prefix=])\n",
93 " append_output(output, [prefix=])\n",
94 " has_user_exited: method handle\n",
94 " has_user_exited: method handle\n",
95 " Check to see if the user wants to stop the command.\n",
95 " Check to see if the user wants to stop the command.\n",
96 " Must return a boolean.\"\"\"\n",
96 " Must return a boolean.\"\"\"\n",
97 " \n",
97 " \n",
98 " # Echo input.\n",
98 " # Echo input.\n",
99 " append_output(command, prefix='>>> ')\n",
99 " append_output(command, prefix='>>> ')\n",
100 " \n",
100 " \n",
101 " # Create the process. Make sure the pipes are set as non-blocking.\n",
101 " # Create the process. Make sure the pipes are set as non-blocking.\n",
102 " process = Popen(command, shell=True, stdout=PIPE, stderr=PIPE)\n",
102 " process = Popen(command, shell=True, stdout=PIPE, stderr=PIPE)\n",
103 " set_pipe_nonblocking(process.stdout)\n",
103 " set_pipe_nonblocking(process.stdout)\n",
104 " set_pipe_nonblocking(process.stderr)\n",
104 " set_pipe_nonblocking(process.stderr)\n",
105 " \n",
105 " \n",
106 " # Only continue to read from the command \n",
106 " # Only continue to read from the command \n",
107 " while (has_user_exited is None or not has_user_exited()) and process.poll() is None:\n",
107 " while (has_user_exited is None or not has_user_exited()) and process.poll() is None:\n",
108 " read_process(process, append_output)\n",
108 " read_process(process, append_output)\n",
109 " kernel.do_one_iteration() # Run IPython iteration. This is the code that\n",
109 " kernel.do_one_iteration() # Run IPython iteration. This is the code that\n",
110 " # makes this operation non-blocking. This will\n",
110 " # makes this operation non-blocking. This will\n",
111 " # allow widget messages and callbacks to be \n",
111 " # allow widget messages and callbacks to be \n",
112 " # processed.\n",
112 " # processed.\n",
113 " \n",
113 " \n",
114 " # If the process is still running, the user must have exited.\n",
114 " # If the process is still running, the user must have exited.\n",
115 " if process.poll() is None:\n",
115 " if process.poll() is None:\n",
116 " process.kill()\n",
116 " process.kill()\n",
117 " else:\n",
117 " else:\n",
118 " read_process(process, append_output) # Read remainer\n",
118 " read_process(process, append_output) # Read remainer\n",
119 " \n",
119 " \n",
120 " \n",
120 " \n",
121 " \n",
121 " \n",
122 " "
122 " "
123 ],
123 ],
124 "language": "python",
124 "language": "python",
125 "metadata": {},
125 "metadata": {},
126 "outputs": []
126 "outputs": []
127 },
127 },
128 {
128 {
129 "cell_type": "markdown",
129 "cell_type": "markdown",
130 "metadata": {},
130 "metadata": {},
131 "source": [
131 "source": [
132 "Create the console widgets without displaying them."
132 "Create the console widgets without displaying them."
133 ]
133 ]
134 },
134 },
135 {
135 {
136 "cell_type": "code",
136 "cell_type": "code",
137 "collapsed": false,
137 "collapsed": false,
138 "input": [
138 "input": [
139 "console_container = widgets.VBox(visible=False)\n",
139 "console_container = widgets.VBox(visible=False)\n",
140 "console_container.padding = '10px'\n",
140 "console_container.padding = '10px'\n",
141 "\n",
141 "\n",
142 "output_box = widgets.Textarea()\n",
142 "output_box = widgets.Textarea()\n",
143 "output_box.height = '400px'\n",
143 "output_box.height = '400px'\n",
144 "output_box.font_family = 'monospace'\n",
144 "output_box.font_family = 'monospace'\n",
145 "output_box.fore_color = '#AAAAAA'\n",
145 "output_box.color = '#AAAAAA'\n",
146 "output_box.back_color = 'black'\n",
146 "output_box.background_color = 'black'\n",
147 "output_box.width = '800px'\n",
147 "output_box.width = '800px'\n",
148 "\n",
148 "\n",
149 "input_box = widgets.Text()\n",
149 "input_box = widgets.Text()\n",
150 "input_box.font_family = 'monospace'\n",
150 "input_box.font_family = 'monospace'\n",
151 "input_box.fore_color = '#AAAAAA'\n",
151 "input_box.color = '#AAAAAA'\n",
152 "input_box.back_color = 'black'\n",
152 "input_box.background_color = 'black'\n",
153 "input_box.width = '800px'\n",
153 "input_box.width = '800px'\n",
154 "\n",
154 "\n",
155 "console_container.children = [output_box, input_box]"
155 "console_container.children = [output_box, input_box]"
156 ],
156 ],
157 "language": "python",
157 "language": "python",
158 "metadata": {},
158 "metadata": {},
159 "outputs": []
159 "outputs": []
160 },
160 },
161 {
161 {
162 "cell_type": "markdown",
162 "cell_type": "markdown",
163 "metadata": {},
163 "metadata": {},
164 "source": [
164 "source": [
165 "Hook the process execution methods up to our console widgets."
165 "Hook the process execution methods up to our console widgets."
166 ]
166 ]
167 },
167 },
168 {
168 {
169 "cell_type": "code",
169 "cell_type": "code",
170 "collapsed": false,
170 "collapsed": false,
171 "input": [
171 "input": [
172 "\n",
172 "\n",
173 "def append_output(output, prefix):\n",
173 "def append_output(output, prefix):\n",
174 " if isinstance(output, string_types):\n",
174 " if isinstance(output, string_types):\n",
175 " output_str = output\n",
175 " output_str = output\n",
176 " else:\n",
176 " else:\n",
177 " output_str = bytes_to_str(output)\n",
177 " output_str = bytes_to_str(output)\n",
178 " output_lines = output_str.split('\\n')\n",
178 " output_lines = output_str.split('\\n')\n",
179 " formatted_output = '\\n'.join([prefix + line for line in output_lines if len(line) > 0]) + '\\n'\n",
179 " formatted_output = '\\n'.join([prefix + line for line in output_lines if len(line) > 0]) + '\\n'\n",
180 " output_box.value += formatted_output\n",
180 " output_box.value += formatted_output\n",
181 " output_box.scroll_to_bottom()\n",
181 " output_box.scroll_to_bottom()\n",
182 " \n",
182 " \n",
183 "def has_user_exited():\n",
183 "def has_user_exited():\n",
184 " return not console_container.visible\n",
184 " return not console_container.visible\n",
185 "\n",
185 "\n",
186 "def handle_input(sender):\n",
186 "def handle_input(sender):\n",
187 " sender.disabled = True\n",
187 " sender.disabled = True\n",
188 " try:\n",
188 " try:\n",
189 " command = sender.value\n",
189 " command = sender.value\n",
190 " sender.value = ''\n",
190 " sender.value = ''\n",
191 " run_command(command, append_output=append_output, has_user_exited=has_user_exited)\n",
191 " run_command(command, append_output=append_output, has_user_exited=has_user_exited)\n",
192 " finally:\n",
192 " finally:\n",
193 " sender.disabled = False\n",
193 " sender.disabled = False\n",
194 " \n",
194 " \n",
195 "input_box.on_submit(handle_input)"
195 "input_box.on_submit(handle_input)"
196 ],
196 ],
197 "language": "python",
197 "language": "python",
198 "metadata": {},
198 "metadata": {},
199 "outputs": []
199 "outputs": []
200 },
200 },
201 {
201 {
202 "cell_type": "markdown",
202 "cell_type": "markdown",
203 "metadata": {},
203 "metadata": {},
204 "source": [
204 "source": [
205 "Create the button that will be used to display and hide the console. Display both the console container and the new button used to toggle it."
205 "Create the button that will be used to display and hide the console. Display both the console container and the new button used to toggle it."
206 ]
206 ]
207 },
207 },
208 {
208 {
209 "cell_type": "code",
209 "cell_type": "code",
210 "collapsed": false,
210 "collapsed": false,
211 "input": [
211 "input": [
212 "toggle_button = widgets.Button(description=\"Start Console\")\n",
212 "toggle_button = widgets.Button(description=\"Start Console\")\n",
213 "def toggle_console(sender):\n",
213 "def toggle_console(sender):\n",
214 " console_container.visible = not console_container.visible\n",
214 " console_container.visible = not console_container.visible\n",
215 " if console_container.visible:\n",
215 " if console_container.visible:\n",
216 " toggle_button.description=\"Stop Console\"\n",
216 " toggle_button.description=\"Stop Console\"\n",
217 " input_box.disabled = False\n",
217 " input_box.disabled = False\n",
218 " else:\n",
218 " else:\n",
219 " toggle_button.description=\"Start Console\"\n",
219 " toggle_button.description=\"Start Console\"\n",
220 "toggle_button.on_click(toggle_console)\n",
220 "toggle_button.on_click(toggle_console)\n",
221 "\n",
221 "\n",
222 "display(toggle_button)\n",
222 "display(toggle_button)\n",
223 "display(console_container)"
223 "display(console_container)"
224 ],
224 ],
225 "language": "python",
225 "language": "python",
226 "metadata": {},
226 "metadata": {},
227 "outputs": []
227 "outputs": []
228 }
228 }
229 ],
229 ],
230 "metadata": {}
230 "metadata": {}
231 }
231 }
232 ]
232 ]
233 } No newline at end of file
233 }
@@ -1,446 +1,445 b''
1 {
1 {
2 "metadata": {
2 "metadata": {
3 "celltoolbar": "Slideshow",
4 "kernelspec": {
3 "kernelspec": {
5 "codemirror_mode": {
4 "codemirror_mode": {
6 "name": "python",
5 "name": "python",
7 "version": 2
6 "version": 2
8 },
7 },
9 "display_name": "Python 2",
8 "display_name": "Python 2",
10 "language": "python",
9 "language": "python",
11 "name": "python2"
10 "name": "python2"
12 },
11 },
13 "name": "",
12 "name": "",
14 "signature": "sha256:942aa1b77fbab2ba0fa5e52a6e30fe1231a07e5f2a9ef9fc495e106a08e65e39"
13 "signature": "sha256:c8af7f5d30b29ee52fe6a79cf0d573c9c2d5b2f522b04731249e3208671741d3"
15 },
14 },
16 "nbformat": 3,
15 "nbformat": 3,
17 "nbformat_minor": 0,
16 "nbformat_minor": 0,
18 "worksheets": [
17 "worksheets": [
19 {
18 {
20 "cells": [
19 "cells": [
21 {
20 {
22 "cell_type": "markdown",
21 "cell_type": "markdown",
23 "metadata": {},
22 "metadata": {},
24 "source": [
23 "source": [
25 "[Index](Index.ipynb) - [Next](Widget List.ipynb)"
24 "[Index](Index.ipynb) - [Next](Widget List.ipynb)"
26 ]
25 ]
27 },
26 },
28 {
27 {
29 "cell_type": "heading",
28 "cell_type": "heading",
30 "level": 1,
29 "level": 1,
31 "metadata": {},
30 "metadata": {},
32 "source": [
31 "source": [
33 "Simple Widget Introduction"
32 "Simple Widget Introduction"
34 ]
33 ]
35 },
34 },
36 {
35 {
37 "cell_type": "heading",
36 "cell_type": "heading",
38 "level": 2,
37 "level": 2,
39 "metadata": {},
38 "metadata": {},
40 "source": [
39 "source": [
41 "What are widgets?"
40 "What are widgets?"
42 ]
41 ]
43 },
42 },
44 {
43 {
45 "cell_type": "markdown",
44 "cell_type": "markdown",
46 "metadata": {
45 "metadata": {
47 "slideshow": {
46 "slideshow": {
48 "slide_type": "slide"
47 "slide_type": "slide"
49 }
48 }
50 },
49 },
51 "source": [
50 "source": [
52 "Widgets are elements that exists in both the front-end and the back-end.\n",
51 "Widgets are elements that exists in both the front-end and the back-end.\n",
53 "\n",
52 "\n",
54 "![Kernel & front-end diagram](../images/FrontendKernel.png)"
53 "![Kernel & front-end diagram](../images/FrontendKernel.png)"
55 ]
54 ]
56 },
55 },
57 {
56 {
58 "cell_type": "heading",
57 "cell_type": "heading",
59 "level": 2,
58 "level": 2,
60 "metadata": {},
59 "metadata": {},
61 "source": [
60 "source": [
62 "What can they be used for?"
61 "What can they be used for?"
63 ]
62 ]
64 },
63 },
65 {
64 {
66 "cell_type": "markdown",
65 "cell_type": "markdown",
67 "metadata": {
66 "metadata": {
68 "slideshow": {
67 "slideshow": {
69 "slide_type": "slide"
68 "slide_type": "slide"
70 }
69 }
71 },
70 },
72 "source": [
71 "source": [
73 "You can use widgets to build **interactive GUIs** for your notebooks. \n",
72 "You can use widgets to build **interactive GUIs** for your notebooks. \n",
74 "You can also use widgets to **synchronize stateful and stateless information** between Python and JavaScript."
73 "You can also use widgets to **synchronize stateful and stateless information** between Python and JavaScript."
75 ]
74 ]
76 },
75 },
77 {
76 {
78 "cell_type": "heading",
77 "cell_type": "heading",
79 "level": 2,
78 "level": 2,
80 "metadata": {},
79 "metadata": {},
81 "source": [
80 "source": [
82 "Using widgets "
81 "Using widgets "
83 ]
82 ]
84 },
83 },
85 {
84 {
86 "cell_type": "markdown",
85 "cell_type": "markdown",
87 "metadata": {
86 "metadata": {
88 "slideshow": {
87 "slideshow": {
89 "slide_type": "slide"
88 "slide_type": "slide"
90 }
89 }
91 },
90 },
92 "source": [
91 "source": [
93 "To use the widget framework, you need to **import `IPython.html.widgets`**."
92 "To use the widget framework, you need to **import `IPython.html.widgets`**."
94 ]
93 ]
95 },
94 },
96 {
95 {
97 "cell_type": "code",
96 "cell_type": "code",
98 "collapsed": false,
97 "collapsed": false,
99 "input": [
98 "input": [
100 "from IPython.html.widgets import *"
99 "from IPython.html.widgets import *"
101 ],
100 ],
102 "language": "python",
101 "language": "python",
103 "metadata": {},
102 "metadata": {},
104 "outputs": []
103 "outputs": []
105 },
104 },
106 {
105 {
107 "cell_type": "heading",
106 "cell_type": "heading",
108 "level": 3,
107 "level": 3,
109 "metadata": {
108 "metadata": {
110 "slideshow": {
109 "slideshow": {
111 "slide_type": "slide"
110 "slide_type": "slide"
112 }
111 }
113 },
112 },
114 "source": [
113 "source": [
115 "repr"
114 "repr"
116 ]
115 ]
117 },
116 },
118 {
117 {
119 "cell_type": "markdown",
118 "cell_type": "markdown",
120 "metadata": {},
119 "metadata": {},
121 "source": [
120 "source": [
122 "Widgets have their own display `repr` which allows them to be displayed using IPython's display framework. Constructing and returning an `IntSlider` 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."
121 "Widgets have their own display `repr` which allows them to be displayed using IPython's display framework. Constructing and returning an `IntSlider` 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."
123 ]
122 ]
124 },
123 },
125 {
124 {
126 "cell_type": "code",
125 "cell_type": "code",
127 "collapsed": false,
126 "collapsed": false,
128 "input": [
127 "input": [
129 "IntSlider()"
128 "IntSlider()"
130 ],
129 ],
131 "language": "python",
130 "language": "python",
132 "metadata": {},
131 "metadata": {},
133 "outputs": []
132 "outputs": []
134 },
133 },
135 {
134 {
136 "cell_type": "heading",
135 "cell_type": "heading",
137 "level": 3,
136 "level": 3,
138 "metadata": {
137 "metadata": {
139 "slideshow": {
138 "slideshow": {
140 "slide_type": "slide"
139 "slide_type": "slide"
141 }
140 }
142 },
141 },
143 "source": [
142 "source": [
144 "display()"
143 "display()"
145 ]
144 ]
146 },
145 },
147 {
146 {
148 "cell_type": "markdown",
147 "cell_type": "markdown",
149 "metadata": {},
148 "metadata": {},
150 "source": [
149 "source": [
151 "You can also explicitly display the widget using `display(...)`."
150 "You can also explicitly display the widget using `display(...)`."
152 ]
151 ]
153 },
152 },
154 {
153 {
155 "cell_type": "code",
154 "cell_type": "code",
156 "collapsed": false,
155 "collapsed": false,
157 "input": [
156 "input": [
158 "from IPython.display import display\n",
157 "from IPython.display import display\n",
159 "w = IntSlider()\n",
158 "w = IntSlider()\n",
160 "display(w)"
159 "display(w)"
161 ],
160 ],
162 "language": "python",
161 "language": "python",
163 "metadata": {},
162 "metadata": {},
164 "outputs": []
163 "outputs": []
165 },
164 },
166 {
165 {
167 "cell_type": "heading",
166 "cell_type": "heading",
168 "level": 3,
167 "level": 3,
169 "metadata": {
168 "metadata": {
170 "slideshow": {
169 "slideshow": {
171 "slide_type": "slide"
170 "slide_type": "slide"
172 }
171 }
173 },
172 },
174 "source": [
173 "source": [
175 "Multiple display() calls"
174 "Multiple display() calls"
176 ]
175 ]
177 },
176 },
178 {
177 {
179 "cell_type": "markdown",
178 "cell_type": "markdown",
180 "metadata": {},
179 "metadata": {},
181 "source": [
180 "source": [
182 "If you display the same widget twice, the displayed instances in the front-end **will remain in sync** with each other."
181 "If you display the same widget twice, the displayed instances in the front-end **will remain in sync** with each other."
183 ]
182 ]
184 },
183 },
185 {
184 {
186 "cell_type": "code",
185 "cell_type": "code",
187 "collapsed": false,
186 "collapsed": false,
188 "input": [
187 "input": [
189 "display(w)"
188 "display(w)"
190 ],
189 ],
191 "language": "python",
190 "language": "python",
192 "metadata": {},
191 "metadata": {},
193 "outputs": []
192 "outputs": []
194 },
193 },
195 {
194 {
196 "cell_type": "heading",
195 "cell_type": "heading",
197 "level": 2,
196 "level": 2,
198 "metadata": {},
197 "metadata": {},
199 "source": [
198 "source": [
200 "Why does displaying the same widget twice work?"
199 "Why does displaying the same widget twice work?"
201 ]
200 ]
202 },
201 },
203 {
202 {
204 "cell_type": "markdown",
203 "cell_type": "markdown",
205 "metadata": {
204 "metadata": {
206 "slideshow": {
205 "slideshow": {
207 "slide_type": "slide"
206 "slide_type": "slide"
208 }
207 }
209 },
208 },
210 "source": [
209 "source": [
211 "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",
210 "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",
212 "\n",
211 "\n",
213 "![Kernel & front-end diagram](images/WidgetModelView.png)"
212 "![Kernel & front-end diagram](images/WidgetModelView.png)"
214 ]
213 ]
215 },
214 },
216 {
215 {
217 "cell_type": "heading",
216 "cell_type": "heading",
218 "level": 3,
217 "level": 3,
219 "metadata": {
218 "metadata": {
220 "slideshow": {
219 "slideshow": {
221 "slide_type": "slide"
220 "slide_type": "slide"
222 }
221 }
223 },
222 },
224 "source": [
223 "source": [
225 "Closing widgets"
224 "Closing widgets"
226 ]
225 ]
227 },
226 },
228 {
227 {
229 "cell_type": "markdown",
228 "cell_type": "markdown",
230 "metadata": {},
229 "metadata": {},
231 "source": [
230 "source": [
232 "You can close a widget by calling its `close()` method."
231 "You can close a widget by calling its `close()` method."
233 ]
232 ]
234 },
233 },
235 {
234 {
236 "cell_type": "code",
235 "cell_type": "code",
237 "collapsed": false,
236 "collapsed": false,
238 "input": [
237 "input": [
239 "display(w)"
238 "display(w)"
240 ],
239 ],
241 "language": "python",
240 "language": "python",
242 "metadata": {},
241 "metadata": {},
243 "outputs": []
242 "outputs": []
244 },
243 },
245 {
244 {
246 "cell_type": "code",
245 "cell_type": "code",
247 "collapsed": false,
246 "collapsed": false,
248 "input": [
247 "input": [
249 "w.close()"
248 "w.close()"
250 ],
249 ],
251 "language": "python",
250 "language": "python",
252 "metadata": {},
251 "metadata": {},
253 "outputs": []
252 "outputs": []
254 },
253 },
255 {
254 {
256 "cell_type": "heading",
255 "cell_type": "heading",
257 "level": 2,
256 "level": 2,
258 "metadata": {},
257 "metadata": {},
259 "source": [
258 "source": [
260 "Widget properties"
259 "Widget properties"
261 ]
260 ]
262 },
261 },
263 {
262 {
264 "cell_type": "markdown",
263 "cell_type": "markdown",
265 "metadata": {
264 "metadata": {
266 "slideshow": {
265 "slideshow": {
267 "slide_type": "slide"
266 "slide_type": "slide"
268 }
267 }
269 },
268 },
270 "source": [
269 "source": [
271 "All of the IPython widgets **share a similar naming scheme**. To read the value of a widget, you can query its `value` property."
270 "All of the IPython widgets **share a similar naming scheme**. To read the value of a widget, you can query its `value` property."
272 ]
271 ]
273 },
272 },
274 {
273 {
275 "cell_type": "code",
274 "cell_type": "code",
276 "collapsed": false,
275 "collapsed": false,
277 "input": [
276 "input": [
278 "w = IntSlider()\n",
277 "w = IntSlider()\n",
279 "display(w)"
278 "display(w)"
280 ],
279 ],
281 "language": "python",
280 "language": "python",
282 "metadata": {},
281 "metadata": {},
283 "outputs": []
282 "outputs": []
284 },
283 },
285 {
284 {
286 "cell_type": "code",
285 "cell_type": "code",
287 "collapsed": false,
286 "collapsed": false,
288 "input": [
287 "input": [
289 "w.value"
288 "w.value"
290 ],
289 ],
291 "language": "python",
290 "language": "python",
292 "metadata": {},
291 "metadata": {},
293 "outputs": []
292 "outputs": []
294 },
293 },
295 {
294 {
296 "cell_type": "markdown",
295 "cell_type": "markdown",
297 "metadata": {},
296 "metadata": {},
298 "source": [
297 "source": [
299 "Similarly, to set a widget's value, you can set its `value` property."
298 "Similarly, to set a widget's value, you can set its `value` property."
300 ]
299 ]
301 },
300 },
302 {
301 {
303 "cell_type": "code",
302 "cell_type": "code",
304 "collapsed": false,
303 "collapsed": false,
305 "input": [
304 "input": [
306 "w.value = 100"
305 "w.value = 100"
307 ],
306 ],
308 "language": "python",
307 "language": "python",
309 "metadata": {},
308 "metadata": {},
310 "outputs": []
309 "outputs": []
311 },
310 },
312 {
311 {
313 "cell_type": "heading",
312 "cell_type": "heading",
314 "level": 3,
313 "level": 3,
315 "metadata": {
314 "metadata": {
316 "slideshow": {
315 "slideshow": {
317 "slide_type": "slide"
316 "slide_type": "slide"
318 }
317 }
319 },
318 },
320 "source": [
319 "source": [
321 "Keys"
320 "Keys"
322 ]
321 ]
323 },
322 },
324 {
323 {
325 "cell_type": "markdown",
324 "cell_type": "markdown",
326 "metadata": {},
325 "metadata": {},
327 "source": [
326 "source": [
328 "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**."
327 "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**."
329 ]
328 ]
330 },
329 },
331 {
330 {
332 "cell_type": "code",
331 "cell_type": "code",
333 "collapsed": false,
332 "collapsed": false,
334 "input": [
333 "input": [
335 "w.keys"
334 "w.keys"
336 ],
335 ],
337 "language": "python",
336 "language": "python",
338 "metadata": {},
337 "metadata": {},
339 "outputs": []
338 "outputs": []
340 },
339 },
341 {
340 {
342 "cell_type": "heading",
341 "cell_type": "heading",
343 "level": 3,
342 "level": 3,
344 "metadata": {},
343 "metadata": {},
345 "source": [
344 "source": [
346 "Shorthand for setting the initial values of widget properties"
345 "Shorthand for setting the initial values of widget properties"
347 ]
346 ]
348 },
347 },
349 {
348 {
350 "cell_type": "markdown",
349 "cell_type": "markdown",
351 "metadata": {
350 "metadata": {
352 "slideshow": {
351 "slideshow": {
353 "slide_type": "slide"
352 "slide_type": "slide"
354 }
353 }
355 },
354 },
356 "source": [
355 "source": [
357 "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)."
356 "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)."
358 ]
357 ]
359 },
358 },
360 {
359 {
361 "cell_type": "code",
360 "cell_type": "code",
362 "collapsed": false,
361 "collapsed": false,
363 "input": [
362 "input": [
364 "Text(value='Hello World!', disabled=True)"
363 "Text(value='Hello World!', disabled=True)"
365 ],
364 ],
366 "language": "python",
365 "language": "python",
367 "metadata": {},
366 "metadata": {},
368 "outputs": []
367 "outputs": []
369 },
368 },
370 {
369 {
371 "cell_type": "heading",
370 "cell_type": "heading",
372 "level": 2,
371 "level": 2,
373 "metadata": {},
372 "metadata": {},
374 "source": [
373 "source": [
375 "Linking two similar widgets"
374 "Linking two similar widgets"
376 ]
375 ]
377 },
376 },
378 {
377 {
379 "cell_type": "markdown",
378 "cell_type": "markdown",
380 "metadata": {
379 "metadata": {
381 "slideshow": {
380 "slideshow": {
382 "slide_type": "slide"
381 "slide_type": "slide"
383 }
382 }
384 },
383 },
385 "source": [
384 "source": [
386 "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."
385 "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."
387 ]
386 ]
388 },
387 },
389 {
388 {
390 "cell_type": "code",
389 "cell_type": "code",
391 "collapsed": false,
390 "collapsed": false,
392 "input": [
391 "input": [
393 "from IPython.utils.traitlets import link\n",
392 "from IPython.utils.traitlets import link\n",
394 "a = FloatText()\n",
393 "a = FloatText()\n",
395 "b = FloatSlider()\n",
394 "b = FloatSlider()\n",
396 "c = FloatProgress()\n",
395 "c = FloatProgress()\n",
397 "display(a,b,c)\n",
396 "display(a,b,c)\n",
398 "\n",
397 "\n",
399 "\n",
398 "\n",
400 "mylink = link((a, 'value'), (b, 'value'), (c, 'value'))"
399 "mylink = link((a, 'value'), (b, 'value'), (c, 'value'))"
401 ],
400 ],
402 "language": "python",
401 "language": "python",
403 "metadata": {},
402 "metadata": {},
404 "outputs": []
403 "outputs": []
405 },
404 },
406 {
405 {
407 "cell_type": "heading",
406 "cell_type": "heading",
408 "level": 3,
407 "level": 3,
409 "metadata": {},
408 "metadata": {},
410 "source": [
409 "source": [
411 "Unlinking widgets"
410 "Unlinking widgets"
412 ]
411 ]
413 },
412 },
414 {
413 {
415 "cell_type": "markdown",
414 "cell_type": "markdown",
416 "metadata": {
415 "metadata": {
417 "slideshow": {
416 "slideshow": {
418 "slide_type": "slide"
417 "slide_type": "slide"
419 }
418 }
420 },
419 },
421 "source": [
420 "source": [
422 "Unlinking the widgets is simple. All you have to do is call `.unlink` on the link object."
421 "Unlinking the widgets is simple. All you have to do is call `.unlink` on the link object."
423 ]
422 ]
424 },
423 },
425 {
424 {
426 "cell_type": "code",
425 "cell_type": "code",
427 "collapsed": false,
426 "collapsed": false,
428 "input": [
427 "input": [
429 "mylink.unlink()"
428 "mylink.unlink()"
430 ],
429 ],
431 "language": "python",
430 "language": "python",
432 "metadata": {},
431 "metadata": {},
433 "outputs": []
432 "outputs": []
434 },
433 },
435 {
434 {
436 "cell_type": "markdown",
435 "cell_type": "markdown",
437 "metadata": {},
436 "metadata": {},
438 "source": [
437 "source": [
439 "[Index](Index.ipynb) - [Next](Widget List.ipynb)"
438 "[Index](Index.ipynb) - [Next](Widget List.ipynb)"
440 ]
439 ]
441 }
440 }
442 ],
441 ],
443 "metadata": {}
442 "metadata": {}
444 }
443 }
445 ]
444 ]
446 } No newline at end of file
445 }
@@ -1,235 +1,234 b''
1 {
1 {
2 "metadata": {
2 "metadata": {
3 "cell_tags": [
3 "cell_tags": [
4 [
4 [
5 "<None>",
5 "<None>",
6 null
6 null
7 ]
7 ]
8 ],
8 ],
9 "celltoolbar": "Slideshow",
10 "kernelspec": {
9 "kernelspec": {
11 "codemirror_mode": {
10 "codemirror_mode": {
12 "name": "python",
11 "name": "python",
13 "version": 2
12 "version": 2
14 },
13 },
15 "display_name": "Python 2",
14 "display_name": "Python 2",
16 "language": "python",
15 "language": "python",
17 "name": "python2"
16 "name": "python2"
18 },
17 },
19 "name": "",
18 "name": "",
20 "signature": "sha256:0e46490eebb11503b7a7411065535d6fbc5ebcf61a519f0b030281f303086bdc"
19 "signature": "sha256:05a3e92089b37f68e3134587ffef6ef73830e5f8b3c515ba24640d7c803820c3"
21 },
20 },
22 "nbformat": 3,
21 "nbformat": 3,
23 "nbformat_minor": 0,
22 "nbformat_minor": 0,
24 "worksheets": [
23 "worksheets": [
25 {
24 {
26 "cells": [
25 "cells": [
27 {
26 {
28 "cell_type": "markdown",
27 "cell_type": "markdown",
29 "metadata": {},
28 "metadata": {},
30 "source": [
29 "source": [
31 "[Index](Index.ipynb) - [Back](Widget List.ipynb) - [Next](Widget Styling.ipynb)"
30 "[Index](Index.ipynb) - [Back](Widget List.ipynb) - [Next](Widget Styling.ipynb)"
32 ]
31 ]
33 },
32 },
34 {
33 {
35 "cell_type": "heading",
34 "cell_type": "heading",
36 "level": 1,
35 "level": 1,
37 "metadata": {
36 "metadata": {
38 "slideshow": {
37 "slideshow": {
39 "slide_type": "slide"
38 "slide_type": "slide"
40 }
39 }
41 },
40 },
42 "source": [
41 "source": [
43 "Widget Events"
42 "Widget Events"
44 ]
43 ]
45 },
44 },
46 {
45 {
47 "cell_type": "heading",
46 "cell_type": "heading",
48 "level": 2,
47 "level": 2,
49 "metadata": {},
48 "metadata": {},
50 "source": [
49 "source": [
51 "Special events"
50 "Special events"
52 ]
51 ]
53 },
52 },
54 {
53 {
55 "cell_type": "code",
54 "cell_type": "code",
56 "collapsed": false,
55 "collapsed": false,
57 "input": [
56 "input": [
58 "from __future__ import print_function"
57 "from __future__ import print_function"
59 ],
58 ],
60 "language": "python",
59 "language": "python",
61 "metadata": {},
60 "metadata": {},
62 "outputs": []
61 "outputs": []
63 },
62 },
64 {
63 {
65 "cell_type": "markdown",
64 "cell_type": "markdown",
66 "metadata": {},
65 "metadata": {},
67 "source": [
66 "source": [
68 "The `Button` is not used to represent a data type. Instead the button widget is used to **handle mouse clicks**. The **`on_click` method** of the `Button` 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."
67 "The `Button` is not used to represent a data type. Instead the button widget is used to **handle mouse clicks**. The **`on_click` method** of the `Button` 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."
69 ]
68 ]
70 },
69 },
71 {
70 {
72 "cell_type": "code",
71 "cell_type": "code",
73 "collapsed": false,
72 "collapsed": false,
74 "input": [
73 "input": [
75 "from IPython.html import widgets\n",
74 "from IPython.html import widgets\n",
76 "print(widgets.Button.on_click.__doc__)"
75 "print(widgets.Button.on_click.__doc__)"
77 ],
76 ],
78 "language": "python",
77 "language": "python",
79 "metadata": {},
78 "metadata": {},
80 "outputs": []
79 "outputs": []
81 },
80 },
82 {
81 {
83 "cell_type": "heading",
82 "cell_type": "heading",
84 "level": 3,
83 "level": 3,
85 "metadata": {
84 "metadata": {
86 "slideshow": {
85 "slideshow": {
87 "slide_type": "slide"
86 "slide_type": "slide"
88 }
87 }
89 },
88 },
90 "source": [
89 "source": [
91 "Example"
90 "Example"
92 ]
91 ]
93 },
92 },
94 {
93 {
95 "cell_type": "markdown",
94 "cell_type": "markdown",
96 "metadata": {},
95 "metadata": {},
97 "source": [
96 "source": [
98 "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."
97 "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."
99 ]
98 ]
100 },
99 },
101 {
100 {
102 "cell_type": "code",
101 "cell_type": "code",
103 "collapsed": false,
102 "collapsed": false,
104 "input": [
103 "input": [
105 "from IPython.display import display\n",
104 "from IPython.display import display\n",
106 "button = widgets.Button(description=\"Click Me!\")\n",
105 "button = widgets.Button(description=\"Click Me!\")\n",
107 "display(button)\n",
106 "display(button)\n",
108 "\n",
107 "\n",
109 "def on_button_clicked(b):\n",
108 "def on_button_clicked(b):\n",
110 " print(\"Button clicked.\")\n",
109 " print(\"Button clicked.\")\n",
111 "\n",
110 "\n",
112 "button.on_click(on_button_clicked)"
111 "button.on_click(on_button_clicked)"
113 ],
112 ],
114 "language": "python",
113 "language": "python",
115 "metadata": {},
114 "metadata": {},
116 "outputs": []
115 "outputs": []
117 },
116 },
118 {
117 {
119 "cell_type": "heading",
118 "cell_type": "heading",
120 "level": 3,
119 "level": 3,
121 "metadata": {
120 "metadata": {
122 "slideshow": {
121 "slideshow": {
123 "slide_type": "slide"
122 "slide_type": "slide"
124 }
123 }
125 },
124 },
126 "source": [
125 "source": [
127 "on_sumbit"
126 "on_sumbit"
128 ]
127 ]
129 },
128 },
130 {
129 {
131 "cell_type": "markdown",
130 "cell_type": "markdown",
132 "metadata": {},
131 "metadata": {},
133 "source": [
132 "source": [
134 "The **`Text`** also has a special **`on_submit` event**. The `on_submit` event **fires when the user hits return**."
133 "The **`Text`** also has a special **`on_submit` event**. The `on_submit` event **fires when the user hits return**."
135 ]
134 ]
136 },
135 },
137 {
136 {
138 "cell_type": "code",
137 "cell_type": "code",
139 "collapsed": false,
138 "collapsed": false,
140 "input": [
139 "input": [
141 "text = widgets.Text()\n",
140 "text = widgets.Text()\n",
142 "display(text)\n",
141 "display(text)\n",
143 "\n",
142 "\n",
144 "def handle_submit(sender):\n",
143 "def handle_submit(sender):\n",
145 " print(text.value)\n",
144 " print(text.value)\n",
146 "\n",
145 "\n",
147 "text.on_submit(handle_submit)"
146 "text.on_submit(handle_submit)"
148 ],
147 ],
149 "language": "python",
148 "language": "python",
150 "metadata": {},
149 "metadata": {},
151 "outputs": []
150 "outputs": []
152 },
151 },
153 {
152 {
154 "cell_type": "heading",
153 "cell_type": "heading",
155 "level": 2,
154 "level": 2,
156 "metadata": {
155 "metadata": {
157 "slideshow": {
156 "slideshow": {
158 "slide_type": "slide"
157 "slide_type": "slide"
159 }
158 }
160 },
159 },
161 "source": [
160 "source": [
162 "Traitlet events"
161 "Traitlet events"
163 ]
162 ]
164 },
163 },
165 {
164 {
166 "cell_type": "markdown",
165 "cell_type": "markdown",
167 "metadata": {},
166 "metadata": {},
168 "source": [
167 "source": [
169 "**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."
168 "**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."
170 ]
169 ]
171 },
170 },
172 {
171 {
173 "cell_type": "code",
172 "cell_type": "code",
174 "collapsed": false,
173 "collapsed": false,
175 "input": [
174 "input": [
176 "print(widgets.Widget.on_trait_change.__doc__)"
175 "print(widgets.Widget.on_trait_change.__doc__)"
177 ],
176 ],
178 "language": "python",
177 "language": "python",
179 "metadata": {},
178 "metadata": {},
180 "outputs": []
179 "outputs": []
181 },
180 },
182 {
181 {
183 "cell_type": "heading",
182 "cell_type": "heading",
184 "level": 3,
183 "level": 3,
185 "metadata": {
184 "metadata": {
186 "slideshow": {
185 "slideshow": {
187 "slide_type": "slide"
186 "slide_type": "slide"
188 }
187 }
189 },
188 },
190 "source": [
189 "source": [
191 "Signatures"
190 "Signatures"
192 ]
191 ]
193 },
192 },
194 {
193 {
195 "cell_type": "markdown",
194 "cell_type": "markdown",
196 "metadata": {},
195 "metadata": {},
197 "source": [
196 "source": [
198 "Mentioned in the doc string, the callback registered can have **4 possible signatures**:\n",
197 "Mentioned in the doc string, the callback registered can have **4 possible signatures**:\n",
199 "\n",
198 "\n",
200 "- callback()\n",
199 "- callback()\n",
201 "- callback(trait_name)\n",
200 "- callback(trait_name)\n",
202 "- callback(trait_name, new_value)\n",
201 "- callback(trait_name, new_value)\n",
203 "- callback(trait_name, old_value, new_value)\n",
202 "- callback(trait_name, old_value, new_value)\n",
204 "\n",
203 "\n",
205 "Using this method, an example of how to output an `IntSlider`'s value as it is changed can be seen below."
204 "Using this method, an example of how to output an `IntSlider`'s value as it is changed can be seen below."
206 ]
205 ]
207 },
206 },
208 {
207 {
209 "cell_type": "code",
208 "cell_type": "code",
210 "collapsed": false,
209 "collapsed": false,
211 "input": [
210 "input": [
212 "int_range = widgets.IntSlider()\n",
211 "int_range = widgets.IntSlider()\n",
213 "display(int_range)\n",
212 "display(int_range)\n",
214 "\n",
213 "\n",
215 "def on_value_change(name, value):\n",
214 "def on_value_change(name, value):\n",
216 " print(value)\n",
215 " print(value)\n",
217 "\n",
216 "\n",
218 "int_range.on_trait_change(on_value_change, 'value')"
217 "int_range.on_trait_change(on_value_change, 'value')"
219 ],
218 ],
220 "language": "python",
219 "language": "python",
221 "metadata": {},
220 "metadata": {},
222 "outputs": []
221 "outputs": []
223 },
222 },
224 {
223 {
225 "cell_type": "markdown",
224 "cell_type": "markdown",
226 "metadata": {},
225 "metadata": {},
227 "source": [
226 "source": [
228 "[Index](Index.ipynb) - [Back](Widget List.ipynb) - [Next](Widget Styling.ipynb)"
227 "[Index](Index.ipynb) - [Back](Widget List.ipynb) - [Next](Widget Styling.ipynb)"
229 ]
228 ]
230 }
229 }
231 ],
230 ],
232 "metadata": {}
231 "metadata": {}
233 }
232 }
234 ]
233 ]
235 } No newline at end of file
234 }
@@ -1,588 +1,587 b''
1 {
1 {
2 "metadata": {
2 "metadata": {
3 "celltoolbar": "Slideshow",
4 "kernelspec": {
3 "kernelspec": {
5 "codemirror_mode": {
4 "codemirror_mode": {
6 "name": "python",
5 "name": "python",
7 "version": 2
6 "version": 2
8 },
7 },
9 "display_name": "Python 2",
8 "display_name": "Python 2",
10 "language": "python",
9 "language": "python",
11 "name": "python2"
10 "name": "python2"
12 },
11 },
13 "name": "",
12 "name": "",
14 "signature": "sha256:869807fa658843bb4742bab3bab7ce4d164a0e3285e3bf840ad76ede2e252b79"
13 "signature": "sha256:83b39d018a7a6ae0a324b9f3d38debafbfb2ed0a114e4bbd357fb318f8f23438"
15 },
14 },
16 "nbformat": 3,
15 "nbformat": 3,
17 "nbformat_minor": 0,
16 "nbformat_minor": 0,
18 "worksheets": [
17 "worksheets": [
19 {
18 {
20 "cells": [
19 "cells": [
21 {
20 {
22 "cell_type": "markdown",
21 "cell_type": "markdown",
23 "metadata": {},
22 "metadata": {},
24 "source": [
23 "source": [
25 "[Index](Index.ipynb) - [Back](Widget Basics.ipynb) - [Next](Widget Events.ipynb)"
24 "[Index](Index.ipynb) - [Back](Widget Basics.ipynb) - [Next](Widget Events.ipynb)"
26 ]
25 ]
27 },
26 },
28 {
27 {
29 "cell_type": "heading",
28 "cell_type": "heading",
30 "level": 1,
29 "level": 1,
31 "metadata": {},
30 "metadata": {},
32 "source": [
31 "source": [
33 "Widget List"
32 "Widget List"
34 ]
33 ]
35 },
34 },
36 {
35 {
37 "cell_type": "heading",
36 "cell_type": "heading",
38 "level": 2,
37 "level": 2,
39 "metadata": {},
38 "metadata": {},
40 "source": [
39 "source": [
41 "Complete list"
40 "Complete list"
42 ]
41 ]
43 },
42 },
44 {
43 {
45 "cell_type": "markdown",
44 "cell_type": "markdown",
46 "metadata": {
45 "metadata": {
47 "slideshow": {
46 "slideshow": {
48 "slide_type": "slide"
47 "slide_type": "slide"
49 }
48 }
50 },
49 },
51 "source": [
50 "source": [
52 "For a complete list of the widgets available to you, you can list the classes in the widget namespace (as seen below). `Widget` and `DOMWidget`, not listed below, are base classes."
51 "For a complete list of the widgets available to you, you can list the classes in the widget namespace (as seen below). `Widget` and `DOMWidget`, not listed below, are base classes."
53 ]
52 ]
54 },
53 },
55 {
54 {
56 "cell_type": "code",
55 "cell_type": "code",
57 "collapsed": false,
56 "collapsed": false,
58 "input": [
57 "input": [
59 "from IPython.html import widgets\n",
58 "from IPython.html import widgets\n",
60 "[n for n in dir(widgets) if not n.endswith('Widget') and n[0] == n[0].upper() and not n[0] == '_']"
59 "[n for n in dir(widgets) if not n.endswith('Widget') and n[0] == n[0].upper() and not n[0] == '_']"
61 ],
60 ],
62 "language": "python",
61 "language": "python",
63 "metadata": {},
62 "metadata": {},
64 "outputs": []
63 "outputs": []
65 },
64 },
66 {
65 {
67 "cell_type": "heading",
66 "cell_type": "heading",
68 "level": 2,
67 "level": 2,
69 "metadata": {
68 "metadata": {
70 "slideshow": {
69 "slideshow": {
71 "slide_type": "slide"
70 "slide_type": "slide"
72 }
71 }
73 },
72 },
74 "source": [
73 "source": [
75 "Numeric widgets"
74 "Numeric widgets"
76 ]
75 ]
77 },
76 },
78 {
77 {
79 "cell_type": "markdown",
78 "cell_type": "markdown",
80 "metadata": {},
79 "metadata": {},
81 "source": [
80 "source": [
82 "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."
81 "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."
83 ]
82 ]
84 },
83 },
85 {
84 {
86 "cell_type": "heading",
85 "cell_type": "heading",
87 "level": 3,
86 "level": 3,
88 "metadata": {
87 "metadata": {
89 "slideshow": {
88 "slideshow": {
90 "slide_type": "slide"
89 "slide_type": "slide"
91 }
90 }
92 },
91 },
93 "source": [
92 "source": [
94 "FloatSlider"
93 "FloatSlider"
95 ]
94 ]
96 },
95 },
97 {
96 {
98 "cell_type": "code",
97 "cell_type": "code",
99 "collapsed": false,
98 "collapsed": false,
100 "input": [
99 "input": [
101 "widgets.FloatSlider(\n",
100 "widgets.FloatSlider(\n",
102 " value=7.5,\n",
101 " value=7.5,\n",
103 " min=5.0,\n",
102 " min=5.0,\n",
104 " max=10.0,\n",
103 " max=10.0,\n",
105 " step=0.1,\n",
104 " step=0.1,\n",
106 " description='Test:',\n",
105 " description='Test:',\n",
107 ")"
106 ")"
108 ],
107 ],
109 "language": "python",
108 "language": "python",
110 "metadata": {},
109 "metadata": {},
111 "outputs": []
110 "outputs": []
112 },
111 },
113 {
112 {
114 "cell_type": "markdown",
113 "cell_type": "markdown",
115 "metadata": {},
114 "metadata": {},
116 "source": [
115 "source": [
117 "Sliders can also be **displayed vertically**."
116 "Sliders can also be **displayed vertically**."
118 ]
117 ]
119 },
118 },
120 {
119 {
121 "cell_type": "code",
120 "cell_type": "code",
122 "collapsed": false,
121 "collapsed": false,
123 "input": [
122 "input": [
124 "widgets.FloatSlider(\n",
123 "widgets.FloatSlider(\n",
125 " value=7.5,\n",
124 " value=7.5,\n",
126 " min=5.0,\n",
125 " min=5.0,\n",
127 " max=10.0,\n",
126 " max=10.0,\n",
128 " step=0.1,\n",
127 " step=0.1,\n",
129 " description='Test',\n",
128 " description='Test',\n",
130 " orientation='vertical',\n",
129 " orientation='vertical',\n",
131 ")"
130 ")"
132 ],
131 ],
133 "language": "python",
132 "language": "python",
134 "metadata": {},
133 "metadata": {},
135 "outputs": []
134 "outputs": []
136 },
135 },
137 {
136 {
138 "cell_type": "heading",
137 "cell_type": "heading",
139 "level": 3,
138 "level": 3,
140 "metadata": {
139 "metadata": {
141 "slideshow": {
140 "slideshow": {
142 "slide_type": "slide"
141 "slide_type": "slide"
143 }
142 }
144 },
143 },
145 "source": [
144 "source": [
146 "FloatProgress"
145 "FloatProgress"
147 ]
146 ]
148 },
147 },
149 {
148 {
150 "cell_type": "code",
149 "cell_type": "code",
151 "collapsed": false,
150 "collapsed": false,
152 "input": [
151 "input": [
153 "widgets.FloatProgress(\n",
152 "widgets.FloatProgress(\n",
154 " value=7.5,\n",
153 " value=7.5,\n",
155 " min=5.0,\n",
154 " min=5.0,\n",
156 " max=10.0,\n",
155 " max=10.0,\n",
157 " step=0.1,\n",
156 " step=0.1,\n",
158 " description='Loading:',\n",
157 " description='Loading:',\n",
159 ")"
158 ")"
160 ],
159 ],
161 "language": "python",
160 "language": "python",
162 "metadata": {},
161 "metadata": {},
163 "outputs": []
162 "outputs": []
164 },
163 },
165 {
164 {
166 "cell_type": "heading",
165 "cell_type": "heading",
167 "level": 3,
166 "level": 3,
168 "metadata": {
167 "metadata": {
169 "slideshow": {
168 "slideshow": {
170 "slide_type": "slide"
169 "slide_type": "slide"
171 }
170 }
172 },
171 },
173 "source": [
172 "source": [
174 "BoundedFloatText"
173 "BoundedFloatText"
175 ]
174 ]
176 },
175 },
177 {
176 {
178 "cell_type": "code",
177 "cell_type": "code",
179 "collapsed": false,
178 "collapsed": false,
180 "input": [
179 "input": [
181 "widgets.BoundedFloatText(\n",
180 "widgets.BoundedFloatText(\n",
182 " value=7.5,\n",
181 " value=7.5,\n",
183 " min=5.0,\n",
182 " min=5.0,\n",
184 " max=10.0,\n",
183 " max=10.0,\n",
185 " description='Text:',\n",
184 " description='Text:',\n",
186 ")"
185 ")"
187 ],
186 ],
188 "language": "python",
187 "language": "python",
189 "metadata": {},
188 "metadata": {},
190 "outputs": []
189 "outputs": []
191 },
190 },
192 {
191 {
193 "cell_type": "heading",
192 "cell_type": "heading",
194 "level": 3,
193 "level": 3,
195 "metadata": {
194 "metadata": {
196 "slideshow": {
195 "slideshow": {
197 "slide_type": "slide"
196 "slide_type": "slide"
198 }
197 }
199 },
198 },
200 "source": [
199 "source": [
201 "FloatText"
200 "FloatText"
202 ]
201 ]
203 },
202 },
204 {
203 {
205 "cell_type": "code",
204 "cell_type": "code",
206 "collapsed": false,
205 "collapsed": false,
207 "input": [
206 "input": [
208 "widgets.FloatText(\n",
207 "widgets.FloatText(\n",
209 " value=7.5,\n",
208 " value=7.5,\n",
210 " description='Any:',\n",
209 " description='Any:',\n",
211 ")"
210 ")"
212 ],
211 ],
213 "language": "python",
212 "language": "python",
214 "metadata": {},
213 "metadata": {},
215 "outputs": []
214 "outputs": []
216 },
215 },
217 {
216 {
218 "cell_type": "heading",
217 "cell_type": "heading",
219 "level": 2,
218 "level": 2,
220 "metadata": {
219 "metadata": {
221 "slideshow": {
220 "slideshow": {
222 "slide_type": "slide"
221 "slide_type": "slide"
223 }
222 }
224 },
223 },
225 "source": [
224 "source": [
226 "Boolean widgets"
225 "Boolean widgets"
227 ]
226 ]
228 },
227 },
229 {
228 {
230 "cell_type": "markdown",
229 "cell_type": "markdown",
231 "metadata": {},
230 "metadata": {},
232 "source": [
231 "source": [
233 "There are two widgets that are designed to display a boolean value."
232 "There are two widgets that are designed to display a boolean value."
234 ]
233 ]
235 },
234 },
236 {
235 {
237 "cell_type": "heading",
236 "cell_type": "heading",
238 "level": 3,
237 "level": 3,
239 "metadata": {},
238 "metadata": {},
240 "source": [
239 "source": [
241 "ToggleButton"
240 "ToggleButton"
242 ]
241 ]
243 },
242 },
244 {
243 {
245 "cell_type": "code",
244 "cell_type": "code",
246 "collapsed": false,
245 "collapsed": false,
247 "input": [
246 "input": [
248 "widgets.ToggleButton(\n",
247 "widgets.ToggleButton(\n",
249 " description='Click me',\n",
248 " description='Click me',\n",
250 " value=False,\n",
249 " value=False,\n",
251 ")"
250 ")"
252 ],
251 ],
253 "language": "python",
252 "language": "python",
254 "metadata": {},
253 "metadata": {},
255 "outputs": []
254 "outputs": []
256 },
255 },
257 {
256 {
258 "cell_type": "heading",
257 "cell_type": "heading",
259 "level": 3,
258 "level": 3,
260 "metadata": {
259 "metadata": {
261 "slideshow": {
260 "slideshow": {
262 "slide_type": "slide"
261 "slide_type": "slide"
263 }
262 }
264 },
263 },
265 "source": [
264 "source": [
266 "Checkbox"
265 "Checkbox"
267 ]
266 ]
268 },
267 },
269 {
268 {
270 "cell_type": "code",
269 "cell_type": "code",
271 "collapsed": false,
270 "collapsed": false,
272 "input": [
271 "input": [
273 "widgets.Checkbox(\n",
272 "widgets.Checkbox(\n",
274 " description='Check me',\n",
273 " description='Check me',\n",
275 " value=True,\n",
274 " value=True,\n",
276 ")"
275 ")"
277 ],
276 ],
278 "language": "python",
277 "language": "python",
279 "metadata": {},
278 "metadata": {},
280 "outputs": []
279 "outputs": []
281 },
280 },
282 {
281 {
283 "cell_type": "heading",
282 "cell_type": "heading",
284 "level": 2,
283 "level": 2,
285 "metadata": {
284 "metadata": {
286 "slideshow": {
285 "slideshow": {
287 "slide_type": "slide"
286 "slide_type": "slide"
288 }
287 }
289 },
288 },
290 "source": [
289 "source": [
291 "Selection widgets"
290 "Selection widgets"
292 ]
291 ]
293 },
292 },
294 {
293 {
295 "cell_type": "markdown",
294 "cell_type": "markdown",
296 "metadata": {},
295 "metadata": {},
297 "source": [
296 "source": [
298 "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."
297 "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."
299 ]
298 ]
300 },
299 },
301 {
300 {
302 "cell_type": "heading",
301 "cell_type": "heading",
303 "level": 3,
302 "level": 3,
304 "metadata": {
303 "metadata": {
305 "slideshow": {
304 "slideshow": {
306 "slide_type": "slide"
305 "slide_type": "slide"
307 }
306 }
308 },
307 },
309 "source": [
308 "source": [
310 "Dropdown"
309 "Dropdown"
311 ]
310 ]
312 },
311 },
313 {
312 {
314 "cell_type": "code",
313 "cell_type": "code",
315 "collapsed": false,
314 "collapsed": false,
316 "input": [
315 "input": [
317 "from IPython.display import display\n",
316 "from IPython.display import display\n",
318 "w = widgets.Dropdown(\n",
317 "w = widgets.Dropdown(\n",
319 " values=[1, 2, 3],\n",
318 " values=[1, 2, 3],\n",
320 " value=2,\n",
319 " value=2,\n",
321 " description='Number:',\n",
320 " description='Number:',\n",
322 ")\n",
321 ")\n",
323 "display(w)"
322 "display(w)"
324 ],
323 ],
325 "language": "python",
324 "language": "python",
326 "metadata": {},
325 "metadata": {},
327 "outputs": []
326 "outputs": []
328 },
327 },
329 {
328 {
330 "cell_type": "code",
329 "cell_type": "code",
331 "collapsed": false,
330 "collapsed": false,
332 "input": [
331 "input": [
333 "w.value"
332 "w.value"
334 ],
333 ],
335 "language": "python",
334 "language": "python",
336 "metadata": {},
335 "metadata": {},
337 "outputs": []
336 "outputs": []
338 },
337 },
339 {
338 {
340 "cell_type": "markdown",
339 "cell_type": "markdown",
341 "metadata": {},
340 "metadata": {},
342 "source": [
341 "source": [
343 "The following is also valid:"
342 "The following is also valid:"
344 ]
343 ]
345 },
344 },
346 {
345 {
347 "cell_type": "code",
346 "cell_type": "code",
348 "collapsed": false,
347 "collapsed": false,
349 "input": [
348 "input": [
350 "w = widgets.Dropdown(\n",
349 "w = widgets.Dropdown(\n",
351 " values={'One': 1, 'Two': 2, 'Three': 3},\n",
350 " values={'One': 1, 'Two': 2, 'Three': 3},\n",
352 " value=2,\n",
351 " value=2,\n",
353 " description='Number:',\n",
352 " description='Number:',\n",
354 ")\n",
353 ")\n",
355 "display(w)"
354 "display(w)"
356 ],
355 ],
357 "language": "python",
356 "language": "python",
358 "metadata": {},
357 "metadata": {},
359 "outputs": []
358 "outputs": []
360 },
359 },
361 {
360 {
362 "cell_type": "code",
361 "cell_type": "code",
363 "collapsed": false,
362 "collapsed": false,
364 "input": [
363 "input": [
365 "w.value"
364 "w.value"
366 ],
365 ],
367 "language": "python",
366 "language": "python",
368 "metadata": {},
367 "metadata": {},
369 "outputs": []
368 "outputs": []
370 },
369 },
371 {
370 {
372 "cell_type": "heading",
371 "cell_type": "heading",
373 "level": 3,
372 "level": 3,
374 "metadata": {
373 "metadata": {
375 "slideshow": {
374 "slideshow": {
376 "slide_type": "slide"
375 "slide_type": "slide"
377 }
376 }
378 },
377 },
379 "source": [
378 "source": [
380 "RadioButtons"
379 "RadioButtons"
381 ]
380 ]
382 },
381 },
383 {
382 {
384 "cell_type": "code",
383 "cell_type": "code",
385 "collapsed": false,
384 "collapsed": false,
386 "input": [
385 "input": [
387 "widgets.RadioButtons(\n",
386 "widgets.RadioButtons(\n",
388 " description='Pizza topping:',\n",
387 " description='Pizza topping:',\n",
389 " values=['pepperoni', 'pineapple', 'anchovies'],\n",
388 " values=['pepperoni', 'pineapple', 'anchovies'],\n",
390 ")"
389 ")"
391 ],
390 ],
392 "language": "python",
391 "language": "python",
393 "metadata": {},
392 "metadata": {},
394 "outputs": []
393 "outputs": []
395 },
394 },
396 {
395 {
397 "cell_type": "heading",
396 "cell_type": "heading",
398 "level": 3,
397 "level": 3,
399 "metadata": {
398 "metadata": {
400 "slideshow": {
399 "slideshow": {
401 "slide_type": "slide"
400 "slide_type": "slide"
402 }
401 }
403 },
402 },
404 "source": [
403 "source": [
405 "Select"
404 "Select"
406 ]
405 ]
407 },
406 },
408 {
407 {
409 "cell_type": "code",
408 "cell_type": "code",
410 "collapsed": false,
409 "collapsed": false,
411 "input": [
410 "input": [
412 "widgets.Select(\n",
411 "widgets.Select(\n",
413 " description='OS:',\n",
412 " description='OS:',\n",
414 " values=['Linux', 'Windows', 'OSX'],\n",
413 " values=['Linux', 'Windows', 'OSX'],\n",
415 ")"
414 ")"
416 ],
415 ],
417 "language": "python",
416 "language": "python",
418 "metadata": {},
417 "metadata": {},
419 "outputs": []
418 "outputs": []
420 },
419 },
421 {
420 {
422 "cell_type": "heading",
421 "cell_type": "heading",
423 "level": 3,
422 "level": 3,
424 "metadata": {
423 "metadata": {
425 "slideshow": {
424 "slideshow": {
426 "slide_type": "slide"
425 "slide_type": "slide"
427 }
426 }
428 },
427 },
429 "source": [
428 "source": [
430 "ToggleButtons"
429 "ToggleButtons"
431 ]
430 ]
432 },
431 },
433 {
432 {
434 "cell_type": "code",
433 "cell_type": "code",
435 "collapsed": false,
434 "collapsed": false,
436 "input": [
435 "input": [
437 "widgets.ToggleButtons(\n",
436 "widgets.ToggleButtons(\n",
438 " description='Speed:',\n",
437 " description='Speed:',\n",
439 " values=['Slow', 'Regular', 'Fast'],\n",
438 " values=['Slow', 'Regular', 'Fast'],\n",
440 ")"
439 ")"
441 ],
440 ],
442 "language": "python",
441 "language": "python",
443 "metadata": {},
442 "metadata": {},
444 "outputs": []
443 "outputs": []
445 },
444 },
446 {
445 {
447 "cell_type": "heading",
446 "cell_type": "heading",
448 "level": 2,
447 "level": 2,
449 "metadata": {
448 "metadata": {
450 "slideshow": {
449 "slideshow": {
451 "slide_type": "slide"
450 "slide_type": "slide"
452 }
451 }
453 },
452 },
454 "source": [
453 "source": [
455 "String widgets"
454 "String widgets"
456 ]
455 ]
457 },
456 },
458 {
457 {
459 "cell_type": "markdown",
458 "cell_type": "markdown",
460 "metadata": {},
459 "metadata": {},
461 "source": [
460 "source": [
462 "There are 4 widgets that can be used to display a string value. Of those, the **`Text` and `Textarea` widgets accept input**. The **`Latex` and `HTML` widgets display the string** as either Latex or HTML respectively, but **do not accept input**."
461 "There are 4 widgets that can be used to display a string value. Of those, the **`Text` and `Textarea` widgets accept input**. The **`Latex` and `HTML` widgets display the string** as either Latex or HTML respectively, but **do not accept input**."
463 ]
462 ]
464 },
463 },
465 {
464 {
466 "cell_type": "heading",
465 "cell_type": "heading",
467 "level": 3,
466 "level": 3,
468 "metadata": {
467 "metadata": {
469 "slideshow": {
468 "slideshow": {
470 "slide_type": "slide"
469 "slide_type": "slide"
471 }
470 }
472 },
471 },
473 "source": [
472 "source": [
474 "Text"
473 "Text"
475 ]
474 ]
476 },
475 },
477 {
476 {
478 "cell_type": "code",
477 "cell_type": "code",
479 "collapsed": false,
478 "collapsed": false,
480 "input": [
479 "input": [
481 "widgets.Text(\n",
480 "widgets.Text(\n",
482 " description='String:',\n",
481 " description='String:',\n",
483 " value='Hello World',\n",
482 " value='Hello World',\n",
484 ")"
483 ")"
485 ],
484 ],
486 "language": "python",
485 "language": "python",
487 "metadata": {},
486 "metadata": {},
488 "outputs": []
487 "outputs": []
489 },
488 },
490 {
489 {
491 "cell_type": "heading",
490 "cell_type": "heading",
492 "level": 3,
491 "level": 3,
493 "metadata": {},
492 "metadata": {},
494 "source": [
493 "source": [
495 "Textarea"
494 "Textarea"
496 ]
495 ]
497 },
496 },
498 {
497 {
499 "cell_type": "code",
498 "cell_type": "code",
500 "collapsed": false,
499 "collapsed": false,
501 "input": [
500 "input": [
502 "widgets.Textarea(\n",
501 "widgets.Textarea(\n",
503 " description='String:',\n",
502 " description='String:',\n",
504 " value='Hello World',\n",
503 " value='Hello World',\n",
505 ")"
504 ")"
506 ],
505 ],
507 "language": "python",
506 "language": "python",
508 "metadata": {},
507 "metadata": {},
509 "outputs": []
508 "outputs": []
510 },
509 },
511 {
510 {
512 "cell_type": "heading",
511 "cell_type": "heading",
513 "level": 3,
512 "level": 3,
514 "metadata": {
513 "metadata": {
515 "slideshow": {
514 "slideshow": {
516 "slide_type": "slide"
515 "slide_type": "slide"
517 }
516 }
518 },
517 },
519 "source": [
518 "source": [
520 "Latex"
519 "Latex"
521 ]
520 ]
522 },
521 },
523 {
522 {
524 "cell_type": "code",
523 "cell_type": "code",
525 "collapsed": false,
524 "collapsed": false,
526 "input": [
525 "input": [
527 "widgets.Latex(\n",
526 "widgets.Latex(\n",
528 " value=\"$$\\\\frac{n!}{k!(n-k)!} = \\\\binom{n}{k}$$\",\n",
527 " value=\"$$\\\\frac{n!}{k!(n-k)!} = \\\\binom{n}{k}$$\",\n",
529 ")"
528 ")"
530 ],
529 ],
531 "language": "python",
530 "language": "python",
532 "metadata": {},
531 "metadata": {},
533 "outputs": []
532 "outputs": []
534 },
533 },
535 {
534 {
536 "cell_type": "heading",
535 "cell_type": "heading",
537 "level": 3,
536 "level": 3,
538 "metadata": {},
537 "metadata": {},
539 "source": [
538 "source": [
540 "HTML"
539 "HTML"
541 ]
540 ]
542 },
541 },
543 {
542 {
544 "cell_type": "code",
543 "cell_type": "code",
545 "collapsed": false,
544 "collapsed": false,
546 "input": [
545 "input": [
547 "widgets.HTML(\n",
546 "widgets.HTML(\n",
548 " value=\"Hello <b>World</b>\"\n",
547 " value=\"Hello <b>World</b>\"\n",
549 ")"
548 ")"
550 ],
549 ],
551 "language": "python",
550 "language": "python",
552 "metadata": {},
551 "metadata": {},
553 "outputs": []
552 "outputs": []
554 },
553 },
555 {
554 {
556 "cell_type": "heading",
555 "cell_type": "heading",
557 "level": 2,
556 "level": 2,
558 "metadata": {
557 "metadata": {
559 "slideshow": {
558 "slideshow": {
560 "slide_type": "slide"
559 "slide_type": "slide"
561 }
560 }
562 },
561 },
563 "source": [
562 "source": [
564 "Button"
563 "Button"
565 ]
564 ]
566 },
565 },
567 {
566 {
568 "cell_type": "code",
567 "cell_type": "code",
569 "collapsed": false,
568 "collapsed": false,
570 "input": [
569 "input": [
571 "widgets.Button(description='Click me')"
570 "widgets.Button(description='Click me')"
572 ],
571 ],
573 "language": "python",
572 "language": "python",
574 "metadata": {},
573 "metadata": {},
575 "outputs": []
574 "outputs": []
576 },
575 },
577 {
576 {
578 "cell_type": "markdown",
577 "cell_type": "markdown",
579 "metadata": {},
578 "metadata": {},
580 "source": [
579 "source": [
581 "[Index](Index.ipynb) - [Back](Widget Basics.ipynb) - [Next](Widget Events.ipynb)"
580 "[Index](Index.ipynb) - [Back](Widget Basics.ipynb) - [Next](Widget Events.ipynb)"
582 ]
581 ]
583 }
582 }
584 ],
583 ],
585 "metadata": {}
584 "metadata": {}
586 }
585 }
587 ]
586 ]
588 } No newline at end of file
587 }
@@ -1,738 +1,737 b''
1 {
1 {
2 "metadata": {
2 "metadata": {
3 "cell_tags": [
3 "cell_tags": [
4 [
4 [
5 "<None>",
5 "<None>",
6 null
6 null
7 ]
7 ]
8 ],
8 ],
9 "celltoolbar": "Slideshow",
10 "kernelspec": {
9 "kernelspec": {
11 "codemirror_mode": {
10 "codemirror_mode": {
12 "name": "python",
11 "name": "python",
13 "version": 2
12 "version": 2
14 },
13 },
15 "display_name": "Python 2",
14 "display_name": "Python 2",
16 "language": "python",
15 "language": "python",
17 "name": "python2"
16 "name": "python2"
18 },
17 },
19 "name": "",
18 "name": "",
20 "signature": "sha256:f6e8d46c3edeaf2371bf508730c093f8d1005cc9be13072aabf71dc3f49b2c3b"
19 "signature": "sha256:8bb091be85fd5e7f76082b1b4b98cddec92a856334935ac2c702fe5ec03f6eff"
21 },
20 },
22 "nbformat": 3,
21 "nbformat": 3,
23 "nbformat_minor": 0,
22 "nbformat_minor": 0,
24 "worksheets": [
23 "worksheets": [
25 {
24 {
26 "cells": [
25 "cells": [
27 {
26 {
28 "cell_type": "markdown",
27 "cell_type": "markdown",
29 "metadata": {},
28 "metadata": {},
30 "source": [
29 "source": [
31 "[Index](Index.ipynb) - [Back](Widget Events.ipynb) - [Next](Custom Widget - Hello World.ipynb)"
30 "[Index](Index.ipynb) - [Back](Widget Events.ipynb) - [Next](Custom Widget - Hello World.ipynb)"
32 ]
31 ]
33 },
32 },
34 {
33 {
35 "cell_type": "code",
34 "cell_type": "code",
36 "collapsed": false,
35 "collapsed": false,
37 "input": [
36 "input": [
38 "%%html\n",
37 "%%html\n",
39 "<style>\n",
38 "<style>\n",
40 ".example-container { background: #999999; padding: 2px; min-height: 100px; }\n",
39 ".example-container { background: #999999; padding: 2px; min-height: 100px; }\n",
41 ".example-container.sm { min-height: 50px; }\n",
40 ".example-container.sm { min-height: 50px; }\n",
42 ".example-box { background: #9999FF; width: 50px; height: 50px; text-align: center; vertical-align: middle; color: white; font-weight: bold; margin: 2px;}\n",
41 ".example-box { background: #9999FF; width: 50px; height: 50px; text-align: center; vertical-align: middle; color: white; font-weight: bold; margin: 2px;}\n",
43 ".example-box.med { width: 65px; height: 65px; } \n",
42 ".example-box.med { width: 65px; height: 65px; } \n",
44 ".example-box.lrg { width: 80px; height: 80px; } \n",
43 ".example-box.lrg { width: 80px; height: 80px; } \n",
45 "</style>"
44 "</style>"
46 ],
45 ],
47 "language": "python",
46 "language": "python",
48 "metadata": {},
47 "metadata": {},
49 "outputs": []
48 "outputs": []
50 },
49 },
51 {
50 {
52 "cell_type": "code",
51 "cell_type": "code",
53 "collapsed": false,
52 "collapsed": false,
54 "input": [
53 "input": [
55 "from IPython.html import widgets\n",
54 "from IPython.html import widgets\n",
56 "from IPython.display import display"
55 "from IPython.display import display"
57 ],
56 ],
58 "language": "python",
57 "language": "python",
59 "metadata": {},
58 "metadata": {},
60 "outputs": []
59 "outputs": []
61 },
60 },
62 {
61 {
63 "cell_type": "heading",
62 "cell_type": "heading",
64 "level": 1,
63 "level": 1,
65 "metadata": {
64 "metadata": {
66 "slideshow": {
65 "slideshow": {
67 "slide_type": "slide"
66 "slide_type": "slide"
68 }
67 }
69 },
68 },
70 "source": [
69 "source": [
71 "Widget Styling"
70 "Widget Styling"
72 ]
71 ]
73 },
72 },
74 {
73 {
75 "cell_type": "heading",
74 "cell_type": "heading",
76 "level": 2,
75 "level": 2,
77 "metadata": {},
76 "metadata": {},
78 "source": [
77 "source": [
79 "Basic styling"
78 "Basic styling"
80 ]
79 ]
81 },
80 },
82 {
81 {
83 "cell_type": "markdown",
82 "cell_type": "markdown",
84 "metadata": {},
83 "metadata": {},
85 "source": [
84 "source": [
86 "The widgets distributed with IPython can be styled by setting the following traits:\n",
85 "The widgets distributed with IPython can be styled by setting the following traits:\n",
87 "\n",
86 "\n",
88 "- width \n",
87 "- width \n",
89 "- height \n",
88 "- height \n",
90 "- fore_color \n",
89 "- fore_color \n",
91 "- back_color \n",
90 "- back_color \n",
92 "- border_color \n",
91 "- border_color \n",
93 "- border_width \n",
92 "- border_width \n",
94 "- border_style \n",
93 "- border_style \n",
95 "- font_style \n",
94 "- font_style \n",
96 "- font_weight \n",
95 "- font_weight \n",
97 "- font_size \n",
96 "- font_size \n",
98 "- font_family \n",
97 "- font_family \n",
99 "\n",
98 "\n",
100 "The example below shows how a `Button` widget can be styled:"
99 "The example below shows how a `Button` widget can be styled:"
101 ]
100 ]
102 },
101 },
103 {
102 {
104 "cell_type": "code",
103 "cell_type": "code",
105 "collapsed": false,
104 "collapsed": false,
106 "input": [
105 "input": [
107 "button = widgets.Button(\n",
106 "button = widgets.Button(\n",
108 " description='Hello World!',\n",
107 " description='Hello World!',\n",
109 " width=100, # Integers are interpreted as pixel measurements.\n",
108 " width=100, # Integers are interpreted as pixel measurements.\n",
110 " height='2em', # em is valid HTML unit of measurement.\n",
109 " height='2em', # em is valid HTML unit of measurement.\n",
111 " fore_color='lime', # Colors can be set by name,\n",
110 " color='lime', # Colors can be set by name,\n",
112 " back_color='#0022FF', # and also by color code.\n",
111 " background_color='#0022FF', # and also by color code.\n",
113 " border_color='red')\n",
112 " border_color='red')\n",
114 "display(button)"
113 "display(button)"
115 ],
114 ],
116 "language": "python",
115 "language": "python",
117 "metadata": {},
116 "metadata": {},
118 "outputs": []
117 "outputs": []
119 },
118 },
120 {
119 {
121 "cell_type": "heading",
120 "cell_type": "heading",
122 "level": 2,
121 "level": 2,
123 "metadata": {
122 "metadata": {
124 "slideshow": {
123 "slideshow": {
125 "slide_type": "slide"
124 "slide_type": "slide"
126 }
125 }
127 },
126 },
128 "source": [
127 "source": [
129 "Parent/child relationships"
128 "Parent/child relationships"
130 ]
129 ]
131 },
130 },
132 {
131 {
133 "cell_type": "markdown",
132 "cell_type": "markdown",
134 "metadata": {},
133 "metadata": {},
135 "source": [
134 "source": [
136 "To display widget A inside widget B, widget A must be a child of widget B. 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."
135 "To display widget A inside widget B, widget A must be a child of widget B. 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."
137 ]
136 ]
138 },
137 },
139 {
138 {
140 "cell_type": "code",
139 "cell_type": "code",
141 "collapsed": false,
140 "collapsed": false,
142 "input": [
141 "input": [
143 "from IPython.display import display\n",
142 "from IPython.display import display\n",
144 "\n",
143 "\n",
145 "float_range = widgets.FloatSlider()\n",
144 "float_range = widgets.FloatSlider()\n",
146 "string = widgets.Text(value='hi')\n",
145 "string = widgets.Text(value='hi')\n",
147 "container = widgets.Box(children=[float_range, string])\n",
146 "container = widgets.Box(children=[float_range, string])\n",
148 "\n",
147 "\n",
149 "container.border_color = 'red'\n",
148 "container.border_color = 'red'\n",
150 "container.border_style = 'dotted'\n",
149 "container.border_style = 'dotted'\n",
151 "container.border_width = 3\n",
150 "container.border_width = 3\n",
152 "display(container) # Displays the `container` and all of it's children."
151 "display(container) # Displays the `container` and all of it's children."
153 ],
152 ],
154 "language": "python",
153 "language": "python",
155 "metadata": {},
154 "metadata": {},
156 "outputs": []
155 "outputs": []
157 },
156 },
158 {
157 {
159 "cell_type": "heading",
158 "cell_type": "heading",
160 "level": 3,
159 "level": 3,
161 "metadata": {},
160 "metadata": {},
162 "source": [
161 "source": [
163 "After the parent is displayed"
162 "After the parent is displayed"
164 ]
163 ]
165 },
164 },
166 {
165 {
167 "cell_type": "markdown",
166 "cell_type": "markdown",
168 "metadata": {
167 "metadata": {
169 "slideshow": {
168 "slideshow": {
170 "slide_type": "slide"
169 "slide_type": "slide"
171 }
170 }
172 },
171 },
173 "source": [
172 "source": [
174 "Children **can be added to parents** after the parent has been displayed. The **parent is responsible for rendering its children**."
173 "Children **can be added to parents** after the parent has been displayed. The **parent is responsible for rendering its children**."
175 ]
174 ]
176 },
175 },
177 {
176 {
178 "cell_type": "code",
177 "cell_type": "code",
179 "collapsed": false,
178 "collapsed": false,
180 "input": [
179 "input": [
181 "container = widgets.Box()\n",
180 "container = widgets.Box()\n",
182 "container.border_color = 'red'\n",
181 "container.border_color = 'red'\n",
183 "container.border_style = 'dotted'\n",
182 "container.border_style = 'dotted'\n",
184 "container.border_width = 3\n",
183 "container.border_width = 3\n",
185 "display(container)\n",
184 "display(container)\n",
186 "\n",
185 "\n",
187 "int_range = widgets.IntSlider()\n",
186 "int_range = widgets.IntSlider()\n",
188 "container.children=[int_range]"
187 "container.children=[int_range]"
189 ],
188 ],
190 "language": "python",
189 "language": "python",
191 "metadata": {},
190 "metadata": {},
192 "outputs": []
191 "outputs": []
193 },
192 },
194 {
193 {
195 "cell_type": "heading",
194 "cell_type": "heading",
196 "level": 2,
195 "level": 2,
197 "metadata": {
196 "metadata": {
198 "slideshow": {
197 "slideshow": {
199 "slide_type": "slide"
198 "slide_type": "slide"
200 }
199 }
201 },
200 },
202 "source": [
201 "source": [
203 "Fancy boxes"
202 "Fancy boxes"
204 ]
203 ]
205 },
204 },
206 {
205 {
207 "cell_type": "markdown",
206 "cell_type": "markdown",
208 "metadata": {},
207 "metadata": {},
209 "source": [
208 "source": [
210 "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 **`Accordion` or a `Tab` in combination with one `Box` 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**."
209 "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 **`Accordion` or a `Tab` in combination with one `Box` 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**."
211 ]
210 ]
212 },
211 },
213 {
212 {
214 "cell_type": "heading",
213 "cell_type": "heading",
215 "level": 3,
214 "level": 3,
216 "metadata": {},
215 "metadata": {},
217 "source": [
216 "source": [
218 "Accordion"
217 "Accordion"
219 ]
218 ]
220 },
219 },
221 {
220 {
222 "cell_type": "code",
221 "cell_type": "code",
223 "collapsed": false,
222 "collapsed": false,
224 "input": [
223 "input": [
225 "name1 = widgets.Text(description='Location:')\n",
224 "name1 = widgets.Text(description='Location:')\n",
226 "zip1 = widgets.BoundedIntText(description='Zip:', min=0, max=99999)\n",
225 "zip1 = widgets.BoundedIntText(description='Zip:', min=0, max=99999)\n",
227 "page1 = widgets.Box(children=[name1, zip1])\n",
226 "page1 = widgets.Box(children=[name1, zip1])\n",
228 "\n",
227 "\n",
229 "name2 = widgets.Text(description='Location:')\n",
228 "name2 = widgets.Text(description='Location:')\n",
230 "zip2 = widgets.BoundedIntText(description='Zip:', min=0, max=99999)\n",
229 "zip2 = widgets.BoundedIntText(description='Zip:', min=0, max=99999)\n",
231 "page2 = widgets.Box(children=[name2, zip2])\n",
230 "page2 = widgets.Box(children=[name2, zip2])\n",
232 "\n",
231 "\n",
233 "accord = widgets.Accordion(children=[page1, page2])\n",
232 "accord = widgets.Accordion(children=[page1, page2])\n",
234 "display(accord)\n",
233 "display(accord)\n",
235 "\n",
234 "\n",
236 "accord.set_title(0, 'From')\n",
235 "accord.set_title(0, 'From')\n",
237 "accord.set_title(1, 'To')"
236 "accord.set_title(1, 'To')"
238 ],
237 ],
239 "language": "python",
238 "language": "python",
240 "metadata": {},
239 "metadata": {},
241 "outputs": []
240 "outputs": []
242 },
241 },
243 {
242 {
244 "cell_type": "heading",
243 "cell_type": "heading",
245 "level": 3,
244 "level": 3,
246 "metadata": {
245 "metadata": {
247 "slideshow": {
246 "slideshow": {
248 "slide_type": "slide"
247 "slide_type": "slide"
249 }
248 }
250 },
249 },
251 "source": [
250 "source": [
252 "TabWidget"
251 "TabWidget"
253 ]
252 ]
254 },
253 },
255 {
254 {
256 "cell_type": "code",
255 "cell_type": "code",
257 "collapsed": false,
256 "collapsed": false,
258 "input": [
257 "input": [
259 "name = widgets.Text(description='Name:')\n",
258 "name = widgets.Text(description='Name:')\n",
260 "color = widgets.Dropdown(description='Color:', values=['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet'])\n",
259 "color = widgets.Dropdown(description='Color:', values=['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet'])\n",
261 "page1 = widgets.Box(children=[name, color])\n",
260 "page1 = widgets.Box(children=[name, color])\n",
262 "\n",
261 "\n",
263 "age = widgets.IntSlider(description='Age:', min=0, max=120, value=50)\n",
262 "age = widgets.IntSlider(description='Age:', min=0, max=120, value=50)\n",
264 "gender = widgets.RadioButtons(description='Gender:', values=['male', 'female'])\n",
263 "gender = widgets.RadioButtons(description='Gender:', values=['male', 'female'])\n",
265 "page2 = widgets.Box(children=[age, gender])\n",
264 "page2 = widgets.Box(children=[age, gender])\n",
266 "\n",
265 "\n",
267 "tabs = widgets.Tab(children=[page1, page2])\n",
266 "tabs = widgets.Tab(children=[page1, page2])\n",
268 "display(tabs)\n",
267 "display(tabs)\n",
269 "\n",
268 "\n",
270 "tabs.set_title(0, 'Name')\n",
269 "tabs.set_title(0, 'Name')\n",
271 "tabs.set_title(1, 'Details')"
270 "tabs.set_title(1, 'Details')"
272 ],
271 ],
273 "language": "python",
272 "language": "python",
274 "metadata": {},
273 "metadata": {},
275 "outputs": []
274 "outputs": []
276 },
275 },
277 {
276 {
278 "cell_type": "heading",
277 "cell_type": "heading",
279 "level": 3,
278 "level": 3,
280 "metadata": {
279 "metadata": {
281 "slideshow": {
280 "slideshow": {
282 "slide_type": "slide"
281 "slide_type": "slide"
283 }
282 }
284 },
283 },
285 "source": [
284 "source": [
286 "Popup"
285 "Popup"
287 ]
286 ]
288 },
287 },
289 {
288 {
290 "cell_type": "markdown",
289 "cell_type": "markdown",
291 "metadata": {},
290 "metadata": {},
292 "source": [
291 "source": [
293 "Unlike the other two special containers, the `Popup` is only **designed to display one set of widgets**. The `Popup` can be used to **display widgets outside of the widget area**. "
292 "Unlike the other two special containers, the `Popup` is only **designed to display one set of widgets**. The `Popup` can be used to **display widgets outside of the widget area**. "
294 ]
293 ]
295 },
294 },
296 {
295 {
297 "cell_type": "code",
296 "cell_type": "code",
298 "collapsed": false,
297 "collapsed": false,
299 "input": [
298 "input": [
300 "counter = widgets.IntText(description='Counter:')\n",
299 "counter = widgets.IntText(description='Counter:')\n",
301 "popup = widgets.Popup(children=[counter], description='Popup Demo', button_text='Popup Button')\n",
300 "popup = widgets.Popup(children=[counter], description='Popup Demo', button_text='Popup Button')\n",
302 "display(popup)"
301 "display(popup)"
303 ],
302 ],
304 "language": "python",
303 "language": "python",
305 "metadata": {},
304 "metadata": {},
306 "outputs": []
305 "outputs": []
307 },
306 },
308 {
307 {
309 "cell_type": "code",
308 "cell_type": "code",
310 "collapsed": false,
309 "collapsed": false,
311 "input": [
310 "input": [
312 "counter.value += 1"
311 "counter.value += 1"
313 ],
312 ],
314 "language": "python",
313 "language": "python",
315 "metadata": {},
314 "metadata": {},
316 "outputs": []
315 "outputs": []
317 },
316 },
318 {
317 {
319 "cell_type": "code",
318 "cell_type": "code",
320 "collapsed": false,
319 "collapsed": false,
321 "input": [],
320 "input": [],
322 "language": "python",
321 "language": "python",
323 "metadata": {},
322 "metadata": {},
324 "outputs": []
323 "outputs": []
325 },
324 },
326 {
325 {
327 "cell_type": "code",
326 "cell_type": "code",
328 "collapsed": false,
327 "collapsed": false,
329 "input": [],
328 "input": [],
330 "language": "python",
329 "language": "python",
331 "metadata": {},
330 "metadata": {},
332 "outputs": []
331 "outputs": []
333 },
332 },
334 {
333 {
335 "cell_type": "code",
334 "cell_type": "code",
336 "collapsed": false,
335 "collapsed": false,
337 "input": [],
336 "input": [],
338 "language": "python",
337 "language": "python",
339 "metadata": {},
338 "metadata": {},
340 "outputs": []
339 "outputs": []
341 },
340 },
342 {
341 {
343 "cell_type": "code",
342 "cell_type": "code",
344 "collapsed": false,
343 "collapsed": false,
345 "input": [],
344 "input": [],
346 "language": "python",
345 "language": "python",
347 "metadata": {},
346 "metadata": {},
348 "outputs": []
347 "outputs": []
349 },
348 },
350 {
349 {
351 "cell_type": "code",
350 "cell_type": "code",
352 "collapsed": false,
351 "collapsed": false,
353 "input": [],
352 "input": [],
354 "language": "python",
353 "language": "python",
355 "metadata": {},
354 "metadata": {},
356 "outputs": []
355 "outputs": []
357 },
356 },
358 {
357 {
359 "cell_type": "code",
358 "cell_type": "code",
360 "collapsed": false,
359 "collapsed": false,
361 "input": [],
360 "input": [],
362 "language": "python",
361 "language": "python",
363 "metadata": {},
362 "metadata": {},
364 "outputs": []
363 "outputs": []
365 },
364 },
366 {
365 {
367 "cell_type": "code",
366 "cell_type": "code",
368 "collapsed": false,
367 "collapsed": false,
369 "input": [],
368 "input": [],
370 "language": "python",
369 "language": "python",
371 "metadata": {},
370 "metadata": {},
372 "outputs": []
371 "outputs": []
373 },
372 },
374 {
373 {
375 "cell_type": "code",
374 "cell_type": "code",
376 "collapsed": false,
375 "collapsed": false,
377 "input": [],
376 "input": [],
378 "language": "python",
377 "language": "python",
379 "metadata": {},
378 "metadata": {},
380 "outputs": []
379 "outputs": []
381 },
380 },
382 {
381 {
383 "cell_type": "code",
382 "cell_type": "code",
384 "collapsed": false,
383 "collapsed": false,
385 "input": [],
384 "input": [],
386 "language": "python",
385 "language": "python",
387 "metadata": {},
386 "metadata": {},
388 "outputs": []
387 "outputs": []
389 },
388 },
390 {
389 {
391 "cell_type": "code",
390 "cell_type": "code",
392 "collapsed": false,
391 "collapsed": false,
393 "input": [],
392 "input": [],
394 "language": "python",
393 "language": "python",
395 "metadata": {},
394 "metadata": {},
396 "outputs": []
395 "outputs": []
397 },
396 },
398 {
397 {
399 "cell_type": "code",
398 "cell_type": "code",
400 "collapsed": false,
399 "collapsed": false,
401 "input": [],
400 "input": [],
402 "language": "python",
401 "language": "python",
403 "metadata": {},
402 "metadata": {},
404 "outputs": []
403 "outputs": []
405 },
404 },
406 {
405 {
407 "cell_type": "code",
406 "cell_type": "code",
408 "collapsed": false,
407 "collapsed": false,
409 "input": [],
408 "input": [],
410 "language": "python",
409 "language": "python",
411 "metadata": {},
410 "metadata": {},
412 "outputs": []
411 "outputs": []
413 },
412 },
414 {
413 {
415 "cell_type": "code",
414 "cell_type": "code",
416 "collapsed": false,
415 "collapsed": false,
417 "input": [],
416 "input": [],
418 "language": "python",
417 "language": "python",
419 "metadata": {},
418 "metadata": {},
420 "outputs": []
419 "outputs": []
421 },
420 },
422 {
421 {
423 "cell_type": "code",
422 "cell_type": "code",
424 "collapsed": false,
423 "collapsed": false,
425 "input": [],
424 "input": [],
426 "language": "python",
425 "language": "python",
427 "metadata": {},
426 "metadata": {},
428 "outputs": []
427 "outputs": []
429 },
428 },
430 {
429 {
431 "cell_type": "code",
430 "cell_type": "code",
432 "collapsed": false,
431 "collapsed": false,
433 "input": [
432 "input": [
434 "counter.value += 1"
433 "counter.value += 1"
435 ],
434 ],
436 "language": "python",
435 "language": "python",
437 "metadata": {},
436 "metadata": {},
438 "outputs": []
437 "outputs": []
439 },
438 },
440 {
439 {
441 "cell_type": "code",
440 "cell_type": "code",
442 "collapsed": false,
441 "collapsed": false,
443 "input": [
442 "input": [
444 "popup.close()"
443 "popup.close()"
445 ],
444 ],
446 "language": "python",
445 "language": "python",
447 "metadata": {},
446 "metadata": {},
448 "outputs": []
447 "outputs": []
449 },
448 },
450 {
449 {
451 "cell_type": "heading",
450 "cell_type": "heading",
452 "level": 1,
451 "level": 1,
453 "metadata": {
452 "metadata": {
454 "slideshow": {
453 "slideshow": {
455 "slide_type": "slide"
454 "slide_type": "slide"
456 }
455 }
457 },
456 },
458 "source": [
457 "source": [
459 "Alignment"
458 "Alignment"
460 ]
459 ]
461 },
460 },
462 {
461 {
463 "cell_type": "markdown",
462 "cell_type": "markdown",
464 "metadata": {},
463 "metadata": {},
465 "source": [
464 "source": [
466 "Most widgets have a **`description` attribute**, which allows a label for the widget to be defined.\n",
465 "Most widgets have a **`description` attribute**, which allows a label for the widget to be defined.\n",
467 "The label of the widget **has a fixed minimum width**.\n",
466 "The label of the widget **has a fixed minimum width**.\n",
468 "The text of the label is **always right aligned and the widget is left aligned**:"
467 "The text of the label is **always right aligned and the widget is left aligned**:"
469 ]
468 ]
470 },
469 },
471 {
470 {
472 "cell_type": "code",
471 "cell_type": "code",
473 "collapsed": false,
472 "collapsed": false,
474 "input": [
473 "input": [
475 "display(widgets.Text(description=\"a:\"))\n",
474 "display(widgets.Text(description=\"a:\"))\n",
476 "display(widgets.Text(description=\"aa:\"))\n",
475 "display(widgets.Text(description=\"aa:\"))\n",
477 "display(widgets.Text(description=\"aaa:\"))"
476 "display(widgets.Text(description=\"aaa:\"))"
478 ],
477 ],
479 "language": "python",
478 "language": "python",
480 "metadata": {},
479 "metadata": {},
481 "outputs": []
480 "outputs": []
482 },
481 },
483 {
482 {
484 "cell_type": "markdown",
483 "cell_type": "markdown",
485 "metadata": {
484 "metadata": {
486 "slideshow": {
485 "slideshow": {
487 "slide_type": "slide"
486 "slide_type": "slide"
488 }
487 }
489 },
488 },
490 "source": [
489 "source": [
491 "If a **label is longer** than the minimum width, the **widget is shifted to the right**:"
490 "If a **label is longer** than the minimum width, the **widget is shifted to the right**:"
492 ]
491 ]
493 },
492 },
494 {
493 {
495 "cell_type": "code",
494 "cell_type": "code",
496 "collapsed": false,
495 "collapsed": false,
497 "input": [
496 "input": [
498 "display(widgets.Text(description=\"a:\"))\n",
497 "display(widgets.Text(description=\"a:\"))\n",
499 "display(widgets.Text(description=\"aa:\"))\n",
498 "display(widgets.Text(description=\"aa:\"))\n",
500 "display(widgets.Text(description=\"aaa:\"))\n",
499 "display(widgets.Text(description=\"aaa:\"))\n",
501 "display(widgets.Text(description=\"aaaaaaaaaaaaaaaaaa:\"))"
500 "display(widgets.Text(description=\"aaaaaaaaaaaaaaaaaa:\"))"
502 ],
501 ],
503 "language": "python",
502 "language": "python",
504 "metadata": {},
503 "metadata": {},
505 "outputs": []
504 "outputs": []
506 },
505 },
507 {
506 {
508 "cell_type": "markdown",
507 "cell_type": "markdown",
509 "metadata": {
508 "metadata": {
510 "slideshow": {
509 "slideshow": {
511 "slide_type": "slide"
510 "slide_type": "slide"
512 }
511 }
513 },
512 },
514 "source": [
513 "source": [
515 "If a `description` is **not set** for the widget, the **label is not displayed**:"
514 "If a `description` is **not set** for the widget, the **label is not displayed**:"
516 ]
515 ]
517 },
516 },
518 {
517 {
519 "cell_type": "code",
518 "cell_type": "code",
520 "collapsed": false,
519 "collapsed": false,
521 "input": [
520 "input": [
522 "display(widgets.Text(description=\"a:\"))\n",
521 "display(widgets.Text(description=\"a:\"))\n",
523 "display(widgets.Text(description=\"aa:\"))\n",
522 "display(widgets.Text(description=\"aa:\"))\n",
524 "display(widgets.Text(description=\"aaa:\"))\n",
523 "display(widgets.Text(description=\"aaa:\"))\n",
525 "display(widgets.Text())"
524 "display(widgets.Text())"
526 ],
525 ],
527 "language": "python",
526 "language": "python",
528 "metadata": {},
527 "metadata": {},
529 "outputs": []
528 "outputs": []
530 },
529 },
531 {
530 {
532 "cell_type": "heading",
531 "cell_type": "heading",
533 "level": 2,
532 "level": 2,
534 "metadata": {
533 "metadata": {
535 "slideshow": {
534 "slideshow": {
536 "slide_type": "slide"
535 "slide_type": "slide"
537 }
536 }
538 },
537 },
539 "source": [
538 "source": [
540 "Flex boxes"
539 "Flex boxes"
541 ]
540 ]
542 },
541 },
543 {
542 {
544 "cell_type": "markdown",
543 "cell_type": "markdown",
545 "metadata": {},
544 "metadata": {},
546 "source": [
545 "source": [
547 "Widgets can be aligned using the `FlexBox`, `HBox`, and `VBox` widgets."
546 "Widgets can be aligned using the `FlexBox`, `HBox`, and `VBox` widgets."
548 ]
547 ]
549 },
548 },
550 {
549 {
551 "cell_type": "heading",
550 "cell_type": "heading",
552 "level": 3,
551 "level": 3,
553 "metadata": {
552 "metadata": {
554 "slideshow": {
553 "slideshow": {
555 "slide_type": "slide"
554 "slide_type": "slide"
556 }
555 }
557 },
556 },
558 "source": [
557 "source": [
559 "Application to widgets"
558 "Application to widgets"
560 ]
559 ]
561 },
560 },
562 {
561 {
563 "cell_type": "markdown",
562 "cell_type": "markdown",
564 "metadata": {},
563 "metadata": {},
565 "source": [
564 "source": [
566 "Widgets display vertically by default:"
565 "Widgets display vertically by default:"
567 ]
566 ]
568 },
567 },
569 {
568 {
570 "cell_type": "code",
569 "cell_type": "code",
571 "collapsed": false,
570 "collapsed": false,
572 "input": [
571 "input": [
573 "buttons = [widgets.Button(description=str(i)) for i in range(3)]\n",
572 "buttons = [widgets.Button(description=str(i)) for i in range(3)]\n",
574 "display(*buttons)"
573 "display(*buttons)"
575 ],
574 ],
576 "language": "python",
575 "language": "python",
577 "metadata": {},
576 "metadata": {},
578 "outputs": []
577 "outputs": []
579 },
578 },
580 {
579 {
581 "cell_type": "heading",
580 "cell_type": "heading",
582 "level": 3,
581 "level": 3,
583 "metadata": {
582 "metadata": {
584 "slideshow": {
583 "slideshow": {
585 "slide_type": "slide"
584 "slide_type": "slide"
586 }
585 }
587 },
586 },
588 "source": [
587 "source": [
589 "Using hbox"
588 "Using hbox"
590 ]
589 ]
591 },
590 },
592 {
591 {
593 "cell_type": "markdown",
592 "cell_type": "markdown",
594 "metadata": {},
593 "metadata": {},
595 "source": [
594 "source": [
596 "To make widgets display horizontally, you need to **child them to a `HBox` widget**."
595 "To make widgets display horizontally, you need to **child them to a `HBox` widget**."
597 ]
596 ]
598 },
597 },
599 {
598 {
600 "cell_type": "code",
599 "cell_type": "code",
601 "collapsed": false,
600 "collapsed": false,
602 "input": [
601 "input": [
603 "container = widgets.HBox(children=buttons)\n",
602 "container = widgets.HBox(children=buttons)\n",
604 "display(container)"
603 "display(container)"
605 ],
604 ],
606 "language": "python",
605 "language": "python",
607 "metadata": {},
606 "metadata": {},
608 "outputs": []
607 "outputs": []
609 },
608 },
610 {
609 {
611 "cell_type": "markdown",
610 "cell_type": "markdown",
612 "metadata": {},
611 "metadata": {},
613 "source": [
612 "source": [
614 "By setting the width of the container to 100% and its `pack` to `center`, you can center the buttons."
613 "By setting the width of the container to 100% and its `pack` to `center`, you can center the buttons."
615 ]
614 ]
616 },
615 },
617 {
616 {
618 "cell_type": "code",
617 "cell_type": "code",
619 "collapsed": false,
618 "collapsed": false,
620 "input": [
619 "input": [
621 "container.width = '100%'\n",
620 "container.width = '100%'\n",
622 "container.pack = 'center'"
621 "container.pack = 'center'"
623 ],
622 ],
624 "language": "python",
623 "language": "python",
625 "metadata": {},
624 "metadata": {},
626 "outputs": []
625 "outputs": []
627 },
626 },
628 {
627 {
629 "cell_type": "heading",
628 "cell_type": "heading",
630 "level": 2,
629 "level": 2,
631 "metadata": {
630 "metadata": {
632 "slideshow": {
631 "slideshow": {
633 "slide_type": "slide"
632 "slide_type": "slide"
634 }
633 }
635 },
634 },
636 "source": [
635 "source": [
637 "Visibility"
636 "Visibility"
638 ]
637 ]
639 },
638 },
640 {
639 {
641 "cell_type": "markdown",
640 "cell_type": "markdown",
642 "metadata": {},
641 "metadata": {},
643 "source": [
642 "source": [
644 "Sometimes it is necessary to **hide or show widgets** in place, **without having to re-display** the widget.\n",
643 "Sometimes it is necessary to **hide or show widgets** in place, **without having to re-display** the widget.\n",
645 "The `visibility` property of widgets can be used to hide or show **widgets that have already been displayed** (as seen below)."
644 "The `visibility` property of widgets can be used to hide or show **widgets that have already been displayed** (as seen below)."
646 ]
645 ]
647 },
646 },
648 {
647 {
649 "cell_type": "code",
648 "cell_type": "code",
650 "collapsed": false,
649 "collapsed": false,
651 "input": [
650 "input": [
652 "string = widgets.Latex(value=\"Hello World!\")\n",
651 "string = widgets.Latex(value=\"Hello World!\")\n",
653 "display(string) "
652 "display(string) "
654 ],
653 ],
655 "language": "python",
654 "language": "python",
656 "metadata": {},
655 "metadata": {},
657 "outputs": []
656 "outputs": []
658 },
657 },
659 {
658 {
660 "cell_type": "code",
659 "cell_type": "code",
661 "collapsed": false,
660 "collapsed": false,
662 "input": [
661 "input": [
663 "string.visible=False"
662 "string.visible=False"
664 ],
663 ],
665 "language": "python",
664 "language": "python",
666 "metadata": {},
665 "metadata": {},
667 "outputs": []
666 "outputs": []
668 },
667 },
669 {
668 {
670 "cell_type": "code",
669 "cell_type": "code",
671 "collapsed": false,
670 "collapsed": false,
672 "input": [
671 "input": [
673 "string.visible=True"
672 "string.visible=True"
674 ],
673 ],
675 "language": "python",
674 "language": "python",
676 "metadata": {},
675 "metadata": {},
677 "outputs": []
676 "outputs": []
678 },
677 },
679 {
678 {
680 "cell_type": "heading",
679 "cell_type": "heading",
681 "level": 3,
680 "level": 3,
682 "metadata": {
681 "metadata": {
683 "slideshow": {
682 "slideshow": {
684 "slide_type": "slide"
683 "slide_type": "slide"
685 }
684 }
686 },
685 },
687 "source": [
686 "source": [
688 "Another example"
687 "Another example"
689 ]
688 ]
690 },
689 },
691 {
690 {
692 "cell_type": "markdown",
691 "cell_type": "markdown",
693 "metadata": {},
692 "metadata": {},
694 "source": [
693 "source": [
695 "In the example below, a form is rendered, which conditionally displays widgets depending on the state of other widgets. Try toggling the student checkbox."
694 "In the example below, a form is rendered, which conditionally displays widgets depending on the state of other widgets. Try toggling the student checkbox."
696 ]
695 ]
697 },
696 },
698 {
697 {
699 "cell_type": "code",
698 "cell_type": "code",
700 "collapsed": false,
699 "collapsed": false,
701 "input": [
700 "input": [
702 "form = widgets.VBox()\n",
701 "form = widgets.VBox()\n",
703 "first = widgets.Text(description=\"First Name:\")\n",
702 "first = widgets.Text(description=\"First Name:\")\n",
704 "last = widgets.Text(description=\"Last Name:\")\n",
703 "last = widgets.Text(description=\"Last Name:\")\n",
705 "\n",
704 "\n",
706 "student = widgets.Checkbox(description=\"Student:\", value=False)\n",
705 "student = widgets.Checkbox(description=\"Student:\", value=False)\n",
707 "school_info = widgets.VBox(visible=False, children=[\n",
706 "school_info = widgets.VBox(visible=False, children=[\n",
708 " widgets.Text(description=\"School:\"),\n",
707 " widgets.Text(description=\"School:\"),\n",
709 " widgets.IntText(description=\"Grade:\", min=0, max=12)\n",
708 " widgets.IntText(description=\"Grade:\", min=0, max=12)\n",
710 " ])\n",
709 " ])\n",
711 "\n",
710 "\n",
712 "pet = widgets.Text(description=\"Pet's Name:\")\n",
711 "pet = widgets.Text(description=\"Pet's Name:\")\n",
713 "form.children = [first, last, student, school_info, pet]\n",
712 "form.children = [first, last, student, school_info, pet]\n",
714 "display(form)\n",
713 "display(form)\n",
715 "\n",
714 "\n",
716 "def on_student_toggle(name, value):\n",
715 "def on_student_toggle(name, value):\n",
717 " if value:\n",
716 " if value:\n",
718 " school_info.visible = True\n",
717 " school_info.visible = True\n",
719 " else:\n",
718 " else:\n",
720 " school_info.visible = False\n",
719 " school_info.visible = False\n",
721 "student.on_trait_change(on_student_toggle, 'value')\n"
720 "student.on_trait_change(on_student_toggle, 'value')\n"
722 ],
721 ],
723 "language": "python",
722 "language": "python",
724 "metadata": {},
723 "metadata": {},
725 "outputs": []
724 "outputs": []
726 },
725 },
727 {
726 {
728 "cell_type": "markdown",
727 "cell_type": "markdown",
729 "metadata": {},
728 "metadata": {},
730 "source": [
729 "source": [
731 "[Index](Index.ipynb) - [Back](Widget Events.ipynb) - [Next](Custom Widget - Hello World.ipynb)"
730 "[Index](Index.ipynb) - [Back](Widget Events.ipynb) - [Next](Custom Widget - Hello World.ipynb)"
732 ]
731 ]
733 }
732 }
734 ],
733 ],
735 "metadata": {}
734 "metadata": {}
736 }
735 }
737 ]
736 ]
738 } No newline at end of file
737 }
General Comments 0
You need to be logged in to leave comments. Login now