|
11 | 11 | "cell_type": "markdown",
|
12 | 12 | "metadata": {},
|
13 | 13 | "source": [
|
14 |
| - "The `interact` function (`IPython.html.widgets.interact`) automatically creates user interface (UI) controls for exploring code and data interactively. It is the easiest way to get started using IPython's widgets." |
| 14 | + "The `interact` function (`ipywidgets.interact`) automatically creates user interface (UI) controls for exploring code and data interactively. It is the easiest way to get started using IPython's widgets." |
15 | 15 | ]
|
16 | 16 | },
|
17 | 17 | {
|
18 | 18 | "cell_type": "code",
|
19 | 19 | "execution_count": null,
|
20 | 20 | "metadata": {
|
21 |
| - "collapsed": false |
| 21 | + "collapsed": false, |
| 22 | + "scrolled": true |
22 | 23 | },
|
23 | 24 | "outputs": [],
|
24 | 25 | "source": [
|
25 |
| - "from __future__ import print_function\n", |
26 |
| - "from IPython.html.widgets import interact, interactive, fixed\n", |
27 |
| - "from IPython.html import widgets" |
| 26 | + "from __future__ import print_function # for python 2\n", |
| 27 | + "from ipywidgets import interact, interactive, fixed\n", |
| 28 | + "import ipywidgets as widgets" |
28 | 29 | ]
|
29 | 30 | },
|
30 | 31 | {
|
|
62 | 63 | " print(x)"
|
63 | 64 | ]
|
64 | 65 | },
|
| 66 | + { |
| 67 | + "cell_type": "code", |
| 68 | + "execution_count": null, |
| 69 | + "metadata": { |
| 70 | + "collapsed": false |
| 71 | + }, |
| 72 | + "outputs": [], |
| 73 | + "source": [ |
| 74 | + "f(10)" |
| 75 | + ] |
| 76 | + }, |
65 | 77 | {
|
66 | 78 | "cell_type": "markdown",
|
67 | 79 | "metadata": {},
|
|
164 | 176 | " print(p, q)"
|
165 | 177 | ]
|
166 | 178 | },
|
| 179 | + { |
| 180 | + "cell_type": "code", |
| 181 | + "execution_count": null, |
| 182 | + "metadata": { |
| 183 | + "collapsed": false |
| 184 | + }, |
| 185 | + "outputs": [], |
| 186 | + "source": [ |
| 187 | + "h(5, 10)" |
| 188 | + ] |
| 189 | + }, |
167 | 190 | {
|
168 | 191 | "cell_type": "markdown",
|
169 | 192 | "metadata": {},
|
|
186 | 209 | "cell_type": "markdown",
|
187 | 210 | "metadata": {},
|
188 | 211 | "source": [
|
189 |
| - "Notice that a slider is only produced for `p` as the value of `q` is fixed." |
| 212 | + "Notice that a slider is only produced for `p` since the value of `q` is fixed." |
190 | 213 | ]
|
191 | 214 | },
|
192 | 215 | {
|
193 | 216 | "cell_type": "markdown",
|
194 | 217 | "metadata": {},
|
195 | 218 | "source": [
|
196 |
| - "## Widget abbreviations" |
| 219 | + "## Widget creation" |
197 | 220 | ]
|
198 | 221 | },
|
199 | 222 | {
|
200 | 223 | "cell_type": "markdown",
|
201 | 224 | "metadata": {},
|
202 | 225 | "source": [
|
203 |
| - "When you pass an integer valued keyword argument (`x=10`) to `interact`, it generates an integer valued slider control with a range of $[-10,+3\\times10]$. In this case `10` is an *abbreviation* for an actual slider widget:\n", |
| 226 | + "The `interact` function tries to guess which type of control you want based on the value you pass in to the `interact` function. When you pass an integer argument to `interact`, such as `interact(f, x=10)`, an integer-valued slider control is generated centered at the value:\n", |
204 | 227 | "\n",
|
205 | 228 | "```python\n",
|
206 |
| - "IntSliderWidget(min=-10,max=30,step=1,value=10)\n", |
| 229 | + "widgets.IntSlider(min=-10,max=30,step=1,value=10)\n", |
207 | 230 | "```\n",
|
208 | 231 | "\n",
|
209 |
| - "In fact, we can get the same result if we pass this `IntSliderWidget` as the keyword argument for `x`:" |
| 232 | + "We can also directly create the widget when calling `interact`, rather than having `interact` guess:" |
210 | 233 | ]
|
211 | 234 | },
|
212 | 235 | {
|
|
224 | 247 | "cell_type": "markdown",
|
225 | 248 | "metadata": {},
|
226 | 249 | "source": [
|
227 |
| - "This examples clarifies how `interact` proceses its keyword arguments:\n", |
| 250 | + "This examples clarifies how `interact` processes its keyword arguments:\n", |
228 | 251 | "\n",
|
229 |
| - "1. If the keyword argument is `Widget` instance with a `value` attribute, that widget is used. Any widget with a `value` attribute can be used, even custom ones.\n", |
230 |
| - "2. Otherwise, the value is treated as a *widget abbreviation* that is converted to a widget before it is used.\n", |
| 252 | + "1. If the keyword argument is a `Widget` instance with a `value` attribute, the widget is used. Any widget with a `value` attribute can be used, even custom ones.\n", |
| 253 | + "2. Otherwise, the value is used to guess a correct a widget, which is used.\n", |
231 | 254 | "\n",
|
232 |
| - "The following table gives an overview of different widget abbreviations:\n", |
| 255 | + "The following table gives an overview of `interact` translates arguments into widgets:\n", |
233 | 256 | "\n",
|
234 | 257 | "<table class=\"table table-condensed table-bordered\">\n",
|
235 | 258 | " <tr><td><strong>Keyword argument</strong></td><td><strong>Widget</strong></td></tr> \n",
|
236 | 259 | " <tr><td>`True` or `False`</td><td>Checkbox</td></tr> \n",
|
237 |
| - " <tr><td>`'Hi there'`</td><td>Textarea</td></tr>\n", |
238 |
| - " <tr><td>`value` or `(min,max)` or `(min,max,step)` if integers are passed</td><td>IntSlider</td></tr>\n", |
239 |
| - " <tr><td>`value` or `(min,max)` or `(min,max,step)` if floats are passed</td><td>FloatSlider</td></tr>\n", |
| 260 | + " <tr><td>string `'Hi there'`</td><td>Textarea</td></tr>\n", |
| 261 | + " <tr><td>numeric `value` or `(min,max)` or `(min,max,step)`</td><td>IntSlider or FloatSlider</td></tr>\n", |
240 | 262 | " <tr><td>`('orange','apple')` or `{'one':1,'two':2}`</td><td>Dropdown</td></tr>\n",
|
241 | 263 | "</table>"
|
242 | 264 | ]
|
|
247 | 269 | "source": [
|
248 | 270 | "You have seen how the checkbox and textarea widgets work above. Here, more details about the different abbreviations for sliders and dropdowns are given.\n",
|
249 | 271 | "\n",
|
250 |
| - "If a 2-tuple of integers is passed `(min,max)` a integer valued slider is produced with those minimum and maximum (inclusive) values. In this case, the default step size of `1` is used." |
| 272 | + "If a 2-tuple of integers is passed, `(min,max)`, a integer-valued slider is produced with those minimum and maximum (inclusive) values. In this case, the default step size of `1` is used." |
251 | 273 | ]
|
252 | 274 | },
|
253 | 275 | {
|
|
265 | 287 | "cell_type": "markdown",
|
266 | 288 | "metadata": {},
|
267 | 289 | "source": [
|
268 |
| - "If a 3-tuple of integers is passed `(min,max,step)` the step size can also be set." |
| 290 | + "If a 3-tuple of integers is passed `(min,max,step)`, the step size is set." |
269 | 291 | ]
|
270 | 292 | },
|
271 | 293 | {
|
|
283 | 305 | "cell_type": "markdown",
|
284 | 306 | "metadata": {},
|
285 | 307 | "source": [
|
286 |
| - "A float valued slider is produced if the elements of the tuples are floats. Here the minimum is `0.0`, the maximum is `10.0` and step size is `0.1` (the default)." |
| 308 | + "A float-valued slider is produced if the elements of the tuples are floats. Here the minimum is `0.0`, the maximum is `10.0` and step size is `0.1` (the default)." |
287 | 309 | ]
|
288 | 310 | },
|
289 | 311 | {
|
|
301 | 323 | "cell_type": "markdown",
|
302 | 324 | "metadata": {},
|
303 | 325 | "source": [
|
304 |
| - "The step size can be changed by passing a 3rd element in the tuple." |
| 326 | + "The step size can be changed by passing a 3-element tuple. The length of the slider may necessitate skipping some steps if there are two many to fit." |
305 | 327 | ]
|
306 | 328 | },
|
307 | 329 | {
|
|
319 | 341 | "cell_type": "markdown",
|
320 | 342 | "metadata": {},
|
321 | 343 | "source": [
|
322 |
| - "For both integer and float valued sliders, you can pick the initial value of the widget by passing a default keyword argument to the underlying Python function. Here we set the initial value of a float slider to `5.5`." |
| 344 | + "For both integer and float-valued sliders, the initial value is the default value of the function parameter, if it exists. Here we set the initial value of a float slider to `5.5`." |
323 | 345 | ]
|
324 | 346 | },
|
325 | 347 | {
|
|
339 | 361 | "cell_type": "markdown",
|
340 | 362 | "metadata": {},
|
341 | 363 | "source": [
|
342 |
| - "Dropdown menus can be produced by passing a tuple of strings. In this case, the strings are both used as the names in the dropdown menu UI and passed to the underlying Python function." |
| 364 | + "Dropdown menus can be produced by passing a tuple of strings. In this case, the strings are used both as the names in the dropdown menu UI and passed to the underlying Python function." |
343 | 365 | ]
|
344 | 366 | },
|
345 | 367 | {
|
|
368 | 390 | },
|
369 | 391 | "outputs": [],
|
370 | 392 | "source": [
|
371 |
| - "interact(f, x={'one': 10, 'two': 20});" |
| 393 | + "from collections import OrderedDict\n", |
| 394 | + "interact(f, x={'a': 10, 'b': 20});" |
372 | 395 | ]
|
373 | 396 | },
|
374 | 397 | {
|
375 | 398 | "cell_type": "markdown",
|
376 | 399 | "metadata": {},
|
377 | 400 | "source": [
|
378 |
| - "## Using function annotations with `interact`" |
| 401 | + "## `interactive`" |
379 | 402 | ]
|
380 | 403 | },
|
381 | 404 | {
|
382 | 405 | "cell_type": "markdown",
|
383 | 406 | "metadata": {},
|
384 | 407 | "source": [
|
385 |
| - "If you are using Python 3, you can also specify widget abbreviations using [function annotations](https://docs.python.org/3/tutorial/controlflow.html#function-annotations). This is a convenient approach allows the widget abbreviations to be defined with a function.\n", |
386 |
| - "\n", |
387 |
| - "Define a function with an checkbox widget abbreviation for the argument `x`." |
388 |
| - ] |
389 |
| - }, |
390 |
| - { |
391 |
| - "cell_type": "code", |
392 |
| - "execution_count": null, |
393 |
| - "metadata": { |
394 |
| - "collapsed": false |
395 |
| - }, |
396 |
| - "outputs": [], |
397 |
| - "source": [ |
398 |
| - "def f(x:True):\n", |
399 |
| - " print(x)" |
| 408 | + "In addition to `interact`, IPython provides another function, `interactive`, that is useful when you want to reuse the widgets that are produced or access the data that is bound to the UI controls." |
400 | 409 | ]
|
401 | 410 | },
|
402 | 411 | {
|
403 | 412 | "cell_type": "markdown",
|
404 | 413 | "metadata": {},
|
405 | 414 | "source": [
|
406 |
| - "Then, because the widget abbreviation has already been defined, you can call `interact` with a single argument." |
| 415 | + "Here is a function that returns the sum of its two arguments." |
407 | 416 | ]
|
408 | 417 | },
|
409 | 418 | {
|
|
414 | 423 | },
|
415 | 424 | "outputs": [],
|
416 | 425 | "source": [
|
417 |
| - "interact(f);" |
418 |
| - ] |
419 |
| - }, |
420 |
| - { |
421 |
| - "cell_type": "markdown", |
422 |
| - "metadata": {}, |
423 |
| - "source": [ |
424 |
| - "## `interactive`" |
425 |
| - ] |
426 |
| - }, |
427 |
| - { |
428 |
| - "cell_type": "markdown", |
429 |
| - "metadata": {}, |
430 |
| - "source": [ |
431 |
| - "In addition to `interact` IPython provides another function, `interactive`, that is useful when you want to reuse the widget that are produced or access the data that is bound to the UI controls." |
432 |
| - ] |
433 |
| - }, |
434 |
| - { |
435 |
| - "cell_type": "markdown", |
436 |
| - "metadata": {}, |
437 |
| - "source": [ |
438 |
| - "Here is a function that returns the sum of its two arguments." |
| 426 | + "def f(a, b):\n", |
| 427 | + " return a+b" |
439 | 428 | ]
|
440 | 429 | },
|
441 | 430 | {
|
|
446 | 435 | },
|
447 | 436 | "outputs": [],
|
448 | 437 | "source": [
|
449 |
| - "def f(a, b):\n", |
450 |
| - " return a+b" |
| 438 | + "f(1,2)" |
451 | 439 | ]
|
452 | 440 | },
|
453 | 441 | {
|
|
527 | 515 | "cell_type": "markdown",
|
528 | 516 | "metadata": {},
|
529 | 517 | "source": [
|
530 |
| - "At this point, the UI controls work just like they would if `interact` had been used. You can manipulate them interactively and the function will be called. However, the widget instance returned by `interactive` also give you access to the current keyword arguments and return value of the underlying Python function.\n", |
| 518 | + "At this point, the UI controls work just like they would if `interact` had been used. You can manipulate them interactively and the function will be called. However, the widget instance returned by `interactive` also gives you access to the current keyword arguments and return value of the underlying Python function.\n", |
531 | 519 | "\n",
|
532 | 520 | "Here are the current keyword arguments. If you rerun this cell after manipulating the sliders, the values will have changed."
|
533 | 521 | ]
|
|
560 | 548 | "source": [
|
561 | 549 | "w.result"
|
562 | 550 | ]
|
| 551 | + }, |
| 552 | + { |
| 553 | + "cell_type": "code", |
| 554 | + "execution_count": null, |
| 555 | + "metadata": { |
| 556 | + "collapsed": true |
| 557 | + }, |
| 558 | + "outputs": [], |
| 559 | + "source": [] |
563 | 560 | }
|
564 | 561 | ],
|
565 | 562 | "metadata": {
|
566 | 563 | "kernelspec": {
|
567 |
| - "display_name": "Python 3", |
| 564 | + "display_name": "Python 3 (conda:strata)", |
568 | 565 | "language": "python",
|
569 |
| - "name": "python3" |
| 566 | + "name": "strata" |
570 | 567 | },
|
571 | 568 | "language_info": {
|
572 | 569 | "codemirror_mode": {
|
|
578 | 575 | "name": "python",
|
579 | 576 | "nbconvert_exporter": "python",
|
580 | 577 | "pygments_lexer": "ipython3",
|
581 |
| - "version": "3.4.3" |
| 578 | + "version": "3.5.0" |
582 | 579 | }
|
583 | 580 | },
|
584 | 581 | "nbformat": 4,
|
|
0 commit comments