Show More
@@ -1,457 +1,457 b'' | |||||
1 | """Base Widget class. Allows user to create widgets in the backend that render |
|
1 | """Base Widget class. Allows user to create widgets in the backend that render | |
2 | in the IPython notebook frontend. |
|
2 | in the IPython notebook frontend. | |
3 | """ |
|
3 | """ | |
4 | #----------------------------------------------------------------------------- |
|
4 | #----------------------------------------------------------------------------- | |
5 | # Copyright (c) 2013, the IPython Development Team. |
|
5 | # Copyright (c) 2013, the IPython Development Team. | |
6 | # |
|
6 | # | |
7 | # Distributed under the terms of the Modified BSD License. |
|
7 | # Distributed under the terms of the Modified BSD License. | |
8 | # |
|
8 | # | |
9 | # The full license is in the file COPYING.txt, distributed with this software. |
|
9 | # The full license is in the file COPYING.txt, distributed with this software. | |
10 | #----------------------------------------------------------------------------- |
|
10 | #----------------------------------------------------------------------------- | |
11 |
|
11 | |||
12 | #----------------------------------------------------------------------------- |
|
12 | #----------------------------------------------------------------------------- | |
13 | # Imports |
|
13 | # Imports | |
14 | #----------------------------------------------------------------------------- |
|
14 | #----------------------------------------------------------------------------- | |
15 | from copy import copy |
|
15 | from copy import copy | |
16 | from glob import glob |
|
16 | from glob import glob | |
17 | import uuid |
|
17 | import uuid | |
18 | import sys |
|
18 | import sys | |
19 | import os |
|
19 | import os | |
20 | import inspect |
|
20 | import inspect | |
21 | import types |
|
21 | import types | |
22 |
|
22 | |||
23 | import IPython |
|
23 | import IPython | |
24 | from IPython.kernel.comm import Comm |
|
24 | from IPython.kernel.comm import Comm | |
25 | from IPython.config import LoggingConfigurable |
|
25 | from IPython.config import LoggingConfigurable | |
26 | from IPython.utils.traitlets import Unicode, Dict, List, Instance, Bool |
|
26 | from IPython.utils.traitlets import Unicode, Dict, List, Instance, Bool | |
27 | from IPython.display import Javascript, display |
|
27 | from IPython.display import Javascript, display | |
28 | from IPython.utils.py3compat import string_types |
|
28 | from IPython.utils.py3compat import string_types | |
29 |
|
29 | |||
30 | #----------------------------------------------------------------------------- |
|
30 | #----------------------------------------------------------------------------- | |
31 | # Classes |
|
31 | # Classes | |
32 | #----------------------------------------------------------------------------- |
|
32 | #----------------------------------------------------------------------------- | |
33 |
|
33 | |||
34 | class BaseWidget(LoggingConfigurable): |
|
34 | class BaseWidget(LoggingConfigurable): | |
35 |
|
35 | |||
36 | # Shared declarations (Class level) |
|
36 | # Shared declarations (Class level) | |
37 | _keys = List(Unicode, default_value = [], |
|
37 | _keys = List(Unicode, default_value = [], | |
38 | help="List of keys comprising the state of the model.", allow_none=False) |
|
38 | help="List of keys comprising the state of the model.", allow_none=False) | |
39 | widget_construction_callback = None |
|
39 | widget_construction_callback = None | |
40 |
|
40 | |||
41 | def on_widget_constructed(callback): |
|
41 | def on_widget_constructed(callback): | |
42 | """Class method, registers a callback to be called when a widget is |
|
42 | """Class method, registers a callback to be called when a widget is | |
43 | constructed. The callback must have the following signature: |
|
43 | constructed. The callback must have the following signature: | |
44 | callback(widget)""" |
|
44 | callback(widget)""" | |
45 | BaseWidget.widget_construction_callback = callback |
|
45 | BaseWidget.widget_construction_callback = callback | |
46 |
|
46 | |||
47 | def _handle_widget_constructed(widget): |
|
47 | def _handle_widget_constructed(widget): | |
48 | """Class method, called when a widget is constructed.""" |
|
48 | """Class method, called when a widget is constructed.""" | |
49 | if BaseWidget.widget_construction_callback is not None and callable(BaseWidget.widget_construction_callback): |
|
49 | if BaseWidget.widget_construction_callback is not None and callable(BaseWidget.widget_construction_callback): | |
50 | BaseWidget.widget_construction_callback(widget) |
|
50 | BaseWidget.widget_construction_callback(widget) | |
51 |
|
51 | |||
52 |
|
52 | |||
53 |
|
53 | |||
54 | # Public declarations (Instance level) |
|
54 | # Public declarations (Instance level) | |
55 | target_name = Unicode('widget', help="""Name of the backbone model |
|
55 | target_name = Unicode('widget', help="""Name of the backbone model | |
56 | registered in the frontend to create and sync this widget with.""") |
|
56 | registered in the frontend to create and sync this widget with.""") | |
57 | default_view_name = Unicode(help="""Default view registered in the frontend |
|
57 | default_view_name = Unicode(help="""Default view registered in the frontend | |
58 | to use to represent the widget.""") |
|
58 | to use to represent the widget.""") | |
59 |
|
59 | |||
60 | # Private/protected declarations |
|
60 | # Private/protected declarations | |
61 | # todo: change this to a context manager |
|
61 | # todo: change this to a context manager | |
62 | _property_lock = (None, None) # Last updated (key, value) from the front-end. Prevents echo. |
|
62 | _property_lock = (None, None) # Last updated (key, value) from the front-end. Prevents echo. | |
63 | _displayed = False |
|
63 | _displayed = False | |
64 | _comm = Instance('IPython.kernel.comm.Comm') |
|
64 | _comm = Instance('IPython.kernel.comm.Comm') | |
65 |
|
65 | |||
66 | def __init__(self, **kwargs): |
|
66 | def __init__(self, **kwargs): | |
67 | """Public constructor |
|
67 | """Public constructor | |
68 | """ |
|
68 | """ | |
69 | self._display_callbacks = [] |
|
69 | self._display_callbacks = [] | |
70 | self._msg_callbacks = [] |
|
70 | self._msg_callbacks = [] | |
71 | super(BaseWidget, self).__init__(**kwargs) |
|
71 | super(BaseWidget, self).__init__(**kwargs) | |
72 |
|
72 | |||
73 | self.on_trait_change(self._handle_property_changed, self.keys) |
|
73 | self.on_trait_change(self._handle_property_changed, self.keys) | |
74 | BaseWidget._handle_widget_constructed(self) |
|
74 | BaseWidget._handle_widget_constructed(self) | |
75 |
|
75 | |||
76 | def __del__(self): |
|
76 | def __del__(self): | |
77 | """Object disposal""" |
|
77 | """Object disposal""" | |
78 | self.close() |
|
78 | self.close() | |
79 |
|
79 | |||
80 |
|
80 | |||
81 | def close(self): |
|
81 | def close(self): | |
82 | """Close method. Closes the widget which closes the underlying comm. |
|
82 | """Close method. Closes the widget which closes the underlying comm. | |
83 | When the comm is closed, all of the widget views are automatically |
|
83 | When the comm is closed, all of the widget views are automatically | |
84 | removed from the frontend.""" |
|
84 | removed from the frontend.""" | |
85 | self._close_communication() |
|
85 | self._close_communication() | |
86 |
|
86 | |||
87 |
|
87 | |||
88 | # Properties |
|
88 | # Properties | |
89 | @property |
|
89 | @property | |
90 | def keys(self): |
|
90 | def keys(self): | |
91 | keys = ['default_view_name'] |
|
91 | keys = ['default_view_name'] | |
92 | keys.extend(self._keys) |
|
92 | keys.extend(self._keys) | |
93 | return keys |
|
93 | return keys | |
94 |
|
94 | |||
95 | @property |
|
95 | @property | |
96 | def comm(self): |
|
96 | def comm(self): | |
97 | if self._comm is None: |
|
97 | if self._comm is None: | |
98 | self._open_communication() |
|
98 | self._open_communication() | |
99 | return self._comm |
|
99 | return self._comm | |
100 |
|
100 | |||
101 | @property |
|
101 | @property | |
102 | def model_id(self): |
|
102 | def model_id(self): | |
103 |
return self. |
|
103 | return self.comm.comm_id | |
104 |
|
104 | |||
105 | # Event handlers |
|
105 | # Event handlers | |
106 | def _handle_msg(self, msg): |
|
106 | def _handle_msg(self, msg): | |
107 | """Called when a msg is recieved from the frontend""" |
|
107 | """Called when a msg is recieved from the frontend""" | |
108 | data = msg['content']['data'] |
|
108 | data = msg['content']['data'] | |
109 | method = data['method'] |
|
109 | method = data['method'] | |
110 |
|
110 | |||
111 | # Handle backbone sync methods CREATE, PATCH, and UPDATE |
|
111 | # Handle backbone sync methods CREATE, PATCH, and UPDATE | |
112 | if method == 'backbone': |
|
112 | if method == 'backbone': | |
113 | if 'sync_method' in data and 'sync_data' in data: |
|
113 | if 'sync_method' in data and 'sync_data' in data: | |
114 | sync_method = data['sync_method'] |
|
114 | sync_method = data['sync_method'] | |
115 | sync_data = data['sync_data'] |
|
115 | sync_data = data['sync_data'] | |
116 | self._handle_recieve_state(sync_data) # handles all methods |
|
116 | self._handle_recieve_state(sync_data) # handles all methods | |
117 |
|
117 | |||
118 | # Handle a custom msg from the front-end |
|
118 | # Handle a custom msg from the front-end | |
119 | elif method == 'custom': |
|
119 | elif method == 'custom': | |
120 | if 'custom_content' in data: |
|
120 | if 'custom_content' in data: | |
121 | self._handle_custom_msg(data['custom_content']) |
|
121 | self._handle_custom_msg(data['custom_content']) | |
122 |
|
122 | |||
123 | def _handle_custom_msg(self, content): |
|
123 | def _handle_custom_msg(self, content): | |
124 | """Called when a custom msg is recieved.""" |
|
124 | """Called when a custom msg is recieved.""" | |
125 | for handler in self._msg_callbacks: |
|
125 | for handler in self._msg_callbacks: | |
126 | if callable(handler): |
|
126 | if callable(handler): | |
127 | argspec = inspect.getargspec(handler) |
|
127 | argspec = inspect.getargspec(handler) | |
128 | nargs = len(argspec[0]) |
|
128 | nargs = len(argspec[0]) | |
129 |
|
129 | |||
130 | # Bound methods have an additional 'self' argument |
|
130 | # Bound methods have an additional 'self' argument | |
131 | if isinstance(handler, types.MethodType): |
|
131 | if isinstance(handler, types.MethodType): | |
132 | nargs -= 1 |
|
132 | nargs -= 1 | |
133 |
|
133 | |||
134 | # Call the callback |
|
134 | # Call the callback | |
135 | if nargs == 1: |
|
135 | if nargs == 1: | |
136 | handler(content) |
|
136 | handler(content) | |
137 | elif nargs == 2: |
|
137 | elif nargs == 2: | |
138 | handler(self, content) |
|
138 | handler(self, content) | |
139 | else: |
|
139 | else: | |
140 | raise TypeError('Widget msg callback must ' \ |
|
140 | raise TypeError('Widget msg callback must ' \ | |
141 | 'accept 1 or 2 arguments, not %d.' % nargs) |
|
141 | 'accept 1 or 2 arguments, not %d.' % nargs) | |
142 |
|
142 | |||
143 |
|
143 | |||
144 | def _handle_recieve_state(self, sync_data): |
|
144 | def _handle_recieve_state(self, sync_data): | |
145 | """Called when a state is recieved from the frontend.""" |
|
145 | """Called when a state is recieved from the frontend.""" | |
146 | # Use _keys instead of keys - Don't get retrieve the css from the client side. |
|
146 | # Use _keys instead of keys - Don't get retrieve the css from the client side. | |
147 | for name in self._keys: |
|
147 | for name in self._keys: | |
148 | if name in sync_data: |
|
148 | if name in sync_data: | |
149 | try: |
|
149 | try: | |
150 | self._property_lock = (name, sync_data[name]) |
|
150 | self._property_lock = (name, sync_data[name]) | |
151 | setattr(self, name, sync_data[name]) |
|
151 | setattr(self, name, sync_data[name]) | |
152 | finally: |
|
152 | finally: | |
153 | self._property_lock = (None, None) |
|
153 | self._property_lock = (None, None) | |
154 |
|
154 | |||
155 |
|
155 | |||
156 | def _handle_property_changed(self, name, old, new): |
|
156 | def _handle_property_changed(self, name, old, new): | |
157 | """Called when a property has been changed.""" |
|
157 | """Called when a property has been changed.""" | |
158 | # Make sure this isn't information that the front-end just sent us. |
|
158 | # Make sure this isn't information that the front-end just sent us. | |
159 | if self._property_lock[0] != name and self._property_lock[1] != new: |
|
159 | if self._property_lock[0] != name and self._property_lock[1] != new: | |
160 | # Send new state to frontend |
|
160 | # Send new state to frontend | |
161 | self.send_state(key=name) |
|
161 | self.send_state(key=name) | |
162 |
|
162 | |||
163 | def _handle_displayed(self, **kwargs): |
|
163 | def _handle_displayed(self, **kwargs): | |
164 | """Called when a view has been displayed for this widget instance |
|
164 | """Called when a view has been displayed for this widget instance | |
165 |
|
165 | |||
166 | Parameters |
|
166 | Parameters | |
167 | ---------- |
|
167 | ---------- | |
168 | [view_name]: unicode (optional kwarg) |
|
168 | [view_name]: unicode (optional kwarg) | |
169 | Name of the view that was displayed.""" |
|
169 | Name of the view that was displayed.""" | |
170 | for handler in self._display_callbacks: |
|
170 | for handler in self._display_callbacks: | |
171 | if callable(handler): |
|
171 | if callable(handler): | |
172 | argspec = inspect.getargspec(handler) |
|
172 | argspec = inspect.getargspec(handler) | |
173 | nargs = len(argspec[0]) |
|
173 | nargs = len(argspec[0]) | |
174 |
|
174 | |||
175 | # Bound methods have an additional 'self' argument |
|
175 | # Bound methods have an additional 'self' argument | |
176 | if isinstance(handler, types.MethodType): |
|
176 | if isinstance(handler, types.MethodType): | |
177 | nargs -= 1 |
|
177 | nargs -= 1 | |
178 |
|
178 | |||
179 | # Call the callback |
|
179 | # Call the callback | |
180 | if nargs == 0: |
|
180 | if nargs == 0: | |
181 | handler() |
|
181 | handler() | |
182 | elif nargs == 1: |
|
182 | elif nargs == 1: | |
183 | handler(self) |
|
183 | handler(self) | |
184 | elif nargs == 2: |
|
184 | elif nargs == 2: | |
185 | handler(self, kwargs.get('view_name', None)) |
|
185 | handler(self, kwargs.get('view_name', None)) | |
186 | else: |
|
186 | else: | |
187 | handler(self, **kwargs) |
|
187 | handler(self, **kwargs) | |
188 |
|
188 | |||
189 | # Public methods |
|
189 | # Public methods | |
190 | def send_state(self, key=None): |
|
190 | def send_state(self, key=None): | |
191 | """Sends the widget state, or a piece of it, to the frontend. |
|
191 | """Sends the widget state, or a piece of it, to the frontend. | |
192 |
|
192 | |||
193 | Parameters |
|
193 | Parameters | |
194 | ---------- |
|
194 | ---------- | |
195 | key : unicode (optional) |
|
195 | key : unicode (optional) | |
196 | A single property's name to sync with the frontend. |
|
196 | A single property's name to sync with the frontend. | |
197 | """ |
|
197 | """ | |
198 | self._send({"method": "update", |
|
198 | self._send({"method": "update", | |
199 | "state": self.get_state()}) |
|
199 | "state": self.get_state()}) | |
200 |
|
200 | |||
201 | def get_state(self, key=None): |
|
201 | def get_state(self, key=None): | |
202 | """Gets the widget state, or a piece of it. |
|
202 | """Gets the widget state, or a piece of it. | |
203 |
|
203 | |||
204 | Parameters |
|
204 | Parameters | |
205 | ---------- |
|
205 | ---------- | |
206 | key : unicode (optional) |
|
206 | key : unicode (optional) | |
207 | A single property's name to get. |
|
207 | A single property's name to get. | |
208 | """ |
|
208 | """ | |
209 | state = {} |
|
209 | state = {} | |
210 |
|
210 | |||
211 | # If a key is provided, just send the state of that key. |
|
211 | # If a key is provided, just send the state of that key. | |
212 | if key is None: |
|
212 | if key is None: | |
213 | keys = self.keys[:] |
|
213 | keys = self.keys[:] | |
214 | else: |
|
214 | else: | |
215 | keys = [key] |
|
215 | keys = [key] | |
216 | for k in keys: |
|
216 | for k in keys: | |
217 | value = getattr(self, k) |
|
217 | value = getattr(self, k) | |
218 |
|
218 | |||
219 | # a more elegant solution to encoding BaseWidgets would be |
|
219 | # a more elegant solution to encoding BaseWidgets would be | |
220 | # to tap into the JSON encoder and teach it how to deal |
|
220 | # to tap into the JSON encoder and teach it how to deal | |
221 | # with BaseWidget objects, or maybe just teach the JSON |
|
221 | # with BaseWidget objects, or maybe just teach the JSON | |
222 | # encoder to look for a _repr_json property before giving |
|
222 | # encoder to look for a _repr_json property before giving | |
223 | # up encoding |
|
223 | # up encoding | |
224 | if isinstance(value, BaseWidget): |
|
224 | if isinstance(value, BaseWidget): | |
225 | value = value.model_id |
|
225 | value = value.model_id | |
226 | elif isinstance(value, list) and len(value)>0 and isinstance(value[0], BaseWidget): |
|
226 | elif isinstance(value, list) and len(value)>0 and isinstance(value[0], BaseWidget): | |
227 | # assume all elements of the list are widgets |
|
227 | # assume all elements of the list are widgets | |
228 | value = [i.model_id for i in value] |
|
228 | value = [i.model_id for i in value] | |
229 | state[k] = value |
|
229 | state[k] = value | |
230 | return state |
|
230 | return state | |
231 |
|
231 | |||
232 |
|
232 | |||
233 | def send(self, content): |
|
233 | def send(self, content): | |
234 | """Sends a custom msg to the widget model in the front-end. |
|
234 | """Sends a custom msg to the widget model in the front-end. | |
235 |
|
235 | |||
236 | Parameters |
|
236 | Parameters | |
237 | ---------- |
|
237 | ---------- | |
238 | content : dict |
|
238 | content : dict | |
239 | Content of the message to send. |
|
239 | Content of the message to send. | |
240 | """ |
|
240 | """ | |
241 | self._send({"method": "custom", |
|
241 | self._send({"method": "custom", | |
242 | "custom_content": content}) |
|
242 | "custom_content": content}) | |
243 |
|
243 | |||
244 |
|
244 | |||
245 | def on_msg(self, callback, remove=False): |
|
245 | def on_msg(self, callback, remove=False): | |
246 | """Register a callback for when a custom msg is recieved from the front-end |
|
246 | """Register a callback for when a custom msg is recieved from the front-end | |
247 |
|
247 | |||
248 | Parameters |
|
248 | Parameters | |
249 | ---------- |
|
249 | ---------- | |
250 | callback: method handler |
|
250 | callback: method handler | |
251 | Can have a signature of: |
|
251 | Can have a signature of: | |
252 | - callback(content) |
|
252 | - callback(content) | |
253 | - callback(sender, content) |
|
253 | - callback(sender, content) | |
254 | remove: bool |
|
254 | remove: bool | |
255 | True if the callback should be unregistered.""" |
|
255 | True if the callback should be unregistered.""" | |
256 | if remove and callback in self._msg_callbacks: |
|
256 | if remove and callback in self._msg_callbacks: | |
257 | self._msg_callbacks.remove(callback) |
|
257 | self._msg_callbacks.remove(callback) | |
258 | elif not remove and not callback in self._msg_callbacks: |
|
258 | elif not remove and not callback in self._msg_callbacks: | |
259 | self._msg_callbacks.append(callback) |
|
259 | self._msg_callbacks.append(callback) | |
260 |
|
260 | |||
261 |
|
261 | |||
262 | def on_displayed(self, callback, remove=False): |
|
262 | def on_displayed(self, callback, remove=False): | |
263 | """Register a callback to be called when the widget has been displayed |
|
263 | """Register a callback to be called when the widget has been displayed | |
264 |
|
264 | |||
265 | Parameters |
|
265 | Parameters | |
266 | ---------- |
|
266 | ---------- | |
267 | callback: method handler |
|
267 | callback: method handler | |
268 | Can have a signature of: |
|
268 | Can have a signature of: | |
269 | - callback() |
|
269 | - callback() | |
270 | - callback(sender) |
|
270 | - callback(sender) | |
271 | - callback(sender, view_name) |
|
271 | - callback(sender, view_name) | |
272 | - callback(sender, **kwargs) |
|
272 | - callback(sender, **kwargs) | |
273 | kwargs from display call passed through without modification. |
|
273 | kwargs from display call passed through without modification. | |
274 | remove: bool |
|
274 | remove: bool | |
275 | True if the callback should be unregistered.""" |
|
275 | True if the callback should be unregistered.""" | |
276 | if remove and callback in self._display_callbacks: |
|
276 | if remove and callback in self._display_callbacks: | |
277 | self._display_callbacks.remove(callback) |
|
277 | self._display_callbacks.remove(callback) | |
278 | elif not remove and not callback in self._display_callbacks: |
|
278 | elif not remove and not callback in self._display_callbacks: | |
279 | self._display_callbacks.append(callback) |
|
279 | self._display_callbacks.append(callback) | |
280 |
|
280 | |||
281 |
|
281 | |||
282 | # Support methods |
|
282 | # Support methods | |
283 | def _repr_widget_(self, **kwargs): |
|
283 | def _repr_widget_(self, **kwargs): | |
284 | """Function that is called when `IPython.display.display` is called on |
|
284 | """Function that is called when `IPython.display.display` is called on | |
285 | the widget. |
|
285 | the widget. | |
286 |
|
286 | |||
287 | Parameters |
|
287 | Parameters | |
288 | ---------- |
|
288 | ---------- | |
289 | view_name: unicode (optional) |
|
289 | view_name: unicode (optional) | |
290 | View to display in the frontend. Overrides default_view_name.""" |
|
290 | View to display in the frontend. Overrides default_view_name.""" | |
291 | view_name = kwargs.get('view_name', self.default_view_name) |
|
291 | view_name = kwargs.get('view_name', self.default_view_name) | |
292 |
|
292 | |||
293 | # Create a communication. |
|
293 | # Create a communication. | |
294 | self._open_communication() |
|
294 | self._open_communication() | |
295 |
|
295 | |||
296 | # Make sure model is syncronized |
|
296 | # Make sure model is syncronized | |
297 | self.send_state() |
|
297 | self.send_state() | |
298 |
|
298 | |||
299 | # Show view. |
|
299 | # Show view. | |
300 | self._send({"method": "display", "view_name": view_name}) |
|
300 | self._send({"method": "display", "view_name": view_name}) | |
301 | self._displayed = True |
|
301 | self._displayed = True | |
302 | self._handle_displayed(**kwargs) |
|
302 | self._handle_displayed(**kwargs) | |
303 |
|
303 | |||
304 |
|
304 | |||
305 | def _open_communication(self): |
|
305 | def _open_communication(self): | |
306 | """Opens a communication with the front-end.""" |
|
306 | """Opens a communication with the front-end.""" | |
307 | # Create a comm. |
|
307 | # Create a comm. | |
308 | if self._comm is None: |
|
308 | if self._comm is None: | |
309 | self._comm = Comm(target_name=self.target_name) |
|
309 | self._comm = Comm(target_name=self.target_name) | |
310 | self._comm.on_msg(self._handle_msg) |
|
310 | self._comm.on_msg(self._handle_msg) | |
311 | self._comm.on_close(self._close_communication) |
|
311 | self._comm.on_close(self._close_communication) | |
312 |
|
312 | |||
313 | # first update |
|
313 | # first update | |
314 | self.send_state() |
|
314 | self.send_state() | |
315 |
|
315 | |||
316 |
|
316 | |||
317 | def _close_communication(self): |
|
317 | def _close_communication(self): | |
318 | """Closes a communication with the front-end.""" |
|
318 | """Closes a communication with the front-end.""" | |
319 | if self._comm is not None: |
|
319 | if self._comm is not None: | |
320 | try: |
|
320 | try: | |
321 | self._comm.close() |
|
321 | self._comm.close() | |
322 | finally: |
|
322 | finally: | |
323 | self._comm = None |
|
323 | self._comm = None | |
324 |
|
324 | |||
325 |
|
325 | |||
326 | def _send(self, msg): |
|
326 | def _send(self, msg): | |
327 | """Sends a message to the model in the front-end""" |
|
327 | """Sends a message to the model in the front-end""" | |
328 | if self._comm is not None: |
|
328 | if self._comm is not None: | |
329 | self._comm.send(msg) |
|
329 | self._comm.send(msg) | |
330 | return True |
|
330 | return True | |
331 | else: |
|
331 | else: | |
332 | return False |
|
332 | return False | |
333 |
|
333 | |||
334 |
|
334 | |||
335 | class ViewWidget(BaseWidget): |
|
335 | class ViewWidget(BaseWidget): | |
336 | target_name = Unicode('ViewModel') |
|
336 | target_name = Unicode('ViewModel') | |
337 |
|
337 | |||
338 | def __init__(self, widget, view): |
|
338 | def __init__(self, widget, view): | |
339 | self.default_view_name = view |
|
339 | self.default_view_name = view | |
340 | self.widget = widget |
|
340 | self.widget = widget | |
341 |
|
341 | |||
342 |
|
342 | |||
343 | class Widget(BaseWidget): |
|
343 | class Widget(BaseWidget): | |
344 | visible = Bool(True, help="Whether or not the widget is visible.") |
|
344 | visible = Bool(True, help="Whether or not the widget is visible.") | |
345 |
|
345 | |||
346 | # Private/protected declarations |
|
346 | # Private/protected declarations | |
347 | _css = Dict() # Internal CSS property dict |
|
347 | _css = Dict() # Internal CSS property dict | |
348 |
|
348 | |||
349 | # Properties |
|
349 | # Properties | |
350 | @property |
|
350 | @property | |
351 | def keys(self): |
|
351 | def keys(self): | |
352 | keys = ['visible', '_css'] |
|
352 | keys = ['visible', '_css'] | |
353 | keys.extend(super(Widget, self).keys) |
|
353 | keys.extend(super(Widget, self).keys) | |
354 | return keys |
|
354 | return keys | |
355 |
|
355 | |||
356 | def get_css(self, key, selector=""): |
|
356 | def get_css(self, key, selector=""): | |
357 | """Get a CSS property of the widget. Note, this function does not |
|
357 | """Get a CSS property of the widget. Note, this function does not | |
358 | actually request the CSS from the front-end; Only properties that have |
|
358 | actually request the CSS from the front-end; Only properties that have | |
359 | been set with set_css can be read. |
|
359 | been set with set_css can be read. | |
360 |
|
360 | |||
361 | Parameters |
|
361 | Parameters | |
362 | ---------- |
|
362 | ---------- | |
363 | key: unicode |
|
363 | key: unicode | |
364 | CSS key |
|
364 | CSS key | |
365 | selector: unicode (optional) |
|
365 | selector: unicode (optional) | |
366 | JQuery selector used when the CSS key/value was set. |
|
366 | JQuery selector used when the CSS key/value was set. | |
367 | """ |
|
367 | """ | |
368 | if selector in self._css and key in self._css[selector]: |
|
368 | if selector in self._css and key in self._css[selector]: | |
369 | return self._css[selector][key] |
|
369 | return self._css[selector][key] | |
370 | else: |
|
370 | else: | |
371 | return None |
|
371 | return None | |
372 |
|
372 | |||
373 |
|
373 | |||
374 | def set_css(self, *args, **kwargs): |
|
374 | def set_css(self, *args, **kwargs): | |
375 | """Set one or more CSS properties of the widget (shared among all of the |
|
375 | """Set one or more CSS properties of the widget (shared among all of the | |
376 | views). This function has two signatures: |
|
376 | views). This function has two signatures: | |
377 | - set_css(css_dict, [selector='']) |
|
377 | - set_css(css_dict, [selector='']) | |
378 | - set_css(key, value, [selector='']) |
|
378 | - set_css(key, value, [selector='']) | |
379 |
|
379 | |||
380 | Parameters |
|
380 | Parameters | |
381 | ---------- |
|
381 | ---------- | |
382 | css_dict : dict |
|
382 | css_dict : dict | |
383 | CSS key/value pairs to apply |
|
383 | CSS key/value pairs to apply | |
384 | key: unicode |
|
384 | key: unicode | |
385 | CSS key |
|
385 | CSS key | |
386 | value |
|
386 | value | |
387 | CSS value |
|
387 | CSS value | |
388 | selector: unicode (optional) |
|
388 | selector: unicode (optional) | |
389 | JQuery selector to use to apply the CSS key/value. |
|
389 | JQuery selector to use to apply the CSS key/value. | |
390 | """ |
|
390 | """ | |
391 | selector = kwargs.get('selector', '') |
|
391 | selector = kwargs.get('selector', '') | |
392 |
|
392 | |||
393 | # Signature 1: set_css(css_dict, [selector='']) |
|
393 | # Signature 1: set_css(css_dict, [selector='']) | |
394 | if len(args) == 1: |
|
394 | if len(args) == 1: | |
395 | if isinstance(args[0], dict): |
|
395 | if isinstance(args[0], dict): | |
396 | for (key, value) in args[0].items(): |
|
396 | for (key, value) in args[0].items(): | |
397 | self.set_css(key, value, selector=selector) |
|
397 | self.set_css(key, value, selector=selector) | |
398 | else: |
|
398 | else: | |
399 | raise Exception('css_dict must be a dict.') |
|
399 | raise Exception('css_dict must be a dict.') | |
400 |
|
400 | |||
401 | # Signature 2: set_css(key, value, [selector='']) |
|
401 | # Signature 2: set_css(key, value, [selector='']) | |
402 | elif len(args) == 2 or len(args) == 3: |
|
402 | elif len(args) == 2 or len(args) == 3: | |
403 |
|
403 | |||
404 | # Selector can be a positional arg if it's the 3rd value |
|
404 | # Selector can be a positional arg if it's the 3rd value | |
405 | if len(args) == 3: |
|
405 | if len(args) == 3: | |
406 | selector = args[2] |
|
406 | selector = args[2] | |
407 | if selector not in self._css: |
|
407 | if selector not in self._css: | |
408 | self._css[selector] = {} |
|
408 | self._css[selector] = {} | |
409 |
|
409 | |||
410 | # Only update the property if it has changed. |
|
410 | # Only update the property if it has changed. | |
411 | key = args[0] |
|
411 | key = args[0] | |
412 | value = args[1] |
|
412 | value = args[1] | |
413 | if not (key in self._css[selector] and value in self._css[selector][key]): |
|
413 | if not (key in self._css[selector] and value in self._css[selector][key]): | |
414 | self._css[selector][key] = value |
|
414 | self._css[selector][key] = value | |
415 | self.send_state('_css') # Send new state to client. |
|
415 | self.send_state('_css') # Send new state to client. | |
416 | else: |
|
416 | else: | |
417 | raise Exception('set_css only accepts 1-3 arguments') |
|
417 | raise Exception('set_css only accepts 1-3 arguments') | |
418 |
|
418 | |||
419 |
|
419 | |||
420 | def add_class(self, class_name, selector=""): |
|
420 | def add_class(self, class_name, selector=""): | |
421 | """Add class[es] to a DOM element |
|
421 | """Add class[es] to a DOM element | |
422 |
|
422 | |||
423 | Parameters |
|
423 | Parameters | |
424 | ---------- |
|
424 | ---------- | |
425 | class_name: unicode |
|
425 | class_name: unicode | |
426 | Class name(s) to add to the DOM element(s). Multiple class names |
|
426 | Class name(s) to add to the DOM element(s). Multiple class names | |
427 | must be space separated. |
|
427 | must be space separated. | |
428 | selector: unicode (optional) |
|
428 | selector: unicode (optional) | |
429 | JQuery selector to select the DOM element(s) that the class(es) will |
|
429 | JQuery selector to select the DOM element(s) that the class(es) will | |
430 | be added to. |
|
430 | be added to. | |
431 | """ |
|
431 | """ | |
432 | self.send({"msg_type": "add_class", |
|
432 | self.send({"msg_type": "add_class", | |
433 | "class_list": class_name, |
|
433 | "class_list": class_name, | |
434 | "selector": selector}) |
|
434 | "selector": selector}) | |
435 |
|
435 | |||
436 |
|
436 | |||
437 | def remove_class(self, class_name, selector=""): |
|
437 | def remove_class(self, class_name, selector=""): | |
438 | """Remove class[es] from a DOM element |
|
438 | """Remove class[es] from a DOM element | |
439 |
|
439 | |||
440 | Parameters |
|
440 | Parameters | |
441 | ---------- |
|
441 | ---------- | |
442 | class_name: unicode |
|
442 | class_name: unicode | |
443 | Class name(s) to remove from the DOM element(s). Multiple class |
|
443 | Class name(s) to remove from the DOM element(s). Multiple class | |
444 | names must be space separated. |
|
444 | names must be space separated. | |
445 | selector: unicode (optional) |
|
445 | selector: unicode (optional) | |
446 | JQuery selector to select the DOM element(s) that the class(es) will |
|
446 | JQuery selector to select the DOM element(s) that the class(es) will | |
447 | be removed from. |
|
447 | be removed from. | |
448 | """ |
|
448 | """ | |
449 | self.send({"msg_type": "remove_class", |
|
449 | self.send({"msg_type": "remove_class", | |
450 | "class_list": class_name, |
|
450 | "class_list": class_name, | |
451 | "selector": selector}) |
|
451 | "selector": selector}) | |
452 |
|
452 | |||
453 |
|
453 | |||
454 | def view(self, view_name=None): |
|
454 | def view(self, view_name=None): | |
455 | """Return a widget that can be displayed to display this widget using |
|
455 | """Return a widget that can be displayed to display this widget using | |
456 | a non-default view""" |
|
456 | a non-default view""" | |
457 | return ViewWidget(self, view_name) |
|
457 | return ViewWidget(self, view_name) |
General Comments 0
You need to be logged in to leave comments.
Login now