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 |
@@ -15,6 +15,7 b' define([' | |||
|
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", |
@@ -5,6 +5,7 b' 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 |
@@ -49,6 +49,7 b'' | |||
|
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", |
@@ -82,6 +83,7 b'' | |||
|
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", |
@@ -154,8 +156,6 b'' | |||
|
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", |
@@ -303,6 +303,7 b'' | |||
|
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", |
General Comments 0
You need to be logged in to leave comments.
Login now