Skip to content

Commit 901f89d

Browse files
committed
Merge pull request #34 from jasongrout/update-jupyter
Update Interactive Widgets to Jupyter, revise text
2 parents 57e8205 + baffdea commit 901f89d

File tree

1 file changed

+66
-69
lines changed

1 file changed

+66
-69
lines changed

examples/Interactive Widgets/Using Interact.ipynb

Lines changed: 66 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,21 @@
1111
"cell_type": "markdown",
1212
"metadata": {},
1313
"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."
1515
]
1616
},
1717
{
1818
"cell_type": "code",
1919
"execution_count": null,
2020
"metadata": {
21-
"collapsed": false
21+
"collapsed": false,
22+
"scrolled": true
2223
},
2324
"outputs": [],
2425
"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"
2829
]
2930
},
3031
{
@@ -62,6 +63,17 @@
6263
" print(x)"
6364
]
6465
},
66+
{
67+
"cell_type": "code",
68+
"execution_count": null,
69+
"metadata": {
70+
"collapsed": false
71+
},
72+
"outputs": [],
73+
"source": [
74+
"f(10)"
75+
]
76+
},
6577
{
6678
"cell_type": "markdown",
6779
"metadata": {},
@@ -164,6 +176,17 @@
164176
" print(p, q)"
165177
]
166178
},
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+
},
167190
{
168191
"cell_type": "markdown",
169192
"metadata": {},
@@ -186,27 +209,27 @@
186209
"cell_type": "markdown",
187210
"metadata": {},
188211
"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."
190213
]
191214
},
192215
{
193216
"cell_type": "markdown",
194217
"metadata": {},
195218
"source": [
196-
"## Widget abbreviations"
219+
"## Widget creation"
197220
]
198221
},
199222
{
200223
"cell_type": "markdown",
201224
"metadata": {},
202225
"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",
204227
"\n",
205228
"```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",
207230
"```\n",
208231
"\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:"
210233
]
211234
},
212235
{
@@ -224,19 +247,18 @@
224247
"cell_type": "markdown",
225248
"metadata": {},
226249
"source": [
227-
"This examples clarifies how `interact` proceses its keyword arguments:\n",
250+
"This examples clarifies how `interact` processes its keyword arguments:\n",
228251
"\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",
231254
"\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",
233256
"\n",
234257
"<table class=\"table table-condensed table-bordered\">\n",
235258
" <tr><td><strong>Keyword argument</strong></td><td><strong>Widget</strong></td></tr> \n",
236259
" <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",
240262
" <tr><td>`('orange','apple')` or `{'one':1,'two':2}`</td><td>Dropdown</td></tr>\n",
241263
"</table>"
242264
]
@@ -247,7 +269,7 @@
247269
"source": [
248270
"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",
249271
"\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."
251273
]
252274
},
253275
{
@@ -265,7 +287,7 @@
265287
"cell_type": "markdown",
266288
"metadata": {},
267289
"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."
269291
]
270292
},
271293
{
@@ -283,7 +305,7 @@
283305
"cell_type": "markdown",
284306
"metadata": {},
285307
"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)."
287309
]
288310
},
289311
{
@@ -301,7 +323,7 @@
301323
"cell_type": "markdown",
302324
"metadata": {},
303325
"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."
305327
]
306328
},
307329
{
@@ -319,7 +341,7 @@
319341
"cell_type": "markdown",
320342
"metadata": {},
321343
"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`."
323345
]
324346
},
325347
{
@@ -339,7 +361,7 @@
339361
"cell_type": "markdown",
340362
"metadata": {},
341363
"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."
343365
]
344366
},
345367
{
@@ -368,42 +390,29 @@
368390
},
369391
"outputs": [],
370392
"source": [
371-
"interact(f, x={'one': 10, 'two': 20});"
393+
"from collections import OrderedDict\n",
394+
"interact(f, x={'a': 10, 'b': 20});"
372395
]
373396
},
374397
{
375398
"cell_type": "markdown",
376399
"metadata": {},
377400
"source": [
378-
"## Using function annotations with `interact`"
401+
"## `interactive`"
379402
]
380403
},
381404
{
382405
"cell_type": "markdown",
383406
"metadata": {},
384407
"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."
400409
]
401410
},
402411
{
403412
"cell_type": "markdown",
404413
"metadata": {},
405414
"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."
407416
]
408417
},
409418
{
@@ -414,28 +423,8 @@
414423
},
415424
"outputs": [],
416425
"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"
439428
]
440429
},
441430
{
@@ -446,8 +435,7 @@
446435
},
447436
"outputs": [],
448437
"source": [
449-
"def f(a, b):\n",
450-
" return a+b"
438+
"f(1,2)"
451439
]
452440
},
453441
{
@@ -527,7 +515,7 @@
527515
"cell_type": "markdown",
528516
"metadata": {},
529517
"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",
531519
"\n",
532520
"Here are the current keyword arguments. If you rerun this cell after manipulating the sliders, the values will have changed."
533521
]
@@ -560,13 +548,22 @@
560548
"source": [
561549
"w.result"
562550
]
551+
},
552+
{
553+
"cell_type": "code",
554+
"execution_count": null,
555+
"metadata": {
556+
"collapsed": true
557+
},
558+
"outputs": [],
559+
"source": []
563560
}
564561
],
565562
"metadata": {
566563
"kernelspec": {
567-
"display_name": "Python 3",
564+
"display_name": "Python 3 (conda:strata)",
568565
"language": "python",
569-
"name": "python3"
566+
"name": "strata"
570567
},
571568
"language_info": {
572569
"codemirror_mode": {
@@ -578,7 +575,7 @@
578575
"name": "python",
579576
"nbconvert_exporter": "python",
580577
"pygments_lexer": "ipython3",
581-
"version": "3.4.3"
578+
"version": "3.5.0"
582579
}
583580
},
584581
"nbformat": 4,

0 commit comments

Comments
 (0)