Show More
@@ -1,242 +1,269 b'' | |||||
1 | { |
|
1 | { | |
2 | "metadata": { |
|
2 | "metadata": { | |
3 | "cell_tags": [ |
|
3 | "cell_tags": [ | |
4 | [ |
|
4 | [ | |
5 | "<None>", |
|
5 | "<None>", | |
6 | null |
|
6 | null | |
7 | ] |
|
7 | ] | |
8 | ], |
|
8 | ], | |
9 | "name": "" |
|
9 | "name": "" | |
10 | }, |
|
10 | }, | |
11 | "nbformat": 3, |
|
11 | "nbformat": 3, | |
12 | "nbformat_minor": 0, |
|
12 | "nbformat_minor": 0, | |
13 | "worksheets": [ |
|
13 | "worksheets": [ | |
14 | { |
|
14 | { | |
15 | "cells": [ |
|
15 | "cells": [ | |
16 | { |
|
16 | { | |
17 | "cell_type": "code", |
|
17 | "cell_type": "code", | |
18 | "collapsed": false, |
|
18 | "collapsed": false, | |
19 | "input": [ |
|
19 | "input": [ | |
20 | "from IPython.html import widgets # Widget definitions\n", |
|
20 | "from IPython.html import widgets # Widget definitions\n", | |
21 | "from IPython.display import display # Used to display widgets in the notebook" |
|
21 | "from IPython.display import display # Used to display widgets in the notebook" | |
22 | ], |
|
22 | ], | |
23 | "language": "python", |
|
23 | "language": "python", | |
24 | "metadata": {}, |
|
24 | "metadata": {}, | |
25 | "outputs": [], |
|
25 | "outputs": [], | |
26 | "prompt_number": 1 |
|
26 | "prompt_number": 1 | |
27 | }, |
|
27 | }, | |
28 | { |
|
28 | { | |
29 | "cell_type": "heading", |
|
29 | "cell_type": "heading", | |
30 | "level": 1, |
|
30 | "level": 1, | |
31 | "metadata": {}, |
|
31 | "metadata": {}, | |
32 | "source": [ |
|
32 | "source": [ | |
33 | "Traitlet Events" |
|
33 | "Traitlet Events" | |
34 | ] |
|
34 | ] | |
35 | }, |
|
35 | }, | |
36 | { |
|
36 | { | |
37 | "cell_type": "markdown", |
|
37 | "cell_type": "markdown", | |
38 | "metadata": {}, |
|
38 | "metadata": {}, | |
39 | "source": [ |
|
39 | "source": [ | |
40 | "The widget properties are IPython traitlets. Traitlets are eventful. To handle property value changes, the `on_trait_change` method of the widget can be used to register an event handling callback. The doc string for `on_trait_change` can be seen below. Both the `name` and `remove` properties are optional." |
|
40 | "The widget properties are IPython traitlets. Traitlets are eventful. To handle property value changes, the `on_trait_change` method of the widget can be used to register an event handling callback. The doc string for `on_trait_change` can be seen below. Both the `name` and `remove` properties are optional." | |
41 | ] |
|
41 | ] | |
42 | }, |
|
42 | }, | |
43 | { |
|
43 | { | |
44 | "cell_type": "code", |
|
44 | "cell_type": "code", | |
45 | "collapsed": false, |
|
45 | "collapsed": false, | |
46 | "input": [ |
|
46 | "input": [ | |
47 | "print(widgets.Widget.on_trait_change.__doc__)" |
|
47 | "print(widgets.Widget.on_trait_change.__doc__)" | |
48 | ], |
|
48 | ], | |
49 | "language": "python", |
|
49 | "language": "python", | |
50 | "metadata": {}, |
|
50 | "metadata": {}, | |
51 | "outputs": [ |
|
51 | "outputs": [ | |
52 | { |
|
52 | { | |
53 | "output_type": "stream", |
|
53 | "output_type": "stream", | |
54 | "stream": "stdout", |
|
54 | "stream": "stdout", | |
55 | "text": [ |
|
55 | "text": [ | |
56 | "Setup a handler to be called when a trait changes.\n", |
|
56 | "Setup a handler to be called when a trait changes.\n", | |
57 | "\n", |
|
57 | "\n", | |
58 | " This is used to setup dynamic notifications of trait changes.\n", |
|
58 | " This is used to setup dynamic notifications of trait changes.\n", | |
59 | "\n", |
|
59 | "\n", | |
60 | " Static handlers can be created by creating methods on a HasTraits\n", |
|
60 | " Static handlers can be created by creating methods on a HasTraits\n", | |
61 | " subclass with the naming convention '_[traitname]_changed'. Thus,\n", |
|
61 | " subclass with the naming convention '_[traitname]_changed'. Thus,\n", | |
62 | " to create static handler for the trait 'a', create the method\n", |
|
62 | " to create static handler for the trait 'a', create the method\n", | |
63 | " _a_changed(self, name, old, new) (fewer arguments can be used, see\n", |
|
63 | " _a_changed(self, name, old, new) (fewer arguments can be used, see\n", | |
64 | " below).\n", |
|
64 | " below).\n", | |
65 | "\n", |
|
65 | "\n", | |
66 | " Parameters\n", |
|
66 | " Parameters\n", | |
67 | " ----------\n", |
|
67 | " ----------\n", | |
68 | " handler : callable\n", |
|
68 | " handler : callable\n", | |
69 | " A callable that is called when a trait changes. Its\n", |
|
69 | " A callable that is called when a trait changes. Its\n", | |
70 | " signature can be handler(), handler(name), handler(name, new)\n", |
|
70 | " signature can be handler(), handler(name), handler(name, new)\n", | |
71 | " or handler(name, old, new).\n", |
|
71 | " or handler(name, old, new).\n", | |
72 | " name : list, str, None\n", |
|
72 | " name : list, str, None\n", | |
73 | " If None, the handler will apply to all traits. If a list\n", |
|
73 | " If None, the handler will apply to all traits. If a list\n", | |
74 | " of str, handler will apply to all names in the list. If a\n", |
|
74 | " of str, handler will apply to all names in the list. If a\n", | |
75 | " str, the handler will apply just to that name.\n", |
|
75 | " str, the handler will apply just to that name.\n", | |
76 | " remove : bool\n", |
|
76 | " remove : bool\n", | |
77 | " If False (the default), then install the handler. If True\n", |
|
77 | " If False (the default), then install the handler. If True\n", | |
78 | " then unintall it.\n", |
|
78 | " then unintall it.\n", | |
79 | " \n" |
|
79 | " \n" | |
80 | ] |
|
80 | ] | |
81 | } |
|
81 | } | |
82 | ], |
|
82 | ], | |
83 | "prompt_number": 2 |
|
83 | "prompt_number": 2 | |
84 | }, |
|
84 | }, | |
85 | { |
|
85 | { | |
86 | "cell_type": "markdown", |
|
86 | "cell_type": "markdown", | |
87 | "metadata": {}, |
|
87 | "metadata": {}, | |
88 | "source": [ |
|
88 | "source": [ | |
89 | "Mentioned in the doc string, the callback registered can have 4 possible signatures:\n", |
|
89 | "Mentioned in the doc string, the callback registered can have 4 possible signatures:\n", | |
90 | "\n", |
|
90 | "\n", | |
91 | "- callback()\n", |
|
91 | "- callback()\n", | |
92 | "- callback(trait_name)\n", |
|
92 | "- callback(trait_name)\n", | |
93 | "- callback(trait_name, new_value)\n", |
|
93 | "- callback(trait_name, new_value)\n", | |
94 | "- callback(trait_name, old_value, new_value)\n", |
|
94 | "- callback(trait_name, old_value, new_value)\n", | |
95 | "\n", |
|
95 | "\n", | |
96 | "An example of how to output an IntRangeWiget's value as it is changed can be seen below." |
|
96 | "An example of how to output an IntRangeWiget's value as it is changed can be seen below." | |
97 | ] |
|
97 | ] | |
98 | }, |
|
98 | }, | |
99 | { |
|
99 | { | |
100 | "cell_type": "code", |
|
100 | "cell_type": "code", | |
101 | "collapsed": false, |
|
101 | "collapsed": false, | |
102 | "input": [ |
|
102 | "input": [ | |
103 | "intrange = widgets.IntRangeWidget()\n", |
|
103 | "intrange = widgets.IntRangeWidget()\n", | |
104 | "display(intrange)\n", |
|
104 | "display(intrange)\n", | |
105 | "\n", |
|
105 | "\n", | |
106 | "def on_value_change(name, value):\n", |
|
106 | "def on_value_change(name, value):\n", | |
107 | " print value\n", |
|
107 | " print value\n", | |
108 | "\n", |
|
108 | "\n", | |
109 | "intrange.on_trait_change(on_value_change, 'value')" |
|
109 | "intrange.on_trait_change(on_value_change, 'value')" | |
110 | ], |
|
110 | ], | |
111 | "language": "python", |
|
111 | "language": "python", | |
112 | "metadata": {}, |
|
112 | "metadata": {}, | |
113 |
"outputs": [ |
|
113 | "outputs": [ | |
|
114 | { | |||
|
115 | "output_type": "stream", | |||
|
116 | "stream": "stdout", | |||
|
117 | "text": [ | |||
|
118 | "25\n" | |||
|
119 | ] | |||
|
120 | }, | |||
|
121 | { | |||
|
122 | "output_type": "stream", | |||
|
123 | "stream": "stdout", | |||
|
124 | "text": [ | |||
|
125 | "73\n" | |||
|
126 | ] | |||
|
127 | }, | |||
|
128 | { | |||
|
129 | "output_type": "stream", | |||
|
130 | "stream": "stdout", | |||
|
131 | "text": [ | |||
|
132 | "99\n" | |||
|
133 | ] | |||
|
134 | } | |||
|
135 | ], | |||
114 | "prompt_number": 3 |
|
136 | "prompt_number": 3 | |
115 | }, |
|
137 | }, | |
116 | { |
|
138 | { | |
117 | "cell_type": "heading", |
|
139 | "cell_type": "heading", | |
118 | "level": 1, |
|
140 | "level": 1, | |
119 | "metadata": {}, |
|
141 | "metadata": {}, | |
120 | "source": [ |
|
142 | "source": [ | |
121 | "Specialized Events" |
|
143 | "Specialized Events" | |
122 | ] |
|
144 | ] | |
123 | }, |
|
145 | }, | |
124 | { |
|
146 | { | |
125 | "cell_type": "heading", |
|
147 | "cell_type": "heading", | |
126 | "level": 2, |
|
148 | "level": 2, | |
127 | "metadata": {}, |
|
149 | "metadata": {}, | |
128 | "source": [ |
|
150 | "source": [ | |
129 | "Button On Click Event" |
|
151 | "Button On Click Event" | |
130 | ] |
|
152 | ] | |
131 | }, |
|
153 | }, | |
132 | { |
|
154 | { | |
133 | "cell_type": "markdown", |
|
155 | "cell_type": "markdown", | |
134 | "metadata": {}, |
|
156 | "metadata": {}, | |
135 | "source": [ |
|
157 | "source": [ | |
136 | "The `ButtonWidget` is a special widget, like the `ContainerWidget` and `MulticontainerWidget`, that isn't used to represent a data type. Instead the button widget is used to handle mouse clicks. The `on_click` method of the `ButtonWidget` can be used to register a click even handler. The doc string of the `on_click` can be seen below." |
|
158 | "The `ButtonWidget` is a special widget, like the `ContainerWidget` and `MulticontainerWidget`, that isn't used to represent a data type. Instead the button widget is used to handle mouse clicks. The `on_click` method of the `ButtonWidget` can be used to register a click even handler. The doc string of the `on_click` can be seen below." | |
137 | ] |
|
159 | ] | |
138 | }, |
|
160 | }, | |
139 | { |
|
161 | { | |
140 | "cell_type": "code", |
|
162 | "cell_type": "code", | |
141 | "collapsed": false, |
|
163 | "collapsed": false, | |
142 | "input": [ |
|
164 | "input": [ | |
143 | "print(widgets.ButtonWidget.on_click.__doc__)" |
|
165 | "print(widgets.ButtonWidget.on_click.__doc__)" | |
144 | ], |
|
166 | ], | |
145 | "language": "python", |
|
167 | "language": "python", | |
146 | "metadata": {}, |
|
168 | "metadata": {}, | |
147 | "outputs": [ |
|
169 | "outputs": [ | |
148 | { |
|
170 | { | |
149 | "output_type": "stream", |
|
171 | "output_type": "stream", | |
150 | "stream": "stdout", |
|
172 | "stream": "stdout", | |
151 | "text": [ |
|
173 | "text": [ | |
152 | "Register a callback to execute when the button is clicked. The\n", |
|
174 | "Register a callback to execute when the button is clicked. The\n", | |
153 | " callback can either accept no parameters or one sender parameter:\n", |
|
175 | " callback can either accept no parameters or one sender parameter:\n", | |
154 | " - callback()\n", |
|
176 | " - callback()\n", | |
155 | " - callback(sender)\n", |
|
177 | " - callback(sender)\n", | |
156 | " If the callback has a sender parameter, the ButtonWidget instance that\n", |
|
178 | " If the callback has a sender parameter, the ButtonWidget instance that\n", | |
157 | " called the callback will be passed into the method as the sender.\n", |
|
179 | " called the callback will be passed into the method as the sender.\n", | |
158 | "\n", |
|
180 | "\n", | |
159 | " Parameters\n", |
|
181 | " Parameters\n", | |
160 | " ----------\n", |
|
182 | " ----------\n", | |
161 | " remove : bool (optional)\n", |
|
183 | " remove : bool (optional)\n", | |
162 | " Set to true to remove the callback from the list of callbacks.\n" |
|
184 | " Set to true to remove the callback from the list of callbacks.\n" | |
163 | ] |
|
185 | ] | |
164 | } |
|
186 | } | |
165 | ], |
|
187 | ], | |
166 | "prompt_number": 4 |
|
188 | "prompt_number": 4 | |
167 | }, |
|
189 | }, | |
168 | { |
|
190 | { | |
169 | "cell_type": "markdown", |
|
191 | "cell_type": "markdown", | |
170 | "metadata": {}, |
|
192 | "metadata": {}, | |
171 | "source": [ |
|
193 | "source": [ | |
172 | "Button clicks are tracked by the `clicks` property of the button widget. By using the `on_click` method and the `clicks` property, a button that outputs how many times it has been clicked is shown below." |
|
194 | "Button clicks are transmitted from the front-end to the back-end using custom messages. By using the `on_click` method, a button that prints a message when it has been clicked is shown below." | |
173 | ] |
|
195 | ] | |
174 | }, |
|
196 | }, | |
175 | { |
|
197 | { | |
176 | "cell_type": "code", |
|
198 | "cell_type": "code", | |
177 | "collapsed": false, |
|
199 | "collapsed": false, | |
178 | "input": [ |
|
200 | "input": [ | |
179 | "button = widgets.ButtonWidget(description=\"Click Me!\")\n", |
|
201 | "button = widgets.ButtonWidget(description=\"Click Me!\")\n", | |
180 | "display(button)\n", |
|
202 | "display(button)\n", | |
181 | "\n", |
|
203 | "\n", | |
182 | "def on_button_clicked(sender):\n", |
|
204 | "def on_button_clicked(sender):\n", | |
183 |
" print(\"Button clicked |
|
205 | " print(\"Button clicked.\")\n", | |
184 | "\n", |
|
206 | "\n", | |
185 | "button.on_click(on_button_clicked)" |
|
207 | "button.on_click(on_button_clicked)" | |
186 | ], |
|
208 | ], | |
187 | "language": "python", |
|
209 | "language": "python", | |
188 | "metadata": {}, |
|
210 | "metadata": {}, | |
189 | "outputs": [ |
|
211 | "outputs": [ | |
190 | { |
|
212 | { | |
191 | "output_type": "stream", |
|
213 | "output_type": "stream", | |
192 | "stream": "stdout", |
|
214 | "stream": "stdout", | |
193 | "text": [ |
|
215 | "text": [ | |
194 |
"Button clicked |
|
216 | "Button clicked.\n" | |
195 | ] |
|
217 | ] | |
196 | }, |
|
218 | }, | |
197 | { |
|
219 | { | |
198 | "output_type": "stream", |
|
220 | "output_type": "stream", | |
199 | "stream": "stdout", |
|
221 | "stream": "stdout", | |
200 | "text": [ |
|
222 | "text": [ | |
201 |
"Button clicked |
|
223 | "Button clicked.\n" | |
202 | ] |
|
224 | ] | |
203 | }, |
|
225 | }, | |
204 | { |
|
226 | { | |
205 | "output_type": "stream", |
|
227 | "output_type": "stream", | |
206 | "stream": "stdout", |
|
228 | "stream": "stdout", | |
207 | "text": [ |
|
229 | "text": [ | |
208 |
"Button clicked |
|
230 | "Button clicked.\n" | |
209 | ] |
|
231 | ] | |
210 | } |
|
232 | } | |
211 | ], |
|
233 | ], | |
212 | "prompt_number": 5 |
|
234 | "prompt_number": 5 | |
213 | }, |
|
235 | }, | |
214 | { |
|
236 | { | |
215 | "cell_type": "markdown", |
|
237 | "cell_type": "markdown", | |
216 | "metadata": {}, |
|
238 | "metadata": {}, | |
217 | "source": [ |
|
239 | "source": [ | |
218 | "Event handlers can also be used to create widgets. In the example below, clicking a button spawns another button with a description equal to how many times the parent button had been clicked at the time." |
|
240 | "Event handlers can also be used to create widgets. In the example below, clicking a button spawns another button with a description equal to how many times the parent button had been clicked at the time." | |
219 | ] |
|
241 | ] | |
220 | }, |
|
242 | }, | |
221 | { |
|
243 | { | |
222 | "cell_type": "code", |
|
244 | "cell_type": "code", | |
223 | "collapsed": false, |
|
245 | "collapsed": false, | |
224 | "input": [ |
|
246 | "input": [ | |
225 | "def show_button(sender=None):\n", |
|
247 | "def show_button(sender=None):\n", | |
226 | " button = widgets.ButtonWidget()\n", |
|
248 | " button = widgets.ButtonWidget()\n", | |
227 | " button.description = \"%d\" % (sender.clicks if sender is not None else 0)\n", |
|
249 | " button.clicks = 0\n", | |
|
250 | " if sender is None:\n", | |||
|
251 | " button.description = \"0\"\n", | |||
|
252 | " else:\n", | |||
|
253 | " sender.clicks += 1\n", | |||
|
254 | " button.description = \"%d\" % sender.clicks\n", | |||
228 | " display(button)\n", |
|
255 | " display(button)\n", | |
229 | " button.on_click(show_button)\n", |
|
256 | " button.on_click(show_button)\n", | |
230 | "show_button()\n", |
|
257 | "show_button()\n", | |
231 | " " |
|
258 | " " | |
232 | ], |
|
259 | ], | |
233 | "language": "python", |
|
260 | "language": "python", | |
234 | "metadata": {}, |
|
261 | "metadata": {}, | |
235 | "outputs": [], |
|
262 | "outputs": [], | |
236 | "prompt_number": 6 |
|
263 | "prompt_number": 6 | |
237 | } |
|
264 | } | |
238 | ], |
|
265 | ], | |
239 | "metadata": {} |
|
266 | "metadata": {} | |
240 | } |
|
267 | } | |
241 | ] |
|
268 | ] | |
242 | } No newline at end of file |
|
269 | } |
General Comments 0
You need to be logged in to leave comments.
Login now