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