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