Display handler now supports full kwargs

This commit is contained in:
Jonathan Frederic 2013-12-30 10:43:17 -08:00 committed by Jonathan Frederic
parent 3361826b5a
commit 2df05bc5b8

View File

@ -180,15 +180,13 @@ class Widget(LoggingConfigurable):
self.send_state(key=name) self.send_state(key=name)
def _handle_displayed(self, view_name, parent=None): def _handle_displayed(self, **kwargs):
"""Called when a view has been displayed for this widget instance """Called when a view has been displayed for this widget instance
Parameters Parameters
---------- ----------
view_name: unicode [view_name]: unicode (optional kwarg)
Name of the view that was displayed. Name of the view that was displayed."""
parent: Widget instance [optional]
Widget that this widget should be displayed as a child of."""
for handler in self._display_callbacks: for handler in self._display_callbacks:
if callable(handler): if callable(handler):
argspec = inspect.getargspec(handler) argspec = inspect.getargspec(handler)
@ -204,12 +202,9 @@ class Widget(LoggingConfigurable):
elif nargs == 1: elif nargs == 1:
handler(self) handler(self)
elif nargs == 2: elif nargs == 2:
handler(self, view_name) handler(self, kwargs.get('view_name', None))
elif nargs == 3:
handler(self, view_name, parent)
else: else:
raise TypeError('Widget display callback must ' \ handler(self, **kwargs)
'accept 0-2 arguments, not %d.' % nargs)
# Public methods # Public methods
@ -375,7 +370,8 @@ class Widget(LoggingConfigurable):
- callback() - callback()
- callback(sender) - callback(sender)
- callback(sender, view_name) - callback(sender, view_name)
- callback(sender, view_name, parent) - callback(sender, **kwargs)
kwargs from display call passed through without modification.
remove: bool remove: bool
True if the callback should be unregistered.""" True if the callback should be unregistered."""
if remove and callback in self._display_callbacks: if remove and callback in self._display_callbacks:
@ -385,7 +381,7 @@ class Widget(LoggingConfigurable):
# Support methods # Support methods
def _repr_widget_(self, view_name=None): def _repr_widget_(self, **kwargs):
"""Function that is called when `IPython.display.display` is called on """Function that is called when `IPython.display.display` is called on
the widget. the widget.
@ -393,8 +389,7 @@ class Widget(LoggingConfigurable):
---------- ----------
view_name: unicode (optional) view_name: unicode (optional)
View to display in the frontend. Overrides default_view_name.""" View to display in the frontend. Overrides default_view_name."""
if not view_name: view_name = kwargs.get('view_name', self.default_view_name)
view_name = self.default_view_name
# Create a communication. # Create a communication.
self._open_communication() self._open_communication()
@ -405,12 +400,11 @@ class Widget(LoggingConfigurable):
# Show view. # Show view.
if self.parent is None or self.parent._comm is None: if self.parent is None or self.parent._comm is None:
self._send({"method": "display", "view_name": view_name}) self._send({"method": "display", "view_name": view_name})
self._handle_displayed(view_name)
else: else:
self._send({"method": "display", self._send({"method": "display",
"view_name": view_name, "view_name": view_name,
"parent": self.parent._comm.comm_id}) "parent": self.parent._comm.comm_id})
self._handle_displayed(view_name, self.parent) self._handle_displayed(**kwargs)
self._displayed = True self._displayed = True
# Now display children if any. # Now display children if any.