##// END OF EJS Templates
Add ImageWidget
Jonathan Frederic -
Show More
@@ -0,0 +1,55 b''
1 //----------------------------------------------------------------------------
2 // Copyright (C) 2013 The IPython Development Team
3 //
4 // Distributed under the terms of the BSD License. The full license is in
5 // the file COPYING, distributed as part of this software.
6 //----------------------------------------------------------------------------
7
8 //============================================================================
9 // ImageWidget
10 //============================================================================
11
12 /**
13 * @module IPython
14 * @namespace IPython
15 **/
16
17 define(["notebook/js/widget"], function(widget_manager){
18 var ImageWidgetModel = IPython.WidgetModel.extend({});
19 widget_manager.register_widget_model('ImageWidgetModel', ImageWidgetModel);
20
21 var ImageView = IPython.WidgetView.extend({
22
23 // Called when view is rendered.
24 render : function(){
25 this.setElement($("<img />"));
26 this.update(); // Set defaults.
27 },
28
29 // Handles: Backend -> Frontend Sync
30 // Frontent -> Frontend Sync
31 update : function(){
32 var image_src = 'data:image/' + this.model.get('image_format') + ';base64,' + this.model.get('_b64value');
33 this.$el.attr('src', image_src);
34
35 var width = this.model.get('width');
36 if (width !== undefined && width.length > 0) {
37 this.$el.attr('width', width);
38 } else {
39 this.$el.removeAttr('width');
40 }
41
42 var height = this.model.get('height');
43 if (height !== undefined && height.length > 0) {
44 this.$el.attr('height', height);
45 } else {
46 this.$el.removeAttr('height');
47 }
48 return IPython.WidgetView.prototype.update.call(this);
49 },
50
51 });
52
53 widget_manager.register_widget_view('ImageView', ImageView);
54
55 });
@@ -0,0 +1,38 b''
1 """ButtonWidget class.
2
3 Represents a button in the frontend using a widget. Allows user to listen for
4 click events on the button and trigger backend code when the clicks are fired.
5 """
6 #-----------------------------------------------------------------------------
7 # Copyright (c) 2013, the IPython Development Team.
8 #
9 # Distributed under the terms of the Modified BSD License.
10 #
11 # The full license is in the file COPYING.txt, distributed with this software.
12 #-----------------------------------------------------------------------------
13
14 #-----------------------------------------------------------------------------
15 # Imports
16 #-----------------------------------------------------------------------------
17 import base64
18
19 from .widget import Widget
20 from IPython.utils.traitlets import Unicode, Bytes
21
22 #-----------------------------------------------------------------------------
23 # Classes
24 #-----------------------------------------------------------------------------
25 class ImageWidget(Widget):
26 target_name = Unicode('ImageWidgetModel')
27 default_view_name = Unicode('ImageView')
28
29 # Define the custom state properties to sync with the front-end
30 _keys = ['image_format', 'width', 'height', '_b64value']
31 image_format = Unicode('png')
32 width = Unicode()
33 height = Unicode()
34 _b64value = Unicode()
35
36 value = Bytes()
37 def _value_changed(self, name, old, new):
38 self._b64value = base64.b64encode(new) No newline at end of file
@@ -1,23 +1,24 b''
1 1 //----------------------------------------------------------------------------
2 2 // Copyright (C) 2013 The IPython Development Team
3 3 //
4 4 // Distributed under the terms of the BSD License. The full license is in
5 5 // the file COPYING, distributed as part of this software.
6 6 //----------------------------------------------------------------------------
7 7
8 8 //============================================================================
9 9 // Basic Widgets
10 10 //============================================================================
11 11
12 12 define([
13 13 "notebook/js/widgets/bool",
14 14 "notebook/js/widgets/button",
15 15 "notebook/js/widgets/container",
16 16 "notebook/js/widgets/float",
17 17 "notebook/js/widgets/float_range",
18 "notebook/js/widgets/image",
18 19 "notebook/js/widgets/int",
19 20 "notebook/js/widgets/int_range",
20 21 "notebook/js/widgets/multicontainer",
21 22 "notebook/js/widgets/selection",
22 23 "notebook/js/widgets/string",
23 24 ], function(){ return true; });
@@ -1,12 +1,13 b''
1 1 from .widget import Widget
2 2
3 3 from .widget_bool import BoolWidget
4 4 from .widget_button import ButtonWidget
5 5 from .widget_container import ContainerWidget
6 6 from .widget_float import FloatWidget
7 7 from .widget_float_range import FloatRangeWidget
8 from .widget_image import ImageWidget
8 9 from .widget_int import IntWidget
9 10 from .widget_int_range import IntRangeWidget
10 11 from .widget_multicontainer import MulticontainerWidget
11 12 from .widget_selection import SelectionWidget
12 13 from .widget_string import StringWidget
@@ -1,326 +1,327 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": "markdown",
18 18 "metadata": {},
19 19 "source": [
20 20 "To use IPython widgets in the notebook, the widget namespace and display function need to be imported."
21 21 ]
22 22 },
23 23 {
24 24 "cell_type": "code",
25 25 "collapsed": false,
26 26 "input": [
27 27 "from IPython.html import widgets # Widget definitions\n",
28 28 "from IPython.display import display # Used to display widgets in the notebook"
29 29 ],
30 30 "language": "python",
31 31 "metadata": {},
32 32 "outputs": [],
33 33 "prompt_number": 1
34 34 },
35 35 {
36 36 "cell_type": "heading",
37 37 "level": 1,
38 38 "metadata": {},
39 39 "source": [
40 40 "Basic Widgets"
41 41 ]
42 42 },
43 43 {
44 44 "cell_type": "markdown",
45 45 "metadata": {},
46 46 "source": [
47 47 "The IPython notebook comes preloaded with basic widgets that represent common data types. These widgets are\n",
48 48 "\n",
49 49 "- BoolWidget : boolean \n",
50 50 "- FloatRangeWidget : bounded float \n",
51 51 "- FloatWidget : unbounded float \n",
52 "- ImageWidget : image\n",
52 53 "- IntRangeWidget : bounded integer \n",
53 54 "- IntWidget : unbounded integer \n",
54 55 "- SelectionWidget : enumeration \n",
55 56 "- StringWidget : string \n",
56 57 "\n",
57 58 "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",
58 59 "\n",
59 60 "- ButtonWidget \n",
60 61 "- ContainerWidget \n",
61 62 "- MulticontainerWidget \n",
62 63 "\n",
63 64 "To see the complete list of widgets, one can execute the following"
64 65 ]
65 66 },
66 67 {
67 68 "cell_type": "code",
68 69 "collapsed": false,
69 70 "input": [
70 71 "[widget for widget in dir(widgets) if widget.endswith('Widget')]"
71 72 ],
72 73 "language": "python",
73 74 "metadata": {},
74 75 "outputs": [
75 76 {
76 77 "metadata": {},
77 78 "output_type": "pyout",
78 79 "prompt_number": 2,
79 80 "text": [
80 81 "['BoolWidget',\n",
81 82 " 'ButtonWidget',\n",
82 83 " 'ContainerWidget',\n",
83 84 " 'FloatRangeWidget',\n",
84 85 " 'FloatWidget',\n",
86 " 'ImageWidget',\n",
85 87 " 'IntRangeWidget',\n",
86 88 " 'IntWidget',\n",
87 89 " 'MulticontainerWidget',\n",
88 90 " 'SelectionWidget',\n",
89 91 " 'StringWidget',\n",
90 92 " 'Widget']"
91 93 ]
92 94 }
93 95 ],
94 96 "prompt_number": 2
95 97 },
96 98 {
97 99 "cell_type": "markdown",
98 100 "metadata": {},
99 101 "source": [
100 102 "The basic widgets can all be constructed without arguments. The following creates a FloatRangeWidget without displaying it"
101 103 ]
102 104 },
103 105 {
104 106 "cell_type": "code",
105 107 "collapsed": false,
106 108 "input": [
107 109 "mywidget = widgets.FloatRangeWidget()"
108 110 ],
109 111 "language": "python",
110 112 "metadata": {},
111 113 "outputs": [],
112 114 "prompt_number": 3
113 115 },
114 116 {
115 117 "cell_type": "markdown",
116 118 "metadata": {},
117 119 "source": [
118 120 "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. `mywidget` is displayed by"
119 121 ]
120 122 },
121 123 {
122 124 "cell_type": "code",
123 125 "collapsed": false,
124 126 "input": [
125 127 "display(mywidget)"
126 128 ],
127 129 "language": "python",
128 130 "metadata": {},
129 131 "outputs": [],
130 132 "prompt_number": 4
131 133 },
132 134 {
133 135 "cell_type": "markdown",
134 136 "metadata": {},
135 137 "source": [
136 138 "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",
137 139 "\n",
138 140 "Widgets are manipulated via special instance properties (traitlets). The names of these instance properties are listed in the widget's `keys` property (as seen below). A few of these properties are common to most, if not all, widgets. The common properties are `value`, `description`, `visible`, and `disabled`. `_css`, `_add_class`, and `_remove_class` are internal properties that exist in all widgets and should not be modified."
139 141 ]
140 142 },
141 143 {
142 144 "cell_type": "code",
143 145 "collapsed": false,
144 146 "input": [
145 147 "mywidget.keys"
146 148 ],
147 149 "language": "python",
148 150 "metadata": {},
149 151 "outputs": [
150 152 {
151 153 "metadata": {},
152 154 "output_type": "pyout",
153 155 "prompt_number": 5,
154 156 "text": [
155 157 "['visible',\n",
156 158 " '_css',\n",
157 " '_add_class',\n",
158 " '_remove_class',\n",
159 159 " 'value',\n",
160 160 " 'step',\n",
161 161 " 'max',\n",
162 162 " 'min',\n",
163 163 " 'disabled',\n",
164 164 " 'orientation',\n",
165 165 " 'description']"
166 166 ]
167 167 }
168 168 ],
169 169 "prompt_number": 5
170 170 },
171 171 {
172 172 "cell_type": "markdown",
173 173 "metadata": {},
174 174 "source": [
175 175 "Changing a widget's property value will automatically update that widget everywhere it is displayed in the notebook. Here the value of `mywidget` is set. The slider shown above (after input 4) updates automatically to the new value. In reverse, changing the value of the displayed widget will update the property's value."
176 176 ]
177 177 },
178 178 {
179 179 "cell_type": "code",
180 180 "collapsed": false,
181 181 "input": [
182 182 "mywidget.value = 25.0"
183 183 ],
184 184 "language": "python",
185 185 "metadata": {},
186 186 "outputs": [],
187 187 "prompt_number": 6
188 188 },
189 189 {
190 190 "cell_type": "markdown",
191 191 "metadata": {},
192 192 "source": [
193 193 "After changing the widget's value in the notebook by hand to 0.0 (sliding the bar to the far left)."
194 194 ]
195 195 },
196 196 {
197 197 "cell_type": "code",
198 198 "collapsed": false,
199 199 "input": [
200 200 "mywidget.value"
201 201 ],
202 202 "language": "python",
203 203 "metadata": {},
204 204 "outputs": [
205 205 {
206 206 "metadata": {},
207 207 "output_type": "pyout",
208 208 "prompt_number": 7,
209 209 "text": [
210 210 "0.0"
211 211 ]
212 212 }
213 213 ],
214 214 "prompt_number": 7
215 215 },
216 216 {
217 217 "cell_type": "markdown",
218 218 "metadata": {},
219 219 "source": [
220 220 "Widget property values can also be set with kwargs during the construction of the widget (as seen below)."
221 221 ]
222 222 },
223 223 {
224 224 "cell_type": "code",
225 225 "collapsed": false,
226 226 "input": [
227 227 "mysecondwidget = widgets.SelectionWidget(values=[\"Item A\", \"Item B\", \"Item C\"], value=\"Nothing Selected\")\n",
228 228 "display(mysecondwidget)"
229 229 ],
230 230 "language": "python",
231 231 "metadata": {},
232 232 "outputs": [],
233 233 "prompt_number": 8
234 234 },
235 235 {
236 236 "cell_type": "heading",
237 237 "level": 1,
238 238 "metadata": {},
239 239 "source": [
240 240 "Views"
241 241 ]
242 242 },
243 243 {
244 244 "cell_type": "markdown",
245 245 "metadata": {},
246 246 "source": [
247 247 "The data types that most of the widgets represent can be displayed more than one way. A `view` is a visual representation of a widget in the notebook. In the example in the section above, the default `view` for the `FloatRangeWidget` is used. The default view is set in the widgets `default_view_name` instance property (as seen below)."
248 248 ]
249 249 },
250 250 {
251 251 "cell_type": "code",
252 252 "collapsed": false,
253 253 "input": [
254 254 "mywidget.default_view_name"
255 255 ],
256 256 "language": "python",
257 257 "metadata": {},
258 258 "outputs": [
259 259 {
260 260 "metadata": {},
261 261 "output_type": "pyout",
262 262 "prompt_number": 9,
263 263 "text": [
264 264 "u'FloatSliderView'"
265 265 ]
266 266 }
267 267 ],
268 268 "prompt_number": 9
269 269 },
270 270 {
271 271 "cell_type": "markdown",
272 272 "metadata": {},
273 273 "source": [
274 274 "When a widget is displayed using `display(...)`, the `default_view_name` is used to determine what view type should be used to display the widget. View names are case sensitive. Sometimes the default view isn't the best view to represent a piece of data. To change what view is used, either the `default_view_name` can be changed or the `view_name` kwarg of `display` can be set. This also can be used to display one widget multiple ways in one output (as seen below)."
275 275 ]
276 276 },
277 277 {
278 278 "cell_type": "code",
279 279 "collapsed": false,
280 280 "input": [
281 281 "display(mywidget)\n",
282 282 "display(mywidget, view_name=\"FloatTextView\")"
283 283 ],
284 284 "language": "python",
285 285 "metadata": {},
286 286 "outputs": [],
287 287 "prompt_number": 10
288 288 },
289 289 {
290 290 "cell_type": "markdown",
291 291 "metadata": {},
292 292 "source": [
293 293 "Some views work with multiple different widget types and some views only work with one. The complete list of views and supported widgets is below. The default views are italicized.\n",
294 294 "\n",
295 295 "| Widget Name | View Names |\n",
296 296 "|:-----------------------|:--------------------|\n",
297 297 "| BoolWidget | *CheckboxView* |\n",
298 298 "| | ToggleButtonView |\n",
299 299 "| ButtonWidget | *ButtonView* |\n",
300 300 "| ContainerWidget | *ContainerView* |\n",
301 301 "| | ModalView |\n",
302 302 "| FloatRangeWidget | *FloatSliderView* |\n",
303 303 "| | FloatTextView |\n",
304 304 "| | ProgressView |\n",
305 305 "| FloatWidget | *FloatTextView* |\n",
306 "| ImageWidget | *ImageView* |\n",
306 307 "| IntRangeWidget | *IntSliderView* |\n",
307 308 "| | IntTextView |\n",
308 309 "| | ProgressView |\n",
309 310 "| IntWidget | *IntTextView* |\n",
310 311 "| MulticontainerWidget | AccordionView |\n",
311 312 "| | *TabView* |\n",
312 313 "| SelectionWidget | ToggleButtonsView |\n",
313 314 "| | RadioButtonsView |\n",
314 315 "| | *DropdownView* |\n",
315 316 "| | ListBoxView |\n",
316 317 "| StringWidget | HTMLView |\n",
317 318 "| | LatexView |\n",
318 319 "| | TextAreaView |\n",
319 320 "| | *TextBoxView* |\n"
320 321 ]
321 322 }
322 323 ],
323 324 "metadata": {}
324 325 }
325 326 ]
326 327 } No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now