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