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