Show More
@@ -1,13 +1,7 b'' | |||||
1 | { |
|
1 | { | |
2 | "metadata": { |
|
2 | "metadata": { | |
3 | "cell_tags": [ |
|
|||
4 | [ |
|
|||
5 | "<None>", |
|
|||
6 | null |
|
|||
7 | ] |
|
|||
8 | ], |
|
|||
9 | "name": "", |
|
3 | "name": "", | |
10 | "signature": "sha256:5ac3a85c8bb2f9bb3cd63b524bbb626ab1531176b43a109d13f5d7794f805eee" |
|
4 | "signature": "sha256:d788f17db7e02e94777b4b8aa36e55b29fc1806059dbb8a1cc9de60cdbe58825" | |
11 | }, |
|
5 | }, | |
12 | "nbformat": 3, |
|
6 | "nbformat": 3, | |
13 | "nbformat_minor": 0, |
|
7 | "nbformat_minor": 0, | |
@@ -18,179 +12,119 b'' | |||||
18 | "cell_type": "markdown", |
|
12 | "cell_type": "markdown", | |
19 | "metadata": {}, |
|
13 | "metadata": {}, | |
20 | "source": [ |
|
14 | "source": [ | |
21 | "To use IPython widgets in the notebook, the widget namespace needs to be imported." |
|
15 | "# Simple widget introduction\n", | |
|
16 | "\n", | |||
|
17 | "## What are widgets?\n", | |||
|
18 | "<blockquote>Widgets are elements that exists in both the front-end and the back-end.</blockquote>\n", | |||
|
19 | "\n", | |||
|
20 | "You can use widgets to build interactive GUIs for your notebooks. \n", | |||
|
21 | "You can also use widgets to synchronize stateful and stateless information between Python and JavaScript.\n", | |||
|
22 | "\n", | |||
|
23 | "## Using widgets \n", | |||
|
24 | "To use the widget framework, you need to import the widgets from `IPython.html.widgets`." | |||
22 | ] |
|
25 | ] | |
23 | }, |
|
26 | }, | |
24 | { |
|
27 | { | |
25 | "cell_type": "code", |
|
28 | "cell_type": "code", | |
26 | "collapsed": false, |
|
29 | "collapsed": false, | |
27 | "input": [ |
|
30 | "input": [ | |
28 |
"from IPython.html |
|
31 | "from IPython.html.widgets import *" | |
29 | "from IPython.display import display # Used to display widgets in the notebook" |
|
|||
30 | ], |
|
32 | ], | |
31 | "language": "python", |
|
33 | "language": "python", | |
32 | "metadata": {}, |
|
34 | "metadata": {}, | |
33 | "outputs": [], |
|
35 | "outputs": [], | |
34 |
"prompt_number": |
|
36 | "prompt_number": 8 | |
35 | }, |
|
|||
36 | { |
|
|||
37 | "cell_type": "heading", |
|
|||
38 | "level": 1, |
|
|||
39 | "metadata": {}, |
|
|||
40 | "source": [ |
|
|||
41 | "Basic Widgets" |
|
|||
42 | ] |
|
|||
43 | }, |
|
37 | }, | |
44 | { |
|
38 | { | |
45 | "cell_type": "markdown", |
|
39 | "cell_type": "markdown", | |
46 | "metadata": {}, |
|
40 | "metadata": {}, | |
47 | "source": [ |
|
41 | "source": [ | |
48 | "IPython comes with basic widgets that represent common interactive controls. These widgets are\n", |
|
42 | "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." | |
49 | "\n", |
|
|||
50 | "- CheckboxWidget\n", |
|
|||
51 | "- ToggleButtonWidget\n", |
|
|||
52 | "- FloatSliderWidget\n", |
|
|||
53 | "- BoundedFloatTextWidget\n", |
|
|||
54 | "- FloatProgressWidget\n", |
|
|||
55 | "- FloatTextWidget\n", |
|
|||
56 | "- ImageWidget\n", |
|
|||
57 | "- IntSliderWidget\n", |
|
|||
58 | "- BoundedIntTextWidget\n", |
|
|||
59 | "- IntProgressWidget\n", |
|
|||
60 | "- IntTextWidget\n", |
|
|||
61 | "- ToggleButtonsWidget\n", |
|
|||
62 | "- RadioButtonsWidget\n", |
|
|||
63 | "- DropdownWidget\n", |
|
|||
64 | "- SelectWidget\n", |
|
|||
65 | "- HTMLWidget\n", |
|
|||
66 | "- LatexWidget\n", |
|
|||
67 | "- TextareaWidget\n", |
|
|||
68 | "- TextWidget\n", |
|
|||
69 | "- ButtonWidget\n", |
|
|||
70 | "\n", |
|
|||
71 | "A few special widgets are also included, that can be used to capture events and change how other widgets are displayed. These widgets are\n", |
|
|||
72 | "\n", |
|
|||
73 | "- ContainerWidget\n", |
|
|||
74 | "- PopupWidget\n", |
|
|||
75 | "- AccordionWidget\n", |
|
|||
76 | "- TabWidget\n", |
|
|||
77 | "\n", |
|
|||
78 | "To see the complete list of widgets, one can execute the following" |
|
|||
79 | ] |
|
43 | ] | |
80 | }, |
|
44 | }, | |
81 | { |
|
45 | { | |
82 | "cell_type": "code", |
|
46 | "cell_type": "code", | |
83 | "collapsed": false, |
|
47 | "collapsed": false, | |
84 | "input": [ |
|
48 | "input": [ | |
85 | "[widget for widget in dir(widgets) if widget.endswith('Widget')]" |
|
49 | "IntSliderWidget()" | |
86 | ], |
|
50 | ], | |
87 | "language": "python", |
|
51 | "language": "python", | |
88 | "metadata": {}, |
|
52 | "metadata": {}, | |
89 | "outputs": [ |
|
53 | "outputs": [], | |
90 | { |
|
54 | "prompt_number": 9 | |
91 | "metadata": {}, |
|
|||
92 | "output_type": "pyout", |
|
|||
93 | "prompt_number": 2, |
|
|||
94 | "text": [ |
|
|||
95 | "['AccordionWidget',\n", |
|
|||
96 | " 'BoundedFloatTextWidget',\n", |
|
|||
97 | " 'BoundedIntTextWidget',\n", |
|
|||
98 | " 'ButtonWidget',\n", |
|
|||
99 | " 'CheckboxWidget',\n", |
|
|||
100 | " 'ContainerWidget',\n", |
|
|||
101 | " 'DOMWidget',\n", |
|
|||
102 | " 'DropdownWidget',\n", |
|
|||
103 | " 'FloatProgressWidget',\n", |
|
|||
104 | " 'FloatSliderWidget',\n", |
|
|||
105 | " 'FloatTextWidget',\n", |
|
|||
106 | " 'HTMLWidget',\n", |
|
|||
107 | " 'ImageWidget',\n", |
|
|||
108 | " 'IntProgressWidget',\n", |
|
|||
109 | " 'IntSliderWidget',\n", |
|
|||
110 | " 'IntTextWidget',\n", |
|
|||
111 | " 'LatexWidget',\n", |
|
|||
112 | " 'PopupWidget',\n", |
|
|||
113 | " 'RadioButtonsWidget',\n", |
|
|||
114 | " 'SelectWidget',\n", |
|
|||
115 | " 'TabWidget',\n", |
|
|||
116 | " 'TextWidget',\n", |
|
|||
117 | " 'TextareaWidget',\n", |
|
|||
118 | " 'ToggleButtonWidget',\n", |
|
|||
119 | " 'ToggleButtonsWidget',\n", |
|
|||
120 | " 'Widget']" |
|
|||
121 | ] |
|
|||
122 | } |
|
|||
123 | ], |
|
|||
124 | "prompt_number": 2 |
|
|||
125 | }, |
|
55 | }, | |
126 | { |
|
56 | { | |
127 | "cell_type": "markdown", |
|
57 | "cell_type": "markdown", | |
128 | "metadata": {}, |
|
58 | "metadata": {}, | |
129 | "source": [ |
|
59 | "source": [ | |
130 | "The basic widgets all have sensible default values. Create a *FloatSliderWidget* without displaying it:" |
|
60 | "You can also explicitly display the widget using `display(...)`." | |
131 | ] |
|
61 | ] | |
132 | }, |
|
62 | }, | |
133 | { |
|
63 | { | |
134 | "cell_type": "code", |
|
64 | "cell_type": "code", | |
135 | "collapsed": false, |
|
65 | "collapsed": false, | |
136 | "input": [ |
|
66 | "input": [ | |
137 | "mywidget = widgets.FloatSliderWidget()" |
|
67 | "from IPython.display import display\n", | |
|
68 | "w = IntSliderWidget()\n", | |||
|
69 | "display(w)" | |||
138 | ], |
|
70 | ], | |
139 | "language": "python", |
|
71 | "language": "python", | |
140 | "metadata": {}, |
|
72 | "metadata": {}, | |
141 | "outputs": [], |
|
73 | "outputs": [], | |
142 |
"prompt_number": |
|
74 | "prompt_number": 10 | |
143 | }, |
|
75 | }, | |
144 | { |
|
76 | { | |
145 | "cell_type": "markdown", |
|
77 | "cell_type": "markdown", | |
146 | "metadata": {}, |
|
78 | "metadata": {}, | |
147 | "source": [ |
|
79 | "source": [ | |
148 | "Constructing a widget does not display it on the page. To display a widget, the widget must be passed to the IPython `display(object)` method or must be returned as the last item in the cell. `mywidget` is displayed by" |
|
80 | "If you display the same widget twice, the displayed instances in the front-end will remain in sync with each other." | |
149 | ] |
|
81 | ] | |
150 | }, |
|
82 | }, | |
151 | { |
|
83 | { | |
152 | "cell_type": "code", |
|
84 | "cell_type": "code", | |
153 | "collapsed": false, |
|
85 | "collapsed": false, | |
154 | "input": [ |
|
86 | "input": [ | |
155 |
"display( |
|
87 | "display(w)" | |
156 | ], |
|
88 | ], | |
157 | "language": "python", |
|
89 | "language": "python", | |
158 | "metadata": {}, |
|
90 | "metadata": {}, | |
159 | "outputs": [], |
|
91 | "outputs": [], | |
160 |
"prompt_number": |
|
92 | "prompt_number": 11 | |
161 | }, |
|
93 | }, | |
162 | { |
|
94 | { | |
163 | "cell_type": "markdown", |
|
95 | "cell_type": "markdown", | |
164 | "metadata": {}, |
|
96 | "metadata": {}, | |
165 | "source": [ |
|
97 | "source": [ | |
166 | "or" |
|
98 | "You can close a widget by calling its `close()` method." | |
167 | ] |
|
99 | ] | |
168 | }, |
|
100 | }, | |
169 | { |
|
101 | { | |
170 | "cell_type": "code", |
|
102 | "cell_type": "code", | |
171 | "collapsed": false, |
|
103 | "collapsed": false, | |
172 | "input": [ |
|
104 | "input": [ | |
173 |
" |
|
105 | "w.close()" | |
174 | ], |
|
106 | ], | |
175 | "language": "python", |
|
107 | "language": "python", | |
176 | "metadata": {}, |
|
108 | "metadata": {}, | |
177 | "outputs": [], |
|
109 | "outputs": [], | |
178 |
"prompt_number": |
|
110 | "prompt_number": 12 | |
179 | }, |
|
111 | }, | |
180 | { |
|
112 | { | |
181 | "cell_type": "markdown", |
|
113 | "cell_type": "markdown", | |
182 | "metadata": {}, |
|
114 | "metadata": {}, | |
183 | "source": [ |
|
115 | "source": [ | |
184 | "It's important to realize that widgets are not the same as output, even though they are displayed with `display`. Widgets are drawn in a special widget area. That area is marked with a close button which allows you to collapse the widgets. Widgets cannot be interleaved with output. Doing so would break the ability to make simple animations using `clear_output`.\n", |
|
116 | "## Why does displaying the same widget twice work?\n", | |
|
117 | "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.\n", | |||
185 | "\n", |
|
118 | "\n", | |
186 | "Widgets are manipulated via special instance attributes (traitlets). The names of these traitlets are listed in the widget's `keys` attribute (as seen below). A few of these attributes are common to most widgets. The basic attributes are `value`, `description`, `visible`, and `disabled`. `_css` and `_view_name` are private attributes that exist in all widgets and should not be modified." |
|
119 | "## Widget properties\n", | |
|
120 | "All of the IPython widgets share a similar naming scheme. To read the value of a widget, you can query its `value` property." | |||
187 | ] |
|
121 | ] | |
188 | }, |
|
122 | }, | |
189 | { |
|
123 | { | |
190 | "cell_type": "code", |
|
124 | "cell_type": "code", | |
191 | "collapsed": false, |
|
125 | "collapsed": false, | |
192 | "input": [ |
|
126 | "input": [ | |
193 | "mywidget.keys" |
|
127 | "w.value" | |
194 | ], |
|
128 | ], | |
195 | "language": "python", |
|
129 | "language": "python", | |
196 | "metadata": {}, |
|
130 | "metadata": {}, | |
@@ -198,53 +132,44 b'' | |||||
198 | { |
|
132 | { | |
199 | "metadata": {}, |
|
133 | "metadata": {}, | |
200 | "output_type": "pyout", |
|
134 | "output_type": "pyout", | |
201 |
"prompt_number": |
|
135 | "prompt_number": 31, | |
202 | "text": [ |
|
136 | "text": [ | |
203 | "['_view_name',\n", |
|
137 | "41" | |
204 | " 'orientation',\n", |
|
|||
205 | " 'min',\n", |
|
|||
206 | " 'max',\n", |
|
|||
207 | " '_css',\n", |
|
|||
208 | " 'value',\n", |
|
|||
209 | " 'disabled',\n", |
|
|||
210 | " 'visible',\n", |
|
|||
211 | " 'step',\n", |
|
|||
212 | " 'description']" |
|
|||
213 | ] |
|
138 | ] | |
214 | } |
|
139 | } | |
215 | ], |
|
140 | ], | |
216 |
"prompt_number": |
|
141 | "prompt_number": 31 | |
217 | }, |
|
142 | }, | |
218 | { |
|
143 | { | |
219 | "cell_type": "markdown", |
|
144 | "cell_type": "markdown", | |
220 | "metadata": {}, |
|
145 | "metadata": {}, | |
221 | "source": [ |
|
146 | "source": [ | |
222 | "Changing a widget's attribute will automatically update that widget everywhere it is displayed in the notebook. Here, the `value` attribute of `mywidget` is set. The slider shown above updates automatically with the new value. Syncing also works in the other direction - changing the value of the displayed widget will update the property's value." |
|
147 | "Similarly, to set a widget's value, you can set its `value` property." | |
223 | ] |
|
148 | ] | |
224 | }, |
|
149 | }, | |
225 | { |
|
150 | { | |
226 | "cell_type": "code", |
|
151 | "cell_type": "code", | |
227 | "collapsed": false, |
|
152 | "collapsed": false, | |
228 | "input": [ |
|
153 | "input": [ | |
229 |
" |
|
154 | "w.value = 100" | |
230 | ], |
|
155 | ], | |
231 | "language": "python", |
|
156 | "language": "python", | |
232 | "metadata": {}, |
|
157 | "metadata": {}, | |
233 | "outputs": [], |
|
158 | "outputs": [], | |
234 |
"prompt_number": |
|
159 | "prompt_number": 32 | |
235 | }, |
|
160 | }, | |
236 | { |
|
161 | { | |
237 | "cell_type": "markdown", |
|
162 | "cell_type": "markdown", | |
238 | "metadata": {}, |
|
163 | "metadata": {}, | |
239 | "source": [ |
|
164 | "source": [ | |
240 | "After changing the widget's value in the notebook by hand to 0.0 (sliding the bar to the far left)." |
|
165 | "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." | |
241 | ] |
|
166 | ] | |
242 | }, |
|
167 | }, | |
243 | { |
|
168 | { | |
244 | "cell_type": "code", |
|
169 | "cell_type": "code", | |
245 | "collapsed": false, |
|
170 | "collapsed": false, | |
246 | "input": [ |
|
171 | "input": [ | |
247 | "mywidget.value" |
|
172 | "w.keys" | |
248 | ], |
|
173 | ], | |
249 | "language": "python", |
|
174 | "language": "python", | |
250 | "metadata": {}, |
|
175 | "metadata": {}, | |
@@ -252,85 +177,110 b'' | |||||
252 | { |
|
177 | { | |
253 | "metadata": {}, |
|
178 | "metadata": {}, | |
254 | "output_type": "pyout", |
|
179 | "output_type": "pyout", | |
255 |
"prompt_number": |
|
180 | "prompt_number": 33, | |
256 | "text": [ |
|
181 | "text": [ | |
257 | "25.0" |
|
182 | "['_view_name',\n", | |
|
183 | " 'orientation',\n", | |||
|
184 | " 'msg_throttle',\n", | |||
|
185 | " 'min',\n", | |||
|
186 | " 'max',\n", | |||
|
187 | " '_css',\n", | |||
|
188 | " 'value',\n", | |||
|
189 | " 'readout',\n", | |||
|
190 | " 'disabled',\n", | |||
|
191 | " 'visible',\n", | |||
|
192 | " 'step',\n", | |||
|
193 | " 'description']" | |||
258 | ] |
|
194 | ] | |
259 | } |
|
195 | } | |
260 | ], |
|
196 | ], | |
261 |
"prompt_number": |
|
197 | "prompt_number": 33 | |
262 | }, |
|
198 | }, | |
263 | { |
|
199 | { | |
264 | "cell_type": "markdown", |
|
200 | "cell_type": "markdown", | |
265 | "metadata": {}, |
|
201 | "metadata": {}, | |
266 | "source": [ |
|
202 | "source": [ | |
267 | "Widget values can also be set with kwargs during the construction of the widget (as seen below)." |
|
203 | "### Tip: Shorthand for setting the initial values of widget properties\n", | |
|
204 | "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)." | |||
268 | ] |
|
205 | ] | |
269 | }, |
|
206 | }, | |
270 | { |
|
207 | { | |
271 | "cell_type": "code", |
|
208 | "cell_type": "code", | |
272 | "collapsed": false, |
|
209 | "collapsed": false, | |
273 | "input": [ |
|
210 | "input": [ | |
274 | "mysecondwidget = widgets.RadioButtonsWidget(values=[\"Item A\", \"Item B\", \"Item C\"], value=\"Item A\")\n", |
|
211 | "TextWidget(value='Hello World!', disabled=True)" | |
275 | "display(mysecondwidget)" |
|
|||
276 | ], |
|
212 | ], | |
277 | "language": "python", |
|
213 | "language": "python", | |
278 | "metadata": {}, |
|
214 | "metadata": {}, | |
279 | "outputs": [], |
|
215 | "outputs": [], | |
280 |
"prompt_number": |
|
216 | "prompt_number": 34 | |
|
217 | }, | |||
|
218 | { | |||
|
219 | "cell_type": "markdown", | |||
|
220 | "metadata": {}, | |||
|
221 | "source": [ | |||
|
222 | "## Excercise\n", | |||
|
223 | "Create and display a `TextWidget`. Change that widget's `value` and some of it's other properties. Discover the other properties by querying the `keys` property of the instance." | |||
|
224 | ] | |||
281 | }, |
|
225 | }, | |
282 | { |
|
226 | { | |
283 | "cell_type": "code", |
|
227 | "cell_type": "code", | |
284 | "collapsed": false, |
|
228 | "collapsed": false, | |
285 | "input": [ |
|
229 | "input": [], | |
286 | "mysecondwidget.value" |
|
|||
287 | ], |
|
|||
288 | "language": "python", |
|
230 | "language": "python", | |
289 | "metadata": {}, |
|
231 | "metadata": {}, | |
290 | "outputs": [ |
|
232 | "outputs": [] | |
291 | { |
|
|||
292 | "metadata": {}, |
|
|||
293 | "output_type": "pyout", |
|
|||
294 | "prompt_number": 10, |
|
|||
295 | "text": [ |
|
|||
296 | "'Item A'" |
|
|||
297 | ] |
|
|||
298 | } |
|
|||
299 | ], |
|
|||
300 | "prompt_number": 10 |
|
|||
301 | }, |
|
233 | }, | |
302 | { |
|
234 | { | |
303 | "cell_type": "markdown", |
|
235 | "cell_type": "markdown", | |
304 | "metadata": {}, |
|
236 | "metadata": {}, | |
305 | "source": [ |
|
237 | "source": [ | |
306 | "Some widgets have special attributes. For example, text boxes and text areas can specify the `placeholder` attribute, which will set \"placeholder\" text to be displayed before the user has typed anything:" |
|
238 | "## Linking two similar widgets\n", | |
|
239 | "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." | |||
307 | ] |
|
240 | ] | |
308 | }, |
|
241 | }, | |
309 | { |
|
242 | { | |
310 | "cell_type": "code", |
|
243 | "cell_type": "code", | |
311 | "collapsed": false, |
|
244 | "collapsed": false, | |
312 | "input": [ |
|
245 | "input": [ | |
313 | "mytextwidget = widgets.TextWidget()\n", |
|
246 | "from IPython.utils.traitlets import link\n", | |
314 | "mytextwidget.placeholder = \"type something here\"\n", |
|
247 | "a = FloatTextWidget()\n", | |
315 | "display(mytextwidget)" |
|
248 | "b = FloatSliderWidget()\n", | |
|
249 | "c = FloatProgressWidget()\n", | |||
|
250 | "display(a,b,c)\n", | |||
|
251 | "\n", | |||
|
252 | "\n", | |||
|
253 | "mylink = link((a, 'value'), (b, 'value'), (c, 'value'))" | |||
316 | ], |
|
254 | ], | |
317 | "language": "python", |
|
255 | "language": "python", | |
318 | "metadata": {}, |
|
256 | "metadata": {}, | |
319 | "outputs": [], |
|
257 | "outputs": [], | |
320 |
"prompt_number": |
|
258 | "prompt_number": 51 | |
|
259 | }, | |||
|
260 | { | |||
|
261 | "cell_type": "markdown", | |||
|
262 | "metadata": {}, | |||
|
263 | "source": [ | |||
|
264 | "Unlinking the widgets is simple. All you have to do is call `.unlink` on the link object." | |||
|
265 | ] | |||
321 | }, |
|
266 | }, | |
322 | { |
|
267 | { | |
323 | "cell_type": "code", |
|
268 | "cell_type": "code", | |
324 | "collapsed": false, |
|
269 | "collapsed": false, | |
325 | "input": [ |
|
270 | "input": [ | |
326 | "mytextareawidget = widgets.TextareaWidget()\n", |
|
271 | "mylink.unlink()" | |
327 | "mytextareawidget.placeholder = \"your text here\"\n", |
|
|||
328 | "display(mytextareawidget)" |
|
|||
329 | ], |
|
272 | ], | |
330 | "language": "python", |
|
273 | "language": "python", | |
331 | "metadata": {}, |
|
274 | "metadata": {}, | |
332 | "outputs": [], |
|
275 | "outputs": [], | |
333 | "prompt_number": 5 |
|
276 | "prompt_number": 52 | |
|
277 | }, | |||
|
278 | { | |||
|
279 | "cell_type": "markdown", | |||
|
280 | "metadata": {}, | |||
|
281 | "source": [ | |||
|
282 | "[Next](Widget List.ipynb)" | |||
|
283 | ] | |||
334 | } |
|
284 | } | |
335 | ], |
|
285 | ], | |
336 | "metadata": {} |
|
286 | "metadata": {} |
General Comments 0
You need to be logged in to leave comments.
Login now