##// END OF EJS Templates
Updated basics notebook for pydata2014
Jonathan Frederic -
Show More
@@ -1,13 +1,7 b''
1 1 {
2 2 "metadata": {
3 "cell_tags": [
4 [
5 "<None>",
6 null
7 ]
8 ],
9 3 "name": "",
10 "signature": "sha256:5ac3a85c8bb2f9bb3cd63b524bbb626ab1531176b43a109d13f5d7794f805eee"
4 "signature": "sha256:d788f17db7e02e94777b4b8aa36e55b29fc1806059dbb8a1cc9de60cdbe58825"
11 5 },
12 6 "nbformat": 3,
13 7 "nbformat_minor": 0,
@@ -18,179 +12,119 b''
18 12 "cell_type": "markdown",
19 13 "metadata": {},
20 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 28 "cell_type": "code",
26 29 "collapsed": false,
27 30 "input": [
28 "from IPython.html import widgets # Widget definitions\n",
29 "from IPython.display import display # Used to display widgets in the notebook"
31 "from IPython.html.widgets import *"
30 32 ],
31 33 "language": "python",
32 34 "metadata": {},
33 35 "outputs": [],
34 "prompt_number": 2
35 },
36 {
37 "cell_type": "heading",
38 "level": 1,
39 "metadata": {},
40 "source": [
41 "Basic Widgets"
42 ]
36 "prompt_number": 8
43 37 },
44 38 {
45 39 "cell_type": "markdown",
46 40 "metadata": {},
47 41 "source": [
48 "IPython comes with basic widgets that represent common interactive controls. These widgets are\n",
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"
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."
79 43 ]
80 44 },
81 45 {
82 46 "cell_type": "code",
83 47 "collapsed": false,
84 48 "input": [
85 "[widget for widget in dir(widgets) if widget.endswith('Widget')]"
49 "IntSliderWidget()"
86 50 ],
87 51 "language": "python",
88 52 "metadata": {},
89 "outputs": [
90 {
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
53 "outputs": [],
54 "prompt_number": 9
125 55 },
126 56 {
127 57 "cell_type": "markdown",
128 58 "metadata": {},
129 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 64 "cell_type": "code",
135 65 "collapsed": false,
136 66 "input": [
137 "mywidget = widgets.FloatSliderWidget()"
67 "from IPython.display import display\n",
68 "w = IntSliderWidget()\n",
69 "display(w)"
138 70 ],
139 71 "language": "python",
140 72 "metadata": {},
141 73 "outputs": [],
142 "prompt_number": 3
74 "prompt_number": 10
143 75 },
144 76 {
145 77 "cell_type": "markdown",
146 78 "metadata": {},
147 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 84 "cell_type": "code",
153 85 "collapsed": false,
154 86 "input": [
155 "display(mywidget)"
87 "display(w)"
156 88 ],
157 89 "language": "python",
158 90 "metadata": {},
159 91 "outputs": [],
160 "prompt_number": 4
92 "prompt_number": 11
161 93 },
162 94 {
163 95 "cell_type": "markdown",
164 96 "metadata": {},
165 97 "source": [
166 "or"
98 "You can close a widget by calling its `close()` method."
167 99 ]
168 100 },
169 101 {
170 102 "cell_type": "code",
171 103 "collapsed": false,
172 104 "input": [
173 "mywidget"
105 "w.close()"
174 106 ],
175 107 "language": "python",
176 108 "metadata": {},
177 109 "outputs": [],
178 "prompt_number": 5
110 "prompt_number": 12
179 111 },
180 112 {
181 113 "cell_type": "markdown",
182 114 "metadata": {},
183 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 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 124 "cell_type": "code",
191 125 "collapsed": false,
192 126 "input": [
193 "mywidget.keys"
127 "w.value"
194 128 ],
195 129 "language": "python",
196 130 "metadata": {},
@@ -198,53 +132,44 b''
198 132 {
199 133 "metadata": {},
200 134 "output_type": "pyout",
201 "prompt_number": 6,
135 "prompt_number": 31,
202 136 "text": [
203 "['_view_name',\n",
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']"
137 "41"
213 138 ]
214 139 }
215 140 ],
216 "prompt_number": 6
141 "prompt_number": 31
217 142 },
218 143 {
219 144 "cell_type": "markdown",
220 145 "metadata": {},
221 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 151 "cell_type": "code",
227 152 "collapsed": false,
228 153 "input": [
229 "mywidget.value = 25.0"
154 "w.value = 100"
230 155 ],
231 156 "language": "python",
232 157 "metadata": {},
233 158 "outputs": [],
234 "prompt_number": 7
159 "prompt_number": 32
235 160 },
236 161 {
237 162 "cell_type": "markdown",
238 163 "metadata": {},
239 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 169 "cell_type": "code",
245 170 "collapsed": false,
246 171 "input": [
247 "mywidget.value"
172 "w.keys"
248 173 ],
249 174 "language": "python",
250 175 "metadata": {},
@@ -252,85 +177,110 b''
252 177 {
253 178 "metadata": {},
254 179 "output_type": "pyout",
255 "prompt_number": 8,
180 "prompt_number": 33,
256 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": 8
197 "prompt_number": 33
262 198 },
263 199 {
264 200 "cell_type": "markdown",
265 201 "metadata": {},
266 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 208 "cell_type": "code",
272 209 "collapsed": false,
273 210 "input": [
274 "mysecondwidget = widgets.RadioButtonsWidget(values=[\"Item A\", \"Item B\", \"Item C\"], value=\"Item A\")\n",
275 "display(mysecondwidget)"
211 "TextWidget(value='Hello World!', disabled=True)"
276 212 ],
277 213 "language": "python",
278 214 "metadata": {},
279 215 "outputs": [],
280 "prompt_number": 9
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 227 "cell_type": "code",
284 228 "collapsed": false,
285 "input": [
286 "mysecondwidget.value"
287 ],
229 "input": [],
288 230 "language": "python",
289 231 "metadata": {},
290 "outputs": [
291 {
292 "metadata": {},
293 "output_type": "pyout",
294 "prompt_number": 10,
295 "text": [
296 "'Item A'"
297 ]
298 }
299 ],
300 "prompt_number": 10
232 "outputs": []
301 233 },
302 234 {
303 235 "cell_type": "markdown",
304 236 "metadata": {},
305 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 243 "cell_type": "code",
311 244 "collapsed": false,
312 245 "input": [
313 "mytextwidget = widgets.TextWidget()\n",
314 "mytextwidget.placeholder = \"type something here\"\n",
315 "display(mytextwidget)"
246 "from IPython.utils.traitlets import link\n",
247 "a = FloatTextWidget()\n",
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 255 "language": "python",
318 256 "metadata": {},
319 257 "outputs": [],
320 "prompt_number": 4
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 268 "cell_type": "code",
324 269 "collapsed": false,
325 270 "input": [
326 "mytextareawidget = widgets.TextareaWidget()\n",
327 "mytextareawidget.placeholder = \"your text here\"\n",
328 "display(mytextareawidget)"
271 "mylink.unlink()"
329 272 ],
330 273 "language": "python",
331 274 "metadata": {},
332 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 286 "metadata": {}
General Comments 0
You need to be logged in to leave comments. Login now