Show More
@@ -1,863 +1,738 | |||
|
1 | 1 | { |
|
2 | 2 | "metadata": { |
|
3 | 3 | "cell_tags": [ |
|
4 | 4 | [ |
|
5 | 5 | "<None>", |
|
6 | 6 | null |
|
7 | 7 | ] |
|
8 | 8 | ], |
|
9 | 9 | "celltoolbar": "Slideshow", |
|
10 | 10 | "kernelspec": { |
|
11 | 11 | "codemirror_mode": { |
|
12 | 12 | "name": "python", |
|
13 | 13 | "version": 2 |
|
14 | 14 | }, |
|
15 | 15 | "display_name": "Python 2", |
|
16 | 16 | "language": "python", |
|
17 | 17 | "name": "python2" |
|
18 | 18 | }, |
|
19 | 19 | "name": "", |
|
20 | "signature": "sha256:aead3efdf2770e2764fcf617c9e9ba6d40343cf25b01642fbd2a3e66de2b0d7c" | |
|
20 | "signature": "sha256:f6e8d46c3edeaf2371bf508730c093f8d1005cc9be13072aabf71dc3f49b2c3b" | |
|
21 | 21 | }, |
|
22 | 22 | "nbformat": 3, |
|
23 | 23 | "nbformat_minor": 0, |
|
24 | 24 | "worksheets": [ |
|
25 | 25 | { |
|
26 | 26 | "cells": [ |
|
27 | 27 | { |
|
28 | 28 | "cell_type": "markdown", |
|
29 | 29 | "metadata": {}, |
|
30 | 30 | "source": [ |
|
31 | 31 | "[Index](Index.ipynb) - [Back](Widget Events.ipynb) - [Next](Custom Widget - Hello World.ipynb)" |
|
32 | 32 | ] |
|
33 | 33 | }, |
|
34 | 34 | { |
|
35 | 35 | "cell_type": "code", |
|
36 | 36 | "collapsed": false, |
|
37 | 37 | "input": [ |
|
38 | 38 | "%%html\n", |
|
39 | 39 | "<style>\n", |
|
40 | 40 | ".example-container { background: #999999; padding: 2px; min-height: 100px; }\n", |
|
41 | 41 | ".example-container.sm { min-height: 50px; }\n", |
|
42 | 42 | ".example-box { background: #9999FF; width: 50px; height: 50px; text-align: center; vertical-align: middle; color: white; font-weight: bold; margin: 2px;}\n", |
|
43 | 43 | ".example-box.med { width: 65px; height: 65px; } \n", |
|
44 | 44 | ".example-box.lrg { width: 80px; height: 80px; } \n", |
|
45 | 45 | "</style>" |
|
46 | 46 | ], |
|
47 | 47 | "language": "python", |
|
48 | 48 | "metadata": {}, |
|
49 | 49 | "outputs": [] |
|
50 | 50 | }, |
|
51 | 51 | { |
|
52 | 52 | "cell_type": "code", |
|
53 | 53 | "collapsed": false, |
|
54 | 54 | "input": [ |
|
55 | 55 | "from IPython.html import widgets\n", |
|
56 | 56 | "from IPython.display import display" |
|
57 | 57 | ], |
|
58 | 58 | "language": "python", |
|
59 | 59 | "metadata": {}, |
|
60 | 60 | "outputs": [] |
|
61 | 61 | }, |
|
62 | 62 | { |
|
63 | 63 | "cell_type": "heading", |
|
64 | 64 | "level": 1, |
|
65 | 65 | "metadata": { |
|
66 | 66 | "slideshow": { |
|
67 | 67 | "slide_type": "slide" |
|
68 | 68 | } |
|
69 | 69 | }, |
|
70 | 70 | "source": [ |
|
71 | 71 | "Widget Styling" |
|
72 | 72 | ] |
|
73 | 73 | }, |
|
74 | 74 | { |
|
75 | 75 | "cell_type": "heading", |
|
76 | 76 | "level": 2, |
|
77 | 77 | "metadata": {}, |
|
78 | 78 | "source": [ |
|
79 | 79 | "Basic styling" |
|
80 | 80 | ] |
|
81 | 81 | }, |
|
82 | 82 | { |
|
83 | 83 | "cell_type": "markdown", |
|
84 | 84 | "metadata": {}, |
|
85 | 85 | "source": [ |
|
86 | 86 | "The widgets distributed with IPython can be styled by setting the following traits:\n", |
|
87 | 87 | "\n", |
|
88 | 88 | "- width \n", |
|
89 | 89 | "- height \n", |
|
90 | 90 | "- fore_color \n", |
|
91 | 91 | "- back_color \n", |
|
92 | 92 | "- border_color \n", |
|
93 | 93 | "- border_width \n", |
|
94 | 94 | "- border_style \n", |
|
95 | 95 | "- font_style \n", |
|
96 | 96 | "- font_weight \n", |
|
97 | 97 | "- font_size \n", |
|
98 | 98 | "- font_family \n", |
|
99 | 99 | "\n", |
|
100 | 100 | "The example below shows how a `Button` widget can be styled:" |
|
101 | 101 | ] |
|
102 | 102 | }, |
|
103 | 103 | { |
|
104 | 104 | "cell_type": "code", |
|
105 | 105 | "collapsed": false, |
|
106 | 106 | "input": [ |
|
107 | 107 | "button = widgets.Button(\n", |
|
108 | 108 | " description='Hello World!',\n", |
|
109 | 109 | " width=100, # Integers are interpreted as pixel measurements.\n", |
|
110 | 110 | " height='2em', # em is valid HTML unit of measurement.\n", |
|
111 | 111 | " fore_color='lime', # Colors can be set by name,\n", |
|
112 | 112 | " back_color='#0022FF', # and also by color code.\n", |
|
113 | 113 | " border_color='red')\n", |
|
114 | 114 | "display(button)" |
|
115 | 115 | ], |
|
116 | 116 | "language": "python", |
|
117 | 117 | "metadata": {}, |
|
118 | 118 | "outputs": [] |
|
119 | 119 | }, |
|
120 | 120 | { |
|
121 | 121 | "cell_type": "heading", |
|
122 | 122 | "level": 2, |
|
123 | 123 | "metadata": { |
|
124 | 124 | "slideshow": { |
|
125 | 125 | "slide_type": "slide" |
|
126 | 126 | } |
|
127 | 127 | }, |
|
128 | 128 | "source": [ |
|
129 | 129 | "Parent/child relationships" |
|
130 | 130 | ] |
|
131 | 131 | }, |
|
132 | 132 | { |
|
133 | 133 | "cell_type": "markdown", |
|
134 | 134 | "metadata": {}, |
|
135 | 135 | "source": [ |
|
136 | 136 | "To display widget A inside widget B, widget A must be a child of widget B. Widgets that can contain other widgets have a **`children` attribute**. This attribute can be **set via a keyword argument** in the widget's constructor **or after construction**. Calling display on an **object with children automatically displays those children**, too." |
|
137 | 137 | ] |
|
138 | 138 | }, |
|
139 | 139 | { |
|
140 | 140 | "cell_type": "code", |
|
141 | 141 | "collapsed": false, |
|
142 | 142 | "input": [ |
|
143 | 143 | "from IPython.display import display\n", |
|
144 | 144 | "\n", |
|
145 | 145 | "float_range = widgets.FloatSlider()\n", |
|
146 | 146 | "string = widgets.Text(value='hi')\n", |
|
147 | 147 | "container = widgets.Box(children=[float_range, string])\n", |
|
148 | 148 | "\n", |
|
149 | 149 | "container.border_color = 'red'\n", |
|
150 | 150 | "container.border_style = 'dotted'\n", |
|
151 | 151 | "container.border_width = 3\n", |
|
152 | 152 | "display(container) # Displays the `container` and all of it's children." |
|
153 | 153 | ], |
|
154 | 154 | "language": "python", |
|
155 | 155 | "metadata": {}, |
|
156 | 156 | "outputs": [] |
|
157 | 157 | }, |
|
158 | 158 | { |
|
159 | 159 | "cell_type": "heading", |
|
160 | 160 | "level": 3, |
|
161 | 161 | "metadata": {}, |
|
162 | 162 | "source": [ |
|
163 | 163 | "After the parent is displayed" |
|
164 | 164 | ] |
|
165 | 165 | }, |
|
166 | 166 | { |
|
167 | 167 | "cell_type": "markdown", |
|
168 | 168 | "metadata": { |
|
169 | 169 | "slideshow": { |
|
170 | 170 | "slide_type": "slide" |
|
171 | 171 | } |
|
172 | 172 | }, |
|
173 | 173 | "source": [ |
|
174 | 174 | "Children **can be added to parents** after the parent has been displayed. The **parent is responsible for rendering its children**." |
|
175 | 175 | ] |
|
176 | 176 | }, |
|
177 | 177 | { |
|
178 | 178 | "cell_type": "code", |
|
179 | 179 | "collapsed": false, |
|
180 | 180 | "input": [ |
|
181 | 181 | "container = widgets.Box()\n", |
|
182 | 182 | "container.border_color = 'red'\n", |
|
183 | 183 | "container.border_style = 'dotted'\n", |
|
184 | 184 | "container.border_width = 3\n", |
|
185 | 185 | "display(container)\n", |
|
186 | 186 | "\n", |
|
187 | 187 | "int_range = widgets.IntSlider()\n", |
|
188 | 188 | "container.children=[int_range]" |
|
189 | 189 | ], |
|
190 | 190 | "language": "python", |
|
191 | 191 | "metadata": {}, |
|
192 | 192 | "outputs": [] |
|
193 | 193 | }, |
|
194 | 194 | { |
|
195 | 195 | "cell_type": "heading", |
|
196 | 196 | "level": 2, |
|
197 | 197 | "metadata": { |
|
198 | 198 | "slideshow": { |
|
199 | 199 | "slide_type": "slide" |
|
200 | 200 | } |
|
201 | 201 | }, |
|
202 | 202 | "source": [ |
|
203 | 203 | "Fancy boxes" |
|
204 | 204 | ] |
|
205 | 205 | }, |
|
206 | 206 | { |
|
207 | 207 | "cell_type": "markdown", |
|
208 | 208 | "metadata": {}, |
|
209 | 209 | "source": [ |
|
210 | 210 | "If you need to display a more complicated set of widgets, there are **specialized containers** that you can use. To display **multiple sets of widgets**, you can use an **`Accordion` or a `Tab` in combination with one `Box` per set of widgets** (as seen below). The \"pages\" of these widgets are their children. To set the titles of the pages, one must **call `set_title` after the widget has been displayed**." |
|
211 | 211 | ] |
|
212 | 212 | }, |
|
213 | 213 | { |
|
214 | 214 | "cell_type": "heading", |
|
215 | 215 | "level": 3, |
|
216 | 216 | "metadata": {}, |
|
217 | 217 | "source": [ |
|
218 | 218 | "Accordion" |
|
219 | 219 | ] |
|
220 | 220 | }, |
|
221 | 221 | { |
|
222 | 222 | "cell_type": "code", |
|
223 | 223 | "collapsed": false, |
|
224 | 224 | "input": [ |
|
225 | 225 | "name1 = widgets.Text(description='Location:')\n", |
|
226 | 226 | "zip1 = widgets.BoundedIntText(description='Zip:', min=0, max=99999)\n", |
|
227 | 227 | "page1 = widgets.Box(children=[name1, zip1])\n", |
|
228 | 228 | "\n", |
|
229 | 229 | "name2 = widgets.Text(description='Location:')\n", |
|
230 | 230 | "zip2 = widgets.BoundedIntText(description='Zip:', min=0, max=99999)\n", |
|
231 | 231 | "page2 = widgets.Box(children=[name2, zip2])\n", |
|
232 | 232 | "\n", |
|
233 | 233 | "accord = widgets.Accordion(children=[page1, page2])\n", |
|
234 | 234 | "display(accord)\n", |
|
235 | 235 | "\n", |
|
236 | 236 | "accord.set_title(0, 'From')\n", |
|
237 | 237 | "accord.set_title(1, 'To')" |
|
238 | 238 | ], |
|
239 | 239 | "language": "python", |
|
240 | 240 | "metadata": {}, |
|
241 | 241 | "outputs": [] |
|
242 | 242 | }, |
|
243 | 243 | { |
|
244 | 244 | "cell_type": "heading", |
|
245 | 245 | "level": 3, |
|
246 | 246 | "metadata": { |
|
247 | 247 | "slideshow": { |
|
248 | 248 | "slide_type": "slide" |
|
249 | 249 | } |
|
250 | 250 | }, |
|
251 | 251 | "source": [ |
|
252 | 252 | "TabWidget" |
|
253 | 253 | ] |
|
254 | 254 | }, |
|
255 | 255 | { |
|
256 | 256 | "cell_type": "code", |
|
257 | 257 | "collapsed": false, |
|
258 | 258 | "input": [ |
|
259 | 259 | "name = widgets.Text(description='Name:')\n", |
|
260 | 260 | "color = widgets.Dropdown(description='Color:', values=['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet'])\n", |
|
261 | 261 | "page1 = widgets.Box(children=[name, color])\n", |
|
262 | 262 | "\n", |
|
263 | 263 | "age = widgets.IntSlider(description='Age:', min=0, max=120, value=50)\n", |
|
264 | 264 | "gender = widgets.RadioButtons(description='Gender:', values=['male', 'female'])\n", |
|
265 | 265 | "page2 = widgets.Box(children=[age, gender])\n", |
|
266 | 266 | "\n", |
|
267 | 267 | "tabs = widgets.Tab(children=[page1, page2])\n", |
|
268 | 268 | "display(tabs)\n", |
|
269 | 269 | "\n", |
|
270 | 270 | "tabs.set_title(0, 'Name')\n", |
|
271 | 271 | "tabs.set_title(1, 'Details')" |
|
272 | 272 | ], |
|
273 | 273 | "language": "python", |
|
274 | 274 | "metadata": {}, |
|
275 | 275 | "outputs": [] |
|
276 | 276 | }, |
|
277 | 277 | { |
|
278 | 278 | "cell_type": "heading", |
|
279 | 279 | "level": 3, |
|
280 | 280 | "metadata": { |
|
281 | 281 | "slideshow": { |
|
282 | 282 | "slide_type": "slide" |
|
283 | 283 | } |
|
284 | 284 | }, |
|
285 | 285 | "source": [ |
|
286 | 286 | "Popup" |
|
287 | 287 | ] |
|
288 | 288 | }, |
|
289 | 289 | { |
|
290 | 290 | "cell_type": "markdown", |
|
291 | 291 | "metadata": {}, |
|
292 | 292 | "source": [ |
|
293 | 293 | "Unlike the other two special containers, the `Popup` is only **designed to display one set of widgets**. The `Popup` can be used to **display widgets outside of the widget area**. " |
|
294 | 294 | ] |
|
295 | 295 | }, |
|
296 | 296 | { |
|
297 | 297 | "cell_type": "code", |
|
298 | 298 | "collapsed": false, |
|
299 | 299 | "input": [ |
|
300 | 300 | "counter = widgets.IntText(description='Counter:')\n", |
|
301 | 301 | "popup = widgets.Popup(children=[counter], description='Popup Demo', button_text='Popup Button')\n", |
|
302 | 302 | "display(popup)" |
|
303 | 303 | ], |
|
304 | 304 | "language": "python", |
|
305 | 305 | "metadata": {}, |
|
306 | 306 | "outputs": [] |
|
307 | 307 | }, |
|
308 | 308 | { |
|
309 | 309 | "cell_type": "code", |
|
310 | 310 | "collapsed": false, |
|
311 | 311 | "input": [ |
|
312 | 312 | "counter.value += 1" |
|
313 | 313 | ], |
|
314 | 314 | "language": "python", |
|
315 | 315 | "metadata": {}, |
|
316 | 316 | "outputs": [] |
|
317 | 317 | }, |
|
318 | 318 | { |
|
319 | 319 | "cell_type": "code", |
|
320 | 320 | "collapsed": false, |
|
321 | 321 | "input": [], |
|
322 | 322 | "language": "python", |
|
323 | 323 | "metadata": {}, |
|
324 | 324 | "outputs": [] |
|
325 | 325 | }, |
|
326 | 326 | { |
|
327 | 327 | "cell_type": "code", |
|
328 | 328 | "collapsed": false, |
|
329 | 329 | "input": [], |
|
330 | 330 | "language": "python", |
|
331 | 331 | "metadata": {}, |
|
332 | 332 | "outputs": [] |
|
333 | 333 | }, |
|
334 | 334 | { |
|
335 | 335 | "cell_type": "code", |
|
336 | 336 | "collapsed": false, |
|
337 | 337 | "input": [], |
|
338 | 338 | "language": "python", |
|
339 | 339 | "metadata": {}, |
|
340 | 340 | "outputs": [] |
|
341 | 341 | }, |
|
342 | 342 | { |
|
343 | 343 | "cell_type": "code", |
|
344 | 344 | "collapsed": false, |
|
345 | 345 | "input": [], |
|
346 | 346 | "language": "python", |
|
347 | 347 | "metadata": {}, |
|
348 | 348 | "outputs": [] |
|
349 | 349 | }, |
|
350 | 350 | { |
|
351 | 351 | "cell_type": "code", |
|
352 | 352 | "collapsed": false, |
|
353 | 353 | "input": [], |
|
354 | 354 | "language": "python", |
|
355 | 355 | "metadata": {}, |
|
356 | 356 | "outputs": [] |
|
357 | 357 | }, |
|
358 | 358 | { |
|
359 | 359 | "cell_type": "code", |
|
360 | 360 | "collapsed": false, |
|
361 | 361 | "input": [], |
|
362 | 362 | "language": "python", |
|
363 | 363 | "metadata": {}, |
|
364 | 364 | "outputs": [] |
|
365 | 365 | }, |
|
366 | 366 | { |
|
367 | 367 | "cell_type": "code", |
|
368 | 368 | "collapsed": false, |
|
369 | 369 | "input": [], |
|
370 | 370 | "language": "python", |
|
371 | 371 | "metadata": {}, |
|
372 | 372 | "outputs": [] |
|
373 | 373 | }, |
|
374 | 374 | { |
|
375 | 375 | "cell_type": "code", |
|
376 | 376 | "collapsed": false, |
|
377 | 377 | "input": [], |
|
378 | 378 | "language": "python", |
|
379 | 379 | "metadata": {}, |
|
380 | 380 | "outputs": [] |
|
381 | 381 | }, |
|
382 | 382 | { |
|
383 | 383 | "cell_type": "code", |
|
384 | 384 | "collapsed": false, |
|
385 | 385 | "input": [], |
|
386 | 386 | "language": "python", |
|
387 | 387 | "metadata": {}, |
|
388 | 388 | "outputs": [] |
|
389 | 389 | }, |
|
390 | 390 | { |
|
391 | 391 | "cell_type": "code", |
|
392 | 392 | "collapsed": false, |
|
393 | 393 | "input": [], |
|
394 | 394 | "language": "python", |
|
395 | 395 | "metadata": {}, |
|
396 | 396 | "outputs": [] |
|
397 | 397 | }, |
|
398 | 398 | { |
|
399 | 399 | "cell_type": "code", |
|
400 | 400 | "collapsed": false, |
|
401 | 401 | "input": [], |
|
402 | 402 | "language": "python", |
|
403 | 403 | "metadata": {}, |
|
404 | 404 | "outputs": [] |
|
405 | 405 | }, |
|
406 | 406 | { |
|
407 | 407 | "cell_type": "code", |
|
408 | 408 | "collapsed": false, |
|
409 | 409 | "input": [], |
|
410 | 410 | "language": "python", |
|
411 | 411 | "metadata": {}, |
|
412 | 412 | "outputs": [] |
|
413 | 413 | }, |
|
414 | 414 | { |
|
415 | 415 | "cell_type": "code", |
|
416 | 416 | "collapsed": false, |
|
417 | 417 | "input": [], |
|
418 | 418 | "language": "python", |
|
419 | 419 | "metadata": {}, |
|
420 | 420 | "outputs": [] |
|
421 | 421 | }, |
|
422 | 422 | { |
|
423 | 423 | "cell_type": "code", |
|
424 | 424 | "collapsed": false, |
|
425 | 425 | "input": [], |
|
426 | 426 | "language": "python", |
|
427 | 427 | "metadata": {}, |
|
428 | 428 | "outputs": [] |
|
429 | 429 | }, |
|
430 | 430 | { |
|
431 | 431 | "cell_type": "code", |
|
432 | 432 | "collapsed": false, |
|
433 | 433 | "input": [ |
|
434 | 434 | "counter.value += 1" |
|
435 | 435 | ], |
|
436 | 436 | "language": "python", |
|
437 | 437 | "metadata": {}, |
|
438 | 438 | "outputs": [] |
|
439 | 439 | }, |
|
440 | 440 | { |
|
441 | 441 | "cell_type": "code", |
|
442 | 442 | "collapsed": false, |
|
443 | 443 | "input": [ |
|
444 | 444 | "popup.close()" |
|
445 | 445 | ], |
|
446 | 446 | "language": "python", |
|
447 | 447 | "metadata": {}, |
|
448 | 448 | "outputs": [] |
|
449 | 449 | }, |
|
450 | 450 | { |
|
451 | 451 | "cell_type": "heading", |
|
452 | 452 | "level": 1, |
|
453 | 453 | "metadata": { |
|
454 | 454 | "slideshow": { |
|
455 | 455 | "slide_type": "slide" |
|
456 | 456 | } |
|
457 | 457 | }, |
|
458 | 458 | "source": [ |
|
459 | 459 | "Alignment" |
|
460 | 460 | ] |
|
461 | 461 | }, |
|
462 | 462 | { |
|
463 | 463 | "cell_type": "markdown", |
|
464 | 464 | "metadata": {}, |
|
465 | 465 | "source": [ |
|
466 | 466 | "Most widgets have a **`description` attribute**, which allows a label for the widget to be defined.\n", |
|
467 | 467 | "The label of the widget **has a fixed minimum width**.\n", |
|
468 | 468 | "The text of the label is **always right aligned and the widget is left aligned**:" |
|
469 | 469 | ] |
|
470 | 470 | }, |
|
471 | 471 | { |
|
472 | 472 | "cell_type": "code", |
|
473 | 473 | "collapsed": false, |
|
474 | 474 | "input": [ |
|
475 | 475 | "display(widgets.Text(description=\"a:\"))\n", |
|
476 | 476 | "display(widgets.Text(description=\"aa:\"))\n", |
|
477 | 477 | "display(widgets.Text(description=\"aaa:\"))" |
|
478 | 478 | ], |
|
479 | 479 | "language": "python", |
|
480 | 480 | "metadata": {}, |
|
481 | 481 | "outputs": [] |
|
482 | 482 | }, |
|
483 | 483 | { |
|
484 | 484 | "cell_type": "markdown", |
|
485 | 485 | "metadata": { |
|
486 | 486 | "slideshow": { |
|
487 | 487 | "slide_type": "slide" |
|
488 | 488 | } |
|
489 | 489 | }, |
|
490 | 490 | "source": [ |
|
491 | 491 | "If a **label is longer** than the minimum width, the **widget is shifted to the right**:" |
|
492 | 492 | ] |
|
493 | 493 | }, |
|
494 | 494 | { |
|
495 | 495 | "cell_type": "code", |
|
496 | 496 | "collapsed": false, |
|
497 | 497 | "input": [ |
|
498 | 498 | "display(widgets.Text(description=\"a:\"))\n", |
|
499 | 499 | "display(widgets.Text(description=\"aa:\"))\n", |
|
500 | 500 | "display(widgets.Text(description=\"aaa:\"))\n", |
|
501 | 501 | "display(widgets.Text(description=\"aaaaaaaaaaaaaaaaaa:\"))" |
|
502 | 502 | ], |
|
503 | 503 | "language": "python", |
|
504 | 504 | "metadata": {}, |
|
505 | 505 | "outputs": [] |
|
506 | 506 | }, |
|
507 | 507 | { |
|
508 | 508 | "cell_type": "markdown", |
|
509 | 509 | "metadata": { |
|
510 | 510 | "slideshow": { |
|
511 | 511 | "slide_type": "slide" |
|
512 | 512 | } |
|
513 | 513 | }, |
|
514 | 514 | "source": [ |
|
515 | 515 | "If a `description` is **not set** for the widget, the **label is not displayed**:" |
|
516 | 516 | ] |
|
517 | 517 | }, |
|
518 | 518 | { |
|
519 | 519 | "cell_type": "code", |
|
520 | 520 | "collapsed": false, |
|
521 | 521 | "input": [ |
|
522 | 522 | "display(widgets.Text(description=\"a:\"))\n", |
|
523 | 523 | "display(widgets.Text(description=\"aa:\"))\n", |
|
524 | 524 | "display(widgets.Text(description=\"aaa:\"))\n", |
|
525 | 525 | "display(widgets.Text())" |
|
526 | 526 | ], |
|
527 | 527 | "language": "python", |
|
528 | 528 | "metadata": {}, |
|
529 | 529 | "outputs": [] |
|
530 | 530 | }, |
|
531 | 531 | { |
|
532 | 532 | "cell_type": "heading", |
|
533 | 533 | "level": 2, |
|
534 | 534 | "metadata": { |
|
535 | 535 | "slideshow": { |
|
536 | 536 | "slide_type": "slide" |
|
537 | 537 | } |
|
538 | 538 | }, |
|
539 | 539 | "source": [ |
|
540 | 540 | "Flex boxes" |
|
541 | 541 | ] |
|
542 | 542 | }, |
|
543 | 543 | { |
|
544 | 544 | "cell_type": "markdown", |
|
545 | 545 | "metadata": {}, |
|
546 | 546 | "source": [ |
|
547 | 547 | "Widgets can be aligned using the `FlexBox`, `HBox`, and `VBox` widgets." |
|
548 | 548 | ] |
|
549 | 549 | }, |
|
550 | 550 | { |
|
551 | 551 | "cell_type": "heading", |
|
552 | 552 | "level": 3, |
|
553 | 553 | "metadata": { |
|
554 | 554 | "slideshow": { |
|
555 | 555 | "slide_type": "slide" |
|
556 | 556 | } |
|
557 | 557 | }, |
|
558 | 558 | "source": [ |
|
559 | 559 | "Application to widgets" |
|
560 | 560 | ] |
|
561 | 561 | }, |
|
562 | 562 | { |
|
563 | 563 | "cell_type": "markdown", |
|
564 | 564 | "metadata": {}, |
|
565 | 565 | "source": [ |
|
566 | 566 | "Widgets display vertically by default:" |
|
567 | 567 | ] |
|
568 | 568 | }, |
|
569 | 569 | { |
|
570 | 570 | "cell_type": "code", |
|
571 | 571 | "collapsed": false, |
|
572 | 572 | "input": [ |
|
573 | 573 | "buttons = [widgets.Button(description=str(i)) for i in range(3)]\n", |
|
574 | 574 | "display(*buttons)" |
|
575 | 575 | ], |
|
576 | 576 | "language": "python", |
|
577 | 577 | "metadata": {}, |
|
578 | 578 | "outputs": [] |
|
579 | 579 | }, |
|
580 | 580 | { |
|
581 | 581 | "cell_type": "heading", |
|
582 | 582 | "level": 3, |
|
583 | 583 | "metadata": { |
|
584 | 584 | "slideshow": { |
|
585 | 585 | "slide_type": "slide" |
|
586 | 586 | } |
|
587 | 587 | }, |
|
588 | 588 | "source": [ |
|
589 | 589 | "Using hbox" |
|
590 | 590 | ] |
|
591 | 591 | }, |
|
592 | 592 | { |
|
593 | 593 | "cell_type": "markdown", |
|
594 | 594 | "metadata": {}, |
|
595 | 595 | "source": [ |
|
596 | 596 | "To make widgets display horizontally, you need to **child them to a `HBox` widget**." |
|
597 | 597 | ] |
|
598 | 598 | }, |
|
599 | 599 | { |
|
600 | 600 | "cell_type": "code", |
|
601 | 601 | "collapsed": false, |
|
602 | 602 | "input": [ |
|
603 | 603 | "container = widgets.HBox(children=buttons)\n", |
|
604 | 604 | "display(container)" |
|
605 | 605 | ], |
|
606 | 606 | "language": "python", |
|
607 | 607 | "metadata": {}, |
|
608 | 608 | "outputs": [] |
|
609 | 609 | }, |
|
610 | 610 | { |
|
611 | 611 | "cell_type": "markdown", |
|
612 | 612 | "metadata": {}, |
|
613 | 613 | "source": [ |
|
614 | 614 | "By setting the width of the container to 100% and its `pack` to `center`, you can center the buttons." |
|
615 | 615 | ] |
|
616 | 616 | }, |
|
617 | 617 | { |
|
618 | 618 | "cell_type": "code", |
|
619 | 619 | "collapsed": false, |
|
620 | 620 | "input": [ |
|
621 | 621 | "container.width = '100%'\n", |
|
622 | 622 | "container.pack = 'center'" |
|
623 | 623 | ], |
|
624 | 624 | "language": "python", |
|
625 | 625 | "metadata": {}, |
|
626 | 626 | "outputs": [] |
|
627 | 627 | }, |
|
628 | 628 | { |
|
629 | 629 | "cell_type": "heading", |
|
630 | 630 | "level": 2, |
|
631 | 631 | "metadata": { |
|
632 | 632 | "slideshow": { |
|
633 | 633 | "slide_type": "slide" |
|
634 | 634 | } |
|
635 | 635 | }, |
|
636 | 636 | "source": [ |
|
637 | "Style classes" | |
|
638 | ] | |
|
639 | }, | |
|
640 | { | |
|
641 | "cell_type": "markdown", | |
|
642 | "metadata": {}, | |
|
643 | "source": [ | |
|
644 | "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/)." | |
|
645 | ] | |
|
646 | }, | |
|
647 | { | |
|
648 | "cell_type": "heading", | |
|
649 | "level": 3, | |
|
650 | "metadata": {}, | |
|
651 | "source": [ | |
|
652 | "ButtonWidgets" | |
|
653 | ] | |
|
654 | }, | |
|
655 | { | |
|
656 | "cell_type": "code", | |
|
657 | "collapsed": false, | |
|
658 | "input": [ | |
|
659 | "# List of the bootstrap button styles\n", | |
|
660 | "classes = [\n", | |
|
661 | " 'btn', \n", | |
|
662 | " 'btn-primary', \n", | |
|
663 | " 'btn-info', \n", | |
|
664 | " 'btn-success', \n", | |
|
665 | " 'btn-warning', \n", | |
|
666 | " 'btn-danger', \n", | |
|
667 | " 'btn-inverse', \n", | |
|
668 | " 'btn-link'\n", | |
|
669 | "]\n", | |
|
670 | "\n", | |
|
671 | "# Display the buttons in a hbox\n", | |
|
672 | "container = widgets.HBox(children=[widgets.Button(description=c) for c in classes])\n", | |
|
673 | "display(container)\n", | |
|
674 | "\n", | |
|
675 | "# Apply classes after display\n", | |
|
676 | "ret = [container.children[i].add_class(c) for i, c in enumerate(classes)]" | |
|
677 | ], | |
|
678 | "language": "python", | |
|
679 | "metadata": {}, | |
|
680 | "outputs": [] | |
|
681 | }, | |
|
682 | { | |
|
683 | "cell_type": "heading", | |
|
684 | "level": 3, | |
|
685 | "metadata": { | |
|
686 | "slideshow": { | |
|
687 | "slide_type": "slide" | |
|
688 | } | |
|
689 | }, | |
|
690 | "source": [ | |
|
691 | "ContainerWidgets" | |
|
692 | ] | |
|
693 | }, | |
|
694 | { | |
|
695 | "cell_type": "code", | |
|
696 | "collapsed": false, | |
|
697 | "input": [ | |
|
698 | "def create_label(cls):\n", | |
|
699 | " class_name = widgets.HTML(value=cls)\n", | |
|
700 | " container = widgets.Box(children=[class_name])\n", | |
|
701 | " display(container)\n", | |
|
702 | " container.add_class(cls)\n", | |
|
703 | "\n", | |
|
704 | "ret = [create_label(c) for c in [\n", | |
|
705 | " 'alert', \n", | |
|
706 | " 'alert alert-error', \n", | |
|
707 | " 'alert alert-success', \n", | |
|
708 | " 'alert alert-info'\n", | |
|
709 | "]]" | |
|
710 | ], | |
|
711 | "language": "python", | |
|
712 | "metadata": {}, | |
|
713 | "outputs": [] | |
|
714 | }, | |
|
715 | { | |
|
716 | "cell_type": "heading", | |
|
717 | "level": 3, | |
|
718 | "metadata": { | |
|
719 | "slideshow": { | |
|
720 | "slide_type": "slide" | |
|
721 | } | |
|
722 | }, | |
|
723 | "source": [ | |
|
724 | "ProgressWidgets" | |
|
725 | ] | |
|
726 | }, | |
|
727 | { | |
|
728 | "cell_type": "code", | |
|
729 | "collapsed": false, | |
|
730 | "input": [ | |
|
731 | "classes = [\n", | |
|
732 | " 'progress-info', \n", | |
|
733 | " 'progress-success', \n", | |
|
734 | " 'progress-warning', \n", | |
|
735 | " 'progress-danger',\n", | |
|
736 | " 'progress-info progress-striped', \n", | |
|
737 | " 'progress-success progress-striped', \n", | |
|
738 | " 'progress-warning progress-striped', \n", | |
|
739 | " 'progress-danger progress-striped',\n", | |
|
740 | " 'active progress-info progress-striped', \n", | |
|
741 | " 'active progress-success progress-striped', \n", | |
|
742 | " 'active progress-warning progress-striped', \n", | |
|
743 | " 'active progress-danger progress-striped',\n", | |
|
744 | "]\n", | |
|
745 | "ws = [widgets.IntProgress(value=50, description=c) for c in classes]\n", | |
|
746 | "ret = [display(w) for w in ws]\n", | |
|
747 | "ret = [ws[i].add_class(c) for i, cs in enumerate(classes) for c in cs.split(' ')]" | |
|
748 | ], | |
|
749 | "language": "python", | |
|
750 | "metadata": {}, | |
|
751 | "outputs": [] | |
|
752 | }, | |
|
753 | { | |
|
754 | "cell_type": "heading", | |
|
755 | "level": 2, | |
|
756 | "metadata": { | |
|
757 | "slideshow": { | |
|
758 | "slide_type": "slide" | |
|
759 | } | |
|
760 | }, | |
|
761 | "source": [ | |
|
762 | 637 | "Visibility" |
|
763 | 638 | ] |
|
764 | 639 | }, |
|
765 | 640 | { |
|
766 | 641 | "cell_type": "markdown", |
|
767 | 642 | "metadata": {}, |
|
768 | 643 | "source": [ |
|
769 | 644 | "Sometimes it is necessary to **hide or show widgets** in place, **without having to re-display** the widget.\n", |
|
770 | 645 | "The `visibility` property of widgets can be used to hide or show **widgets that have already been displayed** (as seen below)." |
|
771 | 646 | ] |
|
772 | 647 | }, |
|
773 | 648 | { |
|
774 | 649 | "cell_type": "code", |
|
775 | 650 | "collapsed": false, |
|
776 | 651 | "input": [ |
|
777 | 652 | "string = widgets.Latex(value=\"Hello World!\")\n", |
|
778 | 653 | "display(string) " |
|
779 | 654 | ], |
|
780 | 655 | "language": "python", |
|
781 | 656 | "metadata": {}, |
|
782 | 657 | "outputs": [] |
|
783 | 658 | }, |
|
784 | 659 | { |
|
785 | 660 | "cell_type": "code", |
|
786 | 661 | "collapsed": false, |
|
787 | 662 | "input": [ |
|
788 | 663 | "string.visible=False" |
|
789 | 664 | ], |
|
790 | 665 | "language": "python", |
|
791 | 666 | "metadata": {}, |
|
792 | 667 | "outputs": [] |
|
793 | 668 | }, |
|
794 | 669 | { |
|
795 | 670 | "cell_type": "code", |
|
796 | 671 | "collapsed": false, |
|
797 | 672 | "input": [ |
|
798 | 673 | "string.visible=True" |
|
799 | 674 | ], |
|
800 | 675 | "language": "python", |
|
801 | 676 | "metadata": {}, |
|
802 | 677 | "outputs": [] |
|
803 | 678 | }, |
|
804 | 679 | { |
|
805 | 680 | "cell_type": "heading", |
|
806 | 681 | "level": 3, |
|
807 | 682 | "metadata": { |
|
808 | 683 | "slideshow": { |
|
809 | 684 | "slide_type": "slide" |
|
810 | 685 | } |
|
811 | 686 | }, |
|
812 | 687 | "source": [ |
|
813 | 688 | "Another example" |
|
814 | 689 | ] |
|
815 | 690 | }, |
|
816 | 691 | { |
|
817 | 692 | "cell_type": "markdown", |
|
818 | 693 | "metadata": {}, |
|
819 | 694 | "source": [ |
|
820 | 695 | "In the example below, a form is rendered, which conditionally displays widgets depending on the state of other widgets. Try toggling the student checkbox." |
|
821 | 696 | ] |
|
822 | 697 | }, |
|
823 | 698 | { |
|
824 | 699 | "cell_type": "code", |
|
825 | 700 | "collapsed": false, |
|
826 | 701 | "input": [ |
|
827 | 702 | "form = widgets.VBox()\n", |
|
828 | 703 | "first = widgets.Text(description=\"First Name:\")\n", |
|
829 | 704 | "last = widgets.Text(description=\"Last Name:\")\n", |
|
830 | 705 | "\n", |
|
831 | 706 | "student = widgets.Checkbox(description=\"Student:\", value=False)\n", |
|
832 | 707 | "school_info = widgets.VBox(visible=False, children=[\n", |
|
833 | 708 | " widgets.Text(description=\"School:\"),\n", |
|
834 | 709 | " widgets.IntText(description=\"Grade:\", min=0, max=12)\n", |
|
835 | 710 | " ])\n", |
|
836 | 711 | "\n", |
|
837 | 712 | "pet = widgets.Text(description=\"Pet's Name:\")\n", |
|
838 | 713 | "form.children = [first, last, student, school_info, pet]\n", |
|
839 | 714 | "display(form)\n", |
|
840 | 715 | "\n", |
|
841 | 716 | "def on_student_toggle(name, value):\n", |
|
842 | 717 | " if value:\n", |
|
843 | 718 | " school_info.visible = True\n", |
|
844 | 719 | " else:\n", |
|
845 | 720 | " school_info.visible = False\n", |
|
846 | 721 | "student.on_trait_change(on_student_toggle, 'value')\n" |
|
847 | 722 | ], |
|
848 | 723 | "language": "python", |
|
849 | 724 | "metadata": {}, |
|
850 | 725 | "outputs": [] |
|
851 | 726 | }, |
|
852 | 727 | { |
|
853 | 728 | "cell_type": "markdown", |
|
854 | 729 | "metadata": {}, |
|
855 | 730 | "source": [ |
|
856 | 731 | "[Index](Index.ipynb) - [Back](Widget Events.ipynb) - [Next](Custom Widget - Hello World.ipynb)" |
|
857 | 732 | ] |
|
858 | 733 | } |
|
859 | 734 | ], |
|
860 | 735 | "metadata": {} |
|
861 | 736 | } |
|
862 | 737 | ] |
|
863 | 738 | } No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now