##// END OF EJS Templates
Updated widget events for pydata2014
Jonathan Frederic -
Show More
@@ -7,7 +7,7 b''
7 ]
7 ]
8 ],
8 ],
9 "name": "",
9 "name": "",
10 "signature": "sha256:8cade57fabc6819dc950bc28502028554861fb1440d5d832922b95fd2b8bf25c"
10 "signature": "sha256:b22622c3f45a7044f2bec2270852ca07a20e589d185d87a6c659f6e3f33547f4"
11 },
11 },
12 "nbformat": 3,
12 "nbformat": 3,
13 "nbformat_minor": 0,
13 "nbformat_minor": 0,
@@ -18,36 +18,26 b''
18 "cell_type": "code",
18 "cell_type": "code",
19 "collapsed": false,
19 "collapsed": false,
20 "input": [
20 "input": [
21 "from __future__ import print_function # 2.7 compatability\n",
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"
25 ],
22 ],
26 "language": "python",
23 "language": "python",
27 "metadata": {},
24 "metadata": {},
28 "outputs": [],
25 "outputs": [],
29 "prompt_number": 1
26 "prompt_number": 12
30 },
31 {
32 "cell_type": "heading",
33 "level": 1,
34 "metadata": {},
35 "source": [
36 "Traitlet Events"
37 ]
38 },
27 },
39 {
28 {
40 "cell_type": "markdown",
29 "cell_type": "markdown",
41 "metadata": {},
30 "metadata": {},
42 "source": [
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 "cell_type": "code",
36 "cell_type": "code",
48 "collapsed": false,
37 "collapsed": false,
49 "input": [
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 "language": "python",
42 "language": "python",
53 "metadata": {},
43 "metadata": {},
@@ -56,94 +46,89 b''
56 "output_type": "stream",
46 "output_type": "stream",
57 "stream": "stdout",
47 "stream": "stdout",
58 "text": [
48 "text": [
59 "Setup a handler to be called when a trait changes.\n",
49 "Register a callback to execute when the button is clicked.\n",
60 "\n",
61 " This is used to setup dynamic notifications of trait changes.\n",
62 "\n",
50 "\n",
63 " Static handlers can be created by creating methods on a HasTraits\n",
51 " The callback will be called with one argument,\n",
64 " subclass with the naming convention '_[traitname]_changed'. Thus,\n",
52 " the clicked button widget instance.\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",
68 "\n",
53 "\n",
69 " Parameters\n",
54 " Parameters\n",
70 " ----------\n",
55 " ----------\n",
71 " handler : callable\n",
56 " remove : bool (optional)\n",
72 " A callable that is called when a trait changes. Its\n",
57 " Set to true to remove the callback from the list of callbacks.\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"
83 ]
58 ]
84 }
59 }
85 ],
60 ],
86 "prompt_number": 2
61 "prompt_number": 13
87 },
62 },
88 {
63 {
89 "cell_type": "markdown",
64 "cell_type": "markdown",
90 "metadata": {},
65 "metadata": {},
91 "source": [
66 "source": [
92 "Mentioned in the doc string, the callback registered can have 4 possible signatures:\n",
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."
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."
100 ]
68 ]
101 },
69 },
102 {
70 {
103 "cell_type": "code",
71 "cell_type": "code",
104 "collapsed": false,
72 "collapsed": false,
105 "input": [
73 "input": [
106 "int_range = widgets.IntSliderWidget()\n",
74 "from IPython.display import display\n",
107 "display(int_range)\n",
75 "button = widgets.ButtonWidget(description=\"Click Me!\")\n",
76 "display(button)\n",
108 "\n",
77 "\n",
109 "def on_value_change(name, value):\n",
78 "def on_button_clicked(b):\n",
110 " print(value)\n",
79 " print(\"Button clicked.\")\n",
111 "\n",
80 "\n",
112 "int_range.on_trait_change(on_value_change, 'value')"
81 "button.on_click(on_button_clicked)"
113 ],
82 ],
114 "language": "python",
83 "language": "python",
115 "metadata": {},
84 "metadata": {},
116 "outputs": [],
85 "outputs": [],
117 "prompt_number": 3
86 "prompt_number": 14
118 },
87 },
119 {
88 {
120 "cell_type": "heading",
89 "cell_type": "markdown",
121 "level": 1,
122 "metadata": {},
90 "metadata": {},
123 "source": [
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 "cell_type": "heading",
113 "cell_type": "heading",
129 "level": 2,
114 "level": 1,
130 "metadata": {},
115 "metadata": {},
131 "source": [
116 "source": [
132 "Button Click Event"
117 "Traitlet Events"
133 ]
118 ]
134 },
119 },
135 {
120 {
136 "cell_type": "markdown",
121 "cell_type": "markdown",
137 "metadata": {},
122 "metadata": {},
138 "source": [
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 "cell_type": "code",
128 "cell_type": "code",
144 "collapsed": false,
129 "collapsed": false,
145 "input": [
130 "input": [
146 "print(widgets.ButtonWidget.on_click.__doc__)"
131 "print(widgets.Widget.on_trait_change.__doc__)"
147 ],
132 ],
148 "language": "python",
133 "language": "python",
149 "metadata": {},
134 "metadata": {},
@@ -152,94 +137,72 b''
152 "output_type": "stream",
137 "output_type": "stream",
153 "stream": "stdout",
138 "stream": "stdout",
154 "text": [
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 "\n",
141 "\n",
157 " The callback will be called with one argument,\n",
142 " This is used to setup dynamic notifications of trait changes.\n",
158 " the clicked button widget instance.\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 "\n",
149 "\n",
160 " Parameters\n",
150 " Parameters\n",
161 " ----------\n",
151 " ----------\n",
162 " remove : bool (optional)\n",
152 " handler : callable\n",
163 " Set to true to remove the callback from the list of callbacks.\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": 4
167 "prompt_number": 16
168 },
168 },
169 {
169 {
170 "cell_type": "markdown",
170 "cell_type": "markdown",
171 "metadata": {},
171 "metadata": {},
172 "source": [
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 "cell_type": "code",
184 "cell_type": "code",
178 "collapsed": false,
185 "collapsed": false,
179 "input": [
186 "input": [
180 "button = widgets.ButtonWidget(description=\"Click Me!\")\n",
187 "int_range = widgets.IntSliderWidget()\n",
181 "display(button)\n",
188 "display(int_range)\n",
182 "\n",
189 "\n",
183 "def on_button_clicked(b):\n",
190 "def on_value_change(name, value):\n",
184 " print(\"Button clicked.\")\n",
191 " print(value)\n",
185 "\n",
192 "\n",
186 "button.on_click(on_button_clicked)"
193 "int_range.on_trait_change(on_value_change, 'value')"
187 ],
194 ],
188 "language": "python",
195 "language": "python",
189 "metadata": {},
196 "metadata": {},
190 "outputs": [
197 "outputs": [],
191 {
198 "prompt_number": 17
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
214 },
199 },
215 {
200 {
216 "cell_type": "markdown",
201 "cell_type": "markdown",
217 "metadata": {},
202 "metadata": {},
218 "source": [
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 "metadata": {}
208 "metadata": {}
General Comments 0
You need to be logged in to leave comments. Login now