Show More
@@ -1,10 +1,10 b'' | |||
|
1 |
from |
|
|
1 | from widget import Widget, init_widget_js | |
|
2 | 2 | |
|
3 | from bool import BoolWidget | |
|
4 | from container import ContainerWidget | |
|
5 | from float import FloatWidget | |
|
6 | from float_range import FloatRangeWidget | |
|
7 | from int import IntWidget | |
|
8 | from int_range import IntRangeWidget | |
|
9 | from selection import SelectionWidget | |
|
10 | from string import StringWidget | |
|
3 | from widget_bool import BoolWidget | |
|
4 | from widget_container import ContainerWidget | |
|
5 | from widget_float import FloatWidget | |
|
6 | from widget_float_range import FloatRangeWidget | |
|
7 | from widget_int import IntWidget | |
|
8 | from widget_int_range import IntRangeWidget | |
|
9 | from widget_selection import SelectionWidget | |
|
10 | from widget_string import StringWidget |
@@ -1,164 +1,165 b'' | |||
|
1 | 1 | from copy import copy |
|
2 | 2 | from glob import glob |
|
3 | 3 | import uuid |
|
4 | 4 | import sys |
|
5 | 5 | import os |
|
6 | 6 | |
|
7 | 7 | import IPython |
|
8 | 8 | from IPython.kernel.comm import Comm |
|
9 | 9 | from IPython.config import LoggingConfigurable |
|
10 | 10 | from IPython.utils.traitlets import Unicode, Dict, List |
|
11 | 11 | from IPython.display import Javascript, display |
|
12 | 12 | from IPython.utils.py3compat import string_types |
|
13 | 13 | |
|
14 | 14 | def init_widget_js(): |
|
15 | 15 | path = os.path.split(os.path.abspath( __file__ ))[0] |
|
16 | 16 | for filepath in glob(os.path.join(path, "*.py")): |
|
17 | 17 | filename = os.path.split(filepath)[1] |
|
18 | 18 | name = filename.rsplit('.', 1)[0] |
|
19 |
if not (name == ' |
|
|
20 | js_path = 'static/notebook/js/widgets/%s.js' % name | |
|
19 | if not (name == 'widget' or name == '__init__') and name.startswith('widget_'): | |
|
20 | # Remove 'widget_' from the start of the name before compiling the path. | |
|
21 | js_path = 'static/notebook/js/widgets/%s.js' % name[7:] | |
|
21 | 22 | display(Javascript(data='$.getScript("%s");' % js_path)) |
|
22 | 23 | |
|
23 | 24 | |
|
24 | 25 | class Widget(LoggingConfigurable): |
|
25 | 26 | |
|
26 | 27 | ### Public declarations |
|
27 | 28 | target_name = Unicode('widget') |
|
28 | 29 | default_view_name = Unicode() |
|
29 | 30 | |
|
30 | 31 | |
|
31 | 32 | ### Private/protected declarations |
|
32 | 33 | _keys = [] |
|
33 | 34 | _property_lock = False |
|
34 | 35 | _parent = None |
|
35 | 36 | _children = [] |
|
36 | 37 | _css = Dict() |
|
37 | 38 | |
|
38 | 39 | |
|
39 | 40 | ### Public constructor |
|
40 | 41 | def __init__(self, parent=None, **kwargs): |
|
41 | 42 | super(Widget, self).__init__(**kwargs) |
|
42 | 43 | |
|
43 | 44 | self._children = [] |
|
44 | 45 | if parent is not None: |
|
45 | 46 | parent._children.append(self) |
|
46 | 47 | self._parent = parent |
|
47 | 48 | self.comm = None |
|
48 | 49 | |
|
49 | 50 | # Register after init to allow default values to be specified |
|
50 | 51 | self.on_trait_change(self._handle_property_changed, self.keys) |
|
51 | 52 | |
|
52 | 53 | |
|
53 | 54 | def __del__(self): |
|
54 | 55 | self.close() |
|
55 | 56 | |
|
56 | 57 | def close(self): |
|
57 | 58 | self.comm.close() |
|
58 | 59 | del self.comm |
|
59 | 60 | |
|
60 | 61 | |
|
61 | 62 | ### Properties |
|
62 | 63 | def _get_parent(self): |
|
63 | 64 | return self._parent |
|
64 | 65 | parent = property(_get_parent) |
|
65 | 66 | |
|
66 | 67 | |
|
67 | 68 | def _get_children(self): |
|
68 | 69 | return copy(self._children) |
|
69 | 70 | children = property(_get_children) |
|
70 | 71 | |
|
71 | 72 | |
|
72 | 73 | def _get_keys(self): |
|
73 | 74 | keys = ['_css'] |
|
74 | 75 | keys.extend(self._keys) |
|
75 | 76 | return keys |
|
76 | 77 | keys = property(_get_keys) |
|
77 | 78 | |
|
78 | 79 | def _get_css(self, key, selector=""): |
|
79 | 80 | if selector in self._css and key in self._css[selector]: |
|
80 | 81 | return self._css[selector][key] |
|
81 | 82 | else: |
|
82 | 83 | return None |
|
83 | 84 | def _set_css(self, value, key, selector=""): |
|
84 | 85 | if selector not in self._css: |
|
85 | 86 | self._css[selector] = {} |
|
86 | 87 | |
|
87 | 88 | # Only update the property if it has changed. |
|
88 | 89 | if not (key in self._css[selector] and value in self._css[selector][key]): |
|
89 | 90 | self._css[selector][key] = value |
|
90 | 91 | self.send_state() # Send new state to client. |
|
91 | 92 | |
|
92 | 93 | css = property(_get_css, _set_css) |
|
93 | 94 | |
|
94 | 95 | |
|
95 | 96 | ### Event handlers |
|
96 | 97 | def _handle_msg(self, msg): |
|
97 | 98 | |
|
98 | 99 | # Handle backbone sync methods |
|
99 | 100 | sync_method = msg['content']['data']['sync_method'] |
|
100 | 101 | sync_data = msg['content']['data']['sync_data'] |
|
101 | 102 | if sync_method.lower() in ['create', 'update']: |
|
102 | 103 | self._handle_recieve_state(sync_data) |
|
103 | 104 | |
|
104 | 105 | |
|
105 | 106 | def _handle_recieve_state(self, sync_data): |
|
106 | 107 | self._property_lock = True |
|
107 | 108 | try: |
|
108 | 109 | |
|
109 | 110 | # Use _keys instead of keys - Don't get retrieve the css from the client side. |
|
110 | 111 | for name in self._keys: |
|
111 | 112 | if name in sync_data: |
|
112 | 113 | setattr(self, name, sync_data[name]) |
|
113 | 114 | finally: |
|
114 | 115 | self._property_lock = False |
|
115 | 116 | |
|
116 | 117 | |
|
117 | 118 | def _handle_property_changed(self, name, old, new): |
|
118 | 119 | if not self._property_lock and self.comm is not None: |
|
119 | 120 | # TODO: Validate properties. |
|
120 | 121 | # Send new state to frontend |
|
121 | 122 | self.send_state() |
|
122 | 123 | |
|
123 | 124 | |
|
124 | 125 | def _handle_close(self): |
|
125 | 126 | self.comm = None |
|
126 | 127 | |
|
127 | 128 | |
|
128 | 129 | ### Public methods |
|
129 | 130 | def _repr_widget_(self, view_name=None): |
|
130 | 131 | if not view_name: |
|
131 | 132 | view_name = self.default_view_name |
|
132 | 133 | |
|
133 | 134 | # Create a comm. |
|
134 | 135 | if self.comm is None: |
|
135 | 136 | self.comm = Comm(target_name=self.target_name) |
|
136 | 137 | self.comm.on_msg(self._handle_msg) |
|
137 | 138 | self.comm.on_close(self._handle_close) |
|
138 | 139 | |
|
139 | 140 | # Make sure model is syncronized |
|
140 | 141 | self.send_state() |
|
141 | 142 | |
|
142 | 143 | # Show view. |
|
143 | 144 | if self.parent is None: |
|
144 | 145 | self.comm.send({"method": "show", "view_name": view_name}) |
|
145 | 146 | else: |
|
146 | 147 | self.comm.send({"method": "show", |
|
147 | 148 | "view_name": view_name, |
|
148 | 149 | "parent": self.parent.comm.comm_id}) |
|
149 | 150 | |
|
150 | 151 | # Now show children if any. |
|
151 | 152 | for child in self.children: |
|
152 | 153 | child._repr_widget_() |
|
153 | 154 | return None |
|
154 | 155 | |
|
155 | 156 | |
|
156 | 157 | def send_state(self): |
|
157 | 158 | state = {} |
|
158 | 159 | for key in self.keys: |
|
159 | 160 | try: |
|
160 | 161 | state[key] = getattr(self, key) |
|
161 | 162 | except Exception as e: |
|
162 | 163 | pass # Eat errors, nom nom nom |
|
163 | 164 | self.comm.send({"method": "update", |
|
164 | 165 | "state": state}) |
|
1 | NO CONTENT: file renamed from IPython/html/widgets/bool.py to IPython/html/widgets/widget_bool.py |
|
1 | NO CONTENT: file renamed from IPython/html/widgets/container.py to IPython/html/widgets/widget_container.py |
|
1 | NO CONTENT: file renamed from IPython/html/widgets/float.py to IPython/html/widgets/widget_float.py |
|
1 | NO CONTENT: file renamed from IPython/html/widgets/float_range.py to IPython/html/widgets/widget_float_range.py |
|
1 | NO CONTENT: file renamed from IPython/html/widgets/int.py to IPython/html/widgets/widget_int.py |
|
1 | NO CONTENT: file renamed from IPython/html/widgets/int_range.py to IPython/html/widgets/widget_int_range.py |
|
1 | NO CONTENT: file renamed from IPython/html/widgets/selection.py to IPython/html/widgets/widget_selection.py |
|
1 | NO CONTENT: file renamed from IPython/html/widgets/string.py to IPython/html/widgets/widget_string.py |
General Comments 0
You need to be logged in to leave comments.
Login now