Show More
@@ -93,6 +93,7 b' class Widget(LoggingConfigurable):' | |||||
93 | self._children = [] |
|
93 | self._children = [] | |
94 | self._add_class = [0] |
|
94 | self._add_class = [0] | |
95 | self._remove_class = [0] |
|
95 | self._remove_class = [0] | |
|
96 | self._display_callbacks = [] | |||
96 | super(Widget, self).__init__(**kwargs) |
|
97 | super(Widget, self).__init__(**kwargs) | |
97 |
|
98 | |||
98 | # Register after init to allow default values to be specified |
|
99 | # Register after init to allow default values to be specified | |
@@ -278,6 +279,49 b' class Widget(LoggingConfigurable):' | |||||
278 | self.send_state(key='_remove_class') |
|
279 | self.send_state(key='_remove_class') | |
279 |
|
280 | |||
280 |
|
281 | |||
|
282 | def on_displayed(self, callback, remove=False): | |||
|
283 | """Register a callback to be called when the widget has been displayed | |||
|
284 | ||||
|
285 | callback: method handler | |||
|
286 | Can have a signature of: | |||
|
287 | - callback() | |||
|
288 | - callback(sender) | |||
|
289 | - callback(sender, view_name) | |||
|
290 | remove: bool | |||
|
291 | True if the callback should be unregistered.""" | |||
|
292 | if remove: | |||
|
293 | self._display_callbacks.remove(callback) | |||
|
294 | elif not callback in self._display_callbacks: | |||
|
295 | self._display_callbacks.append(callback) | |||
|
296 | ||||
|
297 | ||||
|
298 | def handle_displayed(self, view_name): | |||
|
299 | """Called when a view has been displayed for this widget instance | |||
|
300 | ||||
|
301 | view_name: unicode | |||
|
302 | Name of the view that was displayed.""" | |||
|
303 | for handler in self._display_callbacks: | |||
|
304 | if callable(handler): | |||
|
305 | argspec = inspect.getargspec(handler) | |||
|
306 | nargs = len(argspec[0]) | |||
|
307 | ||||
|
308 | # Bound methods have an additional 'self' argument | |||
|
309 | if isinstance(handler, types.MethodType): | |||
|
310 | nargs -= 1 | |||
|
311 | ||||
|
312 | # Call the callback | |||
|
313 | if nargs == 0: | |||
|
314 | handler() | |||
|
315 | elif nargs == 1: | |||
|
316 | handler(self) | |||
|
317 | elif nargs == 2: | |||
|
318 | handler(self, view_name) | |||
|
319 | else: | |||
|
320 | raise TypeError('Widget display callback must ' \ | |||
|
321 | 'accept 0-2 arguments, not %d.' % nargs) | |||
|
322 | ||||
|
323 | ||||
|
324 | ||||
281 | # Support methods |
|
325 | # Support methods | |
282 | def _repr_widget_(self, view_name=None): |
|
326 | def _repr_widget_(self, view_name=None): | |
283 | """Function that is called when `IPython.display.display` is called on |
|
327 | """Function that is called when `IPython.display.display` is called on | |
@@ -308,6 +352,7 b' class Widget(LoggingConfigurable):' | |||||
308 | "view_name": view_name, |
|
352 | "view_name": view_name, | |
309 | "parent": self.parent._comm.comm_id}) |
|
353 | "parent": self.parent._comm.comm_id}) | |
310 | self._displayed = True |
|
354 | self._displayed = True | |
|
355 | self.handle_displayed(view_name) | |||
311 |
|
356 | |||
312 | # Now display children if any. |
|
357 | # Now display children if any. | |
313 | for child in self._children: |
|
358 | for child in self._children: |
General Comments 0
You need to be logged in to leave comments.
Login now