##// END OF EJS Templates
s/image_format/format
Jonathan Frederic -
Show More
@@ -1,57 +1,57
1 //----------------------------------------------------------------------------
1 //----------------------------------------------------------------------------
2 // Copyright (C) 2013 The IPython Development Team
2 // Copyright (C) 2013 The IPython Development Team
3 //
3 //
4 // Distributed under the terms of the BSD License. The full license is in
4 // Distributed under the terms of the BSD License. The full license is in
5 // the file COPYING, distributed as part of this software.
5 // the file COPYING, distributed as part of this software.
6 //----------------------------------------------------------------------------
6 //----------------------------------------------------------------------------
7
7
8 //============================================================================
8 //============================================================================
9 // ImageWidget
9 // ImageWidget
10 //============================================================================
10 //============================================================================
11
11
12 /**
12 /**
13 * @module IPython
13 * @module IPython
14 * @namespace IPython
14 * @namespace IPython
15 **/
15 **/
16
16
17 define(["notebook/js/widgets/widget"], function(widget_manager){
17 define(["notebook/js/widgets/widget"], function(widget_manager){
18 var ImageWidgetModel = IPython.WidgetModel.extend({});
18 var ImageWidgetModel = IPython.WidgetModel.extend({});
19 widget_manager.register_widget_model('ImageWidgetModel', ImageWidgetModel);
19 widget_manager.register_widget_model('ImageWidgetModel', ImageWidgetModel);
20
20
21 var ImageView = IPython.DOMWidgetView.extend({
21 var ImageView = IPython.DOMWidgetView.extend({
22
22
23 // Called when view is rendered.
23 // Called when view is rendered.
24 render : function(){
24 render : function(){
25 this.setElement($("<img />"));
25 this.setElement($("<img />"));
26 this.update(); // Set defaults.
26 this.update(); // Set defaults.
27 },
27 },
28
28
29 update : function(){
29 update : function(){
30 // Update the contents of this view
30 // Update the contents of this view
31 //
31 //
32 // Called when the model is changed. The model may have been
32 // Called when the model is changed. The model may have been
33 // changed by another view or by a state update from the back-end.
33 // changed by another view or by a state update from the back-end.
34 var image_src = 'data:image/' + this.model.get('image_format') + ';base64,' + this.model.get('_b64value');
34 var image_src = 'data:image/' + this.model.get('format') + ';base64,' + this.model.get('_b64value');
35 this.$el.attr('src', image_src);
35 this.$el.attr('src', image_src);
36
36
37 var width = this.model.get('width');
37 var width = this.model.get('width');
38 if (width !== undefined && width.length > 0) {
38 if (width !== undefined && width.length > 0) {
39 this.$el.attr('width', width);
39 this.$el.attr('width', width);
40 } else {
40 } else {
41 this.$el.removeAttr('width');
41 this.$el.removeAttr('width');
42 }
42 }
43
43
44 var height = this.model.get('height');
44 var height = this.model.get('height');
45 if (height !== undefined && height.length > 0) {
45 if (height !== undefined && height.length > 0) {
46 this.$el.attr('height', height);
46 this.$el.attr('height', height);
47 } else {
47 } else {
48 this.$el.removeAttr('height');
48 this.$el.removeAttr('height');
49 }
49 }
50 return IPython.DOMWidgetView.prototype.update.call(this);
50 return IPython.DOMWidgetView.prototype.update.call(this);
51 },
51 },
52
52
53 });
53 });
54
54
55 widget_manager.register_widget_view('ImageView', ImageView);
55 widget_manager.register_widget_view('ImageView', ImageView);
56
56
57 });
57 });
@@ -1,38 +1,38
1 """ButtonWidget class.
1 """ButtonWidget class.
2
2
3 Represents a button in the frontend using a widget. Allows user to listen for
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.
4 click events on the button and trigger backend code when the clicks are fired.
5 """
5 """
6 #-----------------------------------------------------------------------------
6 #-----------------------------------------------------------------------------
7 # Copyright (c) 2013, the IPython Development Team.
7 # Copyright (c) 2013, the IPython Development Team.
8 #
8 #
9 # Distributed under the terms of the Modified BSD License.
9 # Distributed under the terms of the Modified BSD License.
10 #
10 #
11 # The full license is in the file COPYING.txt, distributed with this software.
11 # The full license is in the file COPYING.txt, distributed with this software.
12 #-----------------------------------------------------------------------------
12 #-----------------------------------------------------------------------------
13
13
14 #-----------------------------------------------------------------------------
14 #-----------------------------------------------------------------------------
15 # Imports
15 # Imports
16 #-----------------------------------------------------------------------------
16 #-----------------------------------------------------------------------------
17 import base64
17 import base64
18
18
19 from .widget import DOMWidget
19 from .widget import DOMWidget
20 from IPython.utils.traitlets import Unicode, Bytes
20 from IPython.utils.traitlets import Unicode, Bytes
21
21
22 #-----------------------------------------------------------------------------
22 #-----------------------------------------------------------------------------
23 # Classes
23 # Classes
24 #-----------------------------------------------------------------------------
24 #-----------------------------------------------------------------------------
25 class ImageWidget(DOMWidget):
25 class ImageWidget(DOMWidget):
26 target_name = Unicode('ImageWidgetModel')
26 target_name = Unicode('ImageWidgetModel')
27 view_name = Unicode('ImageView')
27 view_name = Unicode('ImageView')
28
28
29 # Define the custom state properties to sync with the front-end
29 # Define the custom state properties to sync with the front-end
30 keys = ['image_format', 'width', 'height', '_b64value'] + DOMWidget.keys
30 keys = ['format', 'width', 'height', '_b64value'] + DOMWidget.keys
31 image_format = Unicode('png')
31 format = Unicode('png')
32 width = Unicode()
32 width = Unicode()
33 height = Unicode()
33 height = Unicode()
34 _b64value = Unicode()
34 _b64value = Unicode()
35
35
36 value = Bytes()
36 value = Bytes()
37 def _value_changed(self, name, old, new):
37 def _value_changed(self, name, old, new):
38 self._b64value = base64.b64encode(new) No newline at end of file
38 self._b64value = base64.b64encode(new)
General Comments 0
You need to be logged in to leave comments. Login now