Show More
@@ -31,8 +31,9 b' class Widget(LoggingConfigurable):' | |||||
31 | widgets = {} |
|
31 | widgets = {} | |
32 |
|
32 | |||
33 | def on_widget_constructed(callback): |
|
33 | def on_widget_constructed(callback): | |
34 |
""" |
|
34 | """Registers a callback to be called when a widget is constructed. | |
35 | constructed. The callback must have the following signature: |
|
35 | ||
|
36 | The callback must have the following signature: | |||
36 | callback(widget)""" |
|
37 | callback(widget)""" | |
37 | Widget.widget_construction_callback = callback |
|
38 | Widget.widget_construction_callback = callback | |
38 |
|
39 | |||
@@ -41,8 +42,6 b' class Widget(LoggingConfigurable):' | |||||
41 | if Widget.widget_construction_callback is not None and callable(Widget.widget_construction_callback): |
|
42 | if Widget.widget_construction_callback is not None and callable(Widget.widget_construction_callback): | |
42 | Widget.widget_construction_callback(widget) |
|
43 | Widget.widget_construction_callback(widget) | |
43 |
|
44 | |||
44 |
|
||||
45 |
|
||||
46 | # Public declarations (Instance level) |
|
45 | # Public declarations (Instance level) | |
47 | model_name = Unicode('WidgetModel', help="""Name of the backbone model |
|
46 | model_name = Unicode('WidgetModel', help="""Name of the backbone model | |
48 | registered in the front-end to create and sync this widget with.""") |
|
47 | registered in the front-end to create and sync this widget with.""") | |
@@ -80,8 +79,7 b' class Widget(LoggingConfigurable):' | |||||
80 | _comm = Instance('IPython.kernel.comm.Comm') |
|
79 | _comm = Instance('IPython.kernel.comm.Comm') | |
81 |
|
80 | |||
82 | def __init__(self, **kwargs): |
|
81 | def __init__(self, **kwargs): | |
83 | """Public constructor |
|
82 | """Public constructor""" | |
84 | """ |
|
|||
85 | self.closed = False |
|
83 | self.closed = False | |
86 | self._property_lock = (None, None) |
|
84 | self._property_lock = (None, None) | |
87 | self._display_callbacks = [] |
|
85 | self._display_callbacks = [] | |
@@ -97,21 +95,21 b' class Widget(LoggingConfigurable):' | |||||
97 | self.close() |
|
95 | self.close() | |
98 |
|
96 | |||
99 | def close(self): |
|
97 | def close(self): | |
100 | """Close method. Closes the widget which closes the underlying comm. |
|
98 | """Close method. | |
|
99 | ||||
|
100 | Closes the widget which closes the underlying comm. | |||
101 | When the comm is closed, all of the widget views are automatically |
|
101 | When the comm is closed, all of the widget views are automatically | |
102 | removed from the front-end.""" |
|
102 | removed from the front-end.""" | |
103 | if not self.closed: |
|
103 | if not self.closed: | |
104 | self._comm.close() |
|
104 | self._comm.close() | |
105 | self._close() |
|
105 | self._close() | |
106 |
|
106 | |||
107 |
|
||||
108 | def _close(self): |
|
107 | def _close(self): | |
109 | """Unsafe close""" |
|
108 | """Unsafe close""" | |
110 | del Widget.widgets[self.model_id] |
|
109 | del Widget.widgets[self.model_id] | |
111 | self._comm = None |
|
110 | self._comm = None | |
112 | self.closed = True |
|
111 | self.closed = True | |
113 |
|
112 | |||
114 |
|
||||
115 | @property |
|
113 | @property | |
116 | def comm(self): |
|
114 | def comm(self): | |
117 | if self._comm is None: |
|
115 | if self._comm is None: | |
@@ -147,7 +145,6 b' class Widget(LoggingConfigurable):' | |||||
147 | if 'custom_content' in data: |
|
145 | if 'custom_content' in data: | |
148 | self._handle_custom_msg(data['custom_content']) |
|
146 | self._handle_custom_msg(data['custom_content']) | |
149 |
|
147 | |||
150 |
|
||||
151 | def _handle_receive_state(self, sync_data): |
|
148 | def _handle_receive_state(self, sync_data): | |
152 | """Called when a state is received from the front-end.""" |
|
149 | """Called when a state is received from the front-end.""" | |
153 | for name in self.keys: |
|
150 | for name in self.keys: | |
@@ -156,13 +153,11 b' class Widget(LoggingConfigurable):' | |||||
156 | with self.property_lock(name, value): |
|
153 | with self.property_lock(name, value): | |
157 | setattr(self, name, value) |
|
154 | setattr(self, name, value) | |
158 |
|
155 | |||
159 |
|
||||
160 | def _handle_custom_msg(self, content): |
|
156 | def _handle_custom_msg(self, content): | |
161 | """Called when a custom msg is received.""" |
|
157 | """Called when a custom msg is received.""" | |
162 | for handler in self._msg_callbacks: |
|
158 | for handler in self._msg_callbacks: | |
163 | handler(self, content) |
|
159 | handler(self, content) | |
164 |
|
160 | |||
165 |
|
||||
166 | def _handle_property_changed(self, name, old, new): |
|
161 | def _handle_property_changed(self, name, old, new): | |
167 | """Called when a property has been changed.""" |
|
162 | """Called when a property has been changed.""" | |
168 | # Make sure this isn't information that the front-end just sent us. |
|
163 | # Make sure this isn't information that the front-end just sent us. | |
@@ -198,10 +193,8 b' class Widget(LoggingConfigurable):' | |||||
198 | keys = self.keys if key is None else [key] |
|
193 | keys = self.keys if key is None else [key] | |
199 | return {k: self._pack_widgets(getattr(self, k)) for k in keys} |
|
194 | return {k: self._pack_widgets(getattr(self, k)) for k in keys} | |
200 |
|
195 | |||
201 |
|
||||
202 | def _pack_widgets(self, values): |
|
196 | def _pack_widgets(self, values): | |
203 |
""" |
|
197 | """Recursively converts all widget instances to model id strings. | |
204 | strings. |
|
|||
205 |
|
198 | |||
206 | Children widgets will be stored and transmitted to the front-end by |
|
199 | Children widgets will be stored and transmitted to the front-end by | |
207 | their model ids.""" |
|
200 | their model ids.""" | |
@@ -220,10 +213,8 b' class Widget(LoggingConfigurable):' | |||||
220 | else: |
|
213 | else: | |
221 | return values |
|
214 | return values | |
222 |
|
215 | |||
223 |
|
||||
224 | def _unpack_widgets(self, values): |
|
216 | def _unpack_widgets(self, values): | |
225 |
""" |
|
217 | """Recursively converts all model id strings to widget instances. | |
226 | instances. |
|
|||
227 |
|
218 | |||
228 | Children widgets will be stored and transmitted to the front-end by |
|
219 | Children widgets will be stored and transmitted to the front-end by | |
229 | their model ids.""" |
|
220 | their model ids.""" | |
@@ -245,7 +236,6 b' class Widget(LoggingConfigurable):' | |||||
245 | else: |
|
236 | else: | |
246 | return values |
|
237 | return values | |
247 |
|
238 | |||
248 |
|
||||
249 | def send(self, content): |
|
239 | def send(self, content): | |
250 | """Sends a custom msg to the widget model in the front-end. |
|
240 | """Sends a custom msg to the widget model in the front-end. | |
251 |
|
241 | |||
@@ -256,10 +246,8 b' class Widget(LoggingConfigurable):' | |||||
256 | """ |
|
246 | """ | |
257 | self._send({"method": "custom", "custom_content": content}) |
|
247 | self._send({"method": "custom", "custom_content": content}) | |
258 |
|
248 | |||
259 |
|
||||
260 | def on_msg(self, callback, remove=False): |
|
249 | def on_msg(self, callback, remove=False): | |
261 |
""" |
|
250 | """(Un)Register a custom msg recieve callback. | |
262 | from the front-end. |
|
|||
263 |
|
251 | |||
264 | Parameters |
|
252 | Parameters | |
265 | ---------- |
|
253 | ---------- | |
@@ -291,10 +279,8 b' class Widget(LoggingConfigurable):' | |||||
291 | else: |
|
279 | else: | |
292 | raise Exception('Callback must be callable.') |
|
280 | raise Exception('Callback must be callable.') | |
293 |
|
281 | |||
294 |
|
||||
295 | def on_displayed(self, callback, remove=False): |
|
282 | def on_displayed(self, callback, remove=False): | |
296 | """Register or unregister a callback to be called when the widget has |
|
283 | """(Un)Register a widget displayed callback. | |
297 | been displayed. |
|
|||
298 |
|
284 | |||
299 | Parameters |
|
285 | Parameters | |
300 | ---------- |
|
286 | ---------- | |
@@ -312,20 +298,16 b' class Widget(LoggingConfigurable):' | |||||
312 | else: |
|
298 | else: | |
313 | raise Exception('Callback must be callable.') |
|
299 | raise Exception('Callback must be callable.') | |
314 |
|
300 | |||
315 |
|
||||
316 | # Support methods |
|
301 | # Support methods | |
317 | def _ipython_display_(self, **kwargs): |
|
302 | def _ipython_display_(self, **kwargs): | |
318 |
""" |
|
303 | """Called when `IPython.display.display` is called on the widget.""" | |
319 | the widget.""" |
|
|||
320 |
|
||||
321 | # Show view. By sending a display message, the comm is opened and the |
|
304 | # Show view. By sending a display message, the comm is opened and the | |
322 | # initial state is sent. |
|
305 | # initial state is sent. | |
323 | self._send({"method": "display"}) |
|
306 | self._send({"method": "display"}) | |
324 | self._handle_displayed(**kwargs) |
|
307 | self._handle_displayed(**kwargs) | |
325 |
|
308 | |||
326 |
|
||||
327 | def _send(self, msg): |
|
309 | def _send(self, msg): | |
328 | """Sends a message to the model in the front-end""" |
|
310 | """Sends a message to the model in the front-end.""" | |
329 | self.comm.send(msg) |
|
311 | self.comm.send(msg) | |
330 |
|
312 | |||
331 |
|
313 | |||
@@ -408,9 +390,8 b' class DOMWidget(Widget):' | |||||
408 | else: |
|
390 | else: | |
409 | raise Exception('set_css only accepts 1-3 arguments') |
|
391 | raise Exception('set_css only accepts 1-3 arguments') | |
410 |
|
392 | |||
411 |
|
||||
412 | def add_class(self, class_names, selector=""): |
|
393 | def add_class(self, class_names, selector=""): | |
413 | """Add class[es] to a DOM element |
|
394 | """Add class[es] to a DOM element. | |
414 |
|
395 | |||
415 | Parameters |
|
396 | Parameters | |
416 | ---------- |
|
397 | ---------- | |
@@ -428,9 +409,8 b' class DOMWidget(Widget):' | |||||
428 | "class_list": class_list, |
|
409 | "class_list": class_list, | |
429 | "selector": selector}) |
|
410 | "selector": selector}) | |
430 |
|
411 | |||
431 |
|
||||
432 | def remove_class(self, class_names, selector=""): |
|
412 | def remove_class(self, class_names, selector=""): | |
433 | """Remove class[es] from a DOM element |
|
413 | """Remove class[es] from a DOM element. | |
434 |
|
414 | |||
435 | Parameters |
|
415 | Parameters | |
436 | ---------- |
|
416 | ---------- |
@@ -27,6 +27,7 b' class CheckBoxWidget(DOMWidget):' | |||||
27 | description = Unicode('', help="Description of the boolean (label).", sync=True) |
|
27 | description = Unicode('', help="Description of the boolean (label).", sync=True) | |
28 | disabled = Bool(False, help="Enable or disable user changes.", sync=True) |
|
28 | disabled = Bool(False, help="Enable or disable user changes.", sync=True) | |
29 |
|
29 | |||
|
30 | ||||
30 | class ToggleButtonWidget(CheckBoxWidget): |
|
31 | class ToggleButtonWidget(CheckBoxWidget): | |
31 | view_name = Unicode('ToggleButtonView', sync=True) |
|
32 | view_name = Unicode('ToggleButtonView', sync=True) | |
32 | No newline at end of file |
|
33 |
@@ -30,17 +30,17 b' class ButtonWidget(DOMWidget):' | |||||
30 | description = Unicode('', help="Description of the button (label).", sync=True) |
|
30 | description = Unicode('', help="Description of the button (label).", sync=True) | |
31 | disabled = Bool(False, help="Enable or disable user changes.", sync=True) |
|
31 | disabled = Bool(False, help="Enable or disable user changes.", sync=True) | |
32 |
|
32 | |||
33 |
|
||||
34 | def __init__(self, **kwargs): |
|
33 | def __init__(self, **kwargs): | |
|
34 | """Constructor""" | |||
35 | super(ButtonWidget, self).__init__(**kwargs) |
|
35 | super(ButtonWidget, self).__init__(**kwargs) | |
36 |
|
36 | |||
37 | self._click_handlers = [] |
|
37 | self._click_handlers = [] | |
38 | self.on_msg(self._handle_button_msg) |
|
38 | self.on_msg(self._handle_button_msg) | |
39 |
|
39 | |||
40 |
|
||||
41 | def on_click(self, callback, remove=False): |
|
40 | def on_click(self, callback, remove=False): | |
42 |
"""Register a callback to execute when the button is clicked. |
|
41 | """Register a callback to execute when the button is clicked. | |
43 | callback can either accept no parameters or one sender parameter: |
|
42 | ||
|
43 | The callback can either accept no parameters or one sender parameter: | |||
44 | - callback() |
|
44 | - callback() | |
45 | - callback(sender) |
|
45 | - callback(sender) | |
46 | If the callback has a sender parameter, the ButtonWidget instance that |
|
46 | If the callback has a sender parameter, the ButtonWidget instance that | |
@@ -55,9 +55,8 b' class ButtonWidget(DOMWidget):' | |||||
55 | elif not callback in self._click_handlers: |
|
55 | elif not callback in self._click_handlers: | |
56 | self._click_handlers.append(callback) |
|
56 | self._click_handlers.append(callback) | |
57 |
|
57 | |||
58 |
|
||||
59 | def _handle_button_msg(self, content): |
|
58 | def _handle_button_msg(self, content): | |
60 | """Handle a msg from the front-end |
|
59 | """Handle a msg from the front-end. | |
61 |
|
60 | |||
62 | Parameters |
|
61 | Parameters | |
63 | ---------- |
|
62 | ---------- | |
@@ -66,11 +65,10 b' class ButtonWidget(DOMWidget):' | |||||
66 | if 'event' in content and content['event'] == 'click': |
|
65 | if 'event' in content and content['event'] == 'click': | |
67 | self._handle_click() |
|
66 | self._handle_click() | |
68 |
|
67 | |||
69 |
|
||||
70 | def _handle_click(self): |
|
68 | def _handle_click(self): | |
71 |
"""Handles when the button has been clicked. |
|
69 | """Handles when the button has been clicked. | |
72 | callbacks when appropriate.""" |
|
70 | ||
73 |
|
71 | Fires on_click callbacks when appropriate.""" | ||
74 | for handler in self._click_handlers: |
|
72 | for handler in self._click_handlers: | |
75 | if callable(handler): |
|
73 | if callable(handler): | |
76 | argspec = inspect.getargspec(handler) |
|
74 | argspec = inspect.getargspec(handler) | |
@@ -88,4 +86,3 b' class ButtonWidget(DOMWidget):' | |||||
88 | else: |
|
86 | else: | |
89 | raise TypeError('ButtonWidget click callback must ' \ |
|
87 | raise TypeError('ButtonWidget click callback must ' \ | |
90 | 'accept 0 or 1 arguments.') |
|
88 | 'accept 0 or 1 arguments.') | |
91 |
|
@@ -29,5 +29,6 b' class ContainerWidget(DOMWidget):' | |||||
29 | description = Unicode(sync=True) |
|
29 | description = Unicode(sync=True) | |
30 | button_text = Unicode(sync=True) |
|
30 | button_text = Unicode(sync=True) | |
31 |
|
31 | |||
|
32 | ||||
32 | class ModalWidget(ContainerWidget): |
|
33 | class ModalWidget(ContainerWidget): | |
33 | view_name = Unicode('ModalView', sync=True) |
|
34 | view_name = Unicode('ModalView', sync=True) |
@@ -34,7 +34,7 b' class BoundedFloatTextWidget(DOMWidget):' | |||||
34 | self.on_trait_change(self._validate, ['value', 'min', 'max']) |
|
34 | self.on_trait_change(self._validate, ['value', 'min', 'max']) | |
35 |
|
35 | |||
36 | def _validate(self, name, old, new): |
|
36 | def _validate(self, name, old, new): | |
37 | """Validate value, max, min""" |
|
37 | """Validate value, max, min.""" | |
38 | if self.min > new or new > self.max: |
|
38 | if self.min > new or new > self.max: | |
39 | self.value = min(max(new, self.min), self.max) |
|
39 | self.value = min(max(new, self.min), self.max) | |
40 |
|
40 |
@@ -36,7 +36,7 b' class BoundedIntTextWidget(DOMWidget):' | |||||
36 | self.on_trait_change(self._validate, ['value', 'min', 'max']) |
|
36 | self.on_trait_change(self._validate, ['value', 'min', 'max']) | |
37 |
|
37 | |||
38 | def _validate(self, name, old, new): |
|
38 | def _validate(self, name, old, new): | |
39 | """Validate value, max, min""" |
|
39 | """Validate value, max, min.""" | |
40 | if self.min > new or new > self.max: |
|
40 | if self.min > new or new > self.max: | |
41 | self.value = min(max(new, self.min), self.max) |
|
41 | self.value = min(max(new, self.min), self.max) | |
42 |
|
42 |
@@ -31,7 +31,7 b' class AccordionWidget(DOMWidget):' | |||||
31 |
|
31 | |||
32 | # Public methods |
|
32 | # Public methods | |
33 | def set_title(self, index, title): |
|
33 | def set_title(self, index, title): | |
34 | """Sets the title of a container page |
|
34 | """Sets the title of a container page. | |
35 |
|
35 | |||
36 | Parameters |
|
36 | Parameters | |
37 | ---------- |
|
37 | ---------- | |
@@ -42,9 +42,8 b' class AccordionWidget(DOMWidget):' | |||||
42 | self._titles[index] = title |
|
42 | self._titles[index] = title | |
43 | self.send_state('_titles') |
|
43 | self.send_state('_titles') | |
44 |
|
44 | |||
45 |
|
||||
46 | def get_title(self, index): |
|
45 | def get_title(self, index): | |
47 | """Gets the title of a container pages |
|
46 | """Gets the title of a container pages. | |
48 |
|
47 | |||
49 | Parameters |
|
48 | Parameters | |
50 | ---------- |
|
49 | ---------- |
@@ -51,7 +51,7 b' class TextBoxWidget(HTMLWidget):' | |||||
51 | self.on_msg(self._handle_string_msg) |
|
51 | self.on_msg(self._handle_string_msg) | |
52 |
|
52 | |||
53 | def _handle_string_msg(self, content): |
|
53 | def _handle_string_msg(self, content): | |
54 | """Handle a msg from the front-end |
|
54 | """Handle a msg from the front-end. | |
55 |
|
55 | |||
56 | Parameters |
|
56 | Parameters | |
57 | ---------- |
|
57 | ---------- | |
@@ -62,8 +62,9 b' class TextBoxWidget(HTMLWidget):' | |||||
62 | handler(self) |
|
62 | handler(self) | |
63 |
|
63 | |||
64 | def on_submit(self, callback, remove=False): |
|
64 | def on_submit(self, callback, remove=False): | |
65 |
"""Register a callback to handle text submission |
|
65 | """(Un)Register a callback to handle text submission. | |
66 | user clicks enter). |
|
66 | ||
|
67 | Triggered when the user clicks enter. | |||
67 |
|
68 | |||
68 | Parameters |
|
69 | Parameters | |
69 | callback: Method handle |
|
70 | callback: Method handle |
General Comments 0
You need to be logged in to leave comments.
Login now