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