From 16c366d1b5357e04c363e9e2da38e8d4bbb96107 Mon Sep 17 00:00:00 2001 From: Jason Grout Date: Thu, 17 Jul 2014 19:09:50 +0000 Subject: [PATCH 1/3] Automatically open a comm on creation of widgets This will immediately create a model on the javascript side when a widget is created. This means that, for example, a widget that only interacts with its model can work without "displaying" it. --- IPython/html/widgets/widget.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/IPython/html/widgets/widget.py b/IPython/html/widgets/widget.py index be945ca2d..2107f21b2 100644 --- a/IPython/html/widgets/widget.py +++ b/IPython/html/widgets/widget.py @@ -123,6 +123,7 @@ class Widget(LoggingConfigurable): self.on_trait_change(self._handle_property_changed, self.keys) Widget._call_widget_constructed(self) + self.open() def __del__(self): """Object disposal""" @@ -137,6 +138,11 @@ class Widget(LoggingConfigurable): """Gets the Comm associated with this widget. If a Comm doesn't exist yet, a Comm will be created automagically.""" + self.open() + return self._comm + + def open(self): + """Open a comm to the frontend if one isn't already open.""" if self._comm is None: # Create a comm. self._comm = Comm(target_name=self._model_name) @@ -145,8 +151,7 @@ class Widget(LoggingConfigurable): # first update self.send_state() - return self._comm - + @property def model_id(self): """Gets the model id of this widget. From 8513f673e312f6c00ff156f3bd52a60c8c3f68ab Mon Sep 17 00:00:00 2001 From: Jason Grout Date: Wed, 23 Jul 2014 18:00:32 +0000 Subject: [PATCH 2/3] Make the widget comm attribute more straigtforward Instead of automagically instantiating a comm when it is accessed, require a call to open(). This makes the comm attribute much less magical, and hopefully more understandable. --- IPython/html/widgets/widget.py | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/IPython/html/widgets/widget.py b/IPython/html/widgets/widget.py index 2107f21b2..f9ee5fc29 100644 --- a/IPython/html/widgets/widget.py +++ b/IPython/html/widgets/widget.py @@ -100,7 +100,7 @@ class Widget(LoggingConfigurable): registered in the front-end to create and sync this widget with.""") _view_name = Unicode(help="""Default view registered in the front-end to use to represent the widget.""", sync=True) - _comm = Instance('IPython.kernel.comm.Comm') + comm = Instance('IPython.kernel.comm.Comm') msg_throttle = Int(3, sync=True, help="""Maximum number of msgs the front-end can send before receiving an idle msg from the back-end.""") @@ -133,20 +133,12 @@ class Widget(LoggingConfigurable): # Properties #------------------------------------------------------------------------- - @property - def comm(self): - """Gets the Comm associated with this widget. - - If a Comm doesn't exist yet, a Comm will be created automagically.""" - self.open() - return self._comm - def open(self): """Open a comm to the frontend if one isn't already open.""" - if self._comm is None: + if self.comm is None: # Create a comm. - self._comm = Comm(target_name=self._model_name) - self._comm.on_msg(self._handle_msg) + self.comm = Comm(target_name=self._model_name) + self.comm.on_msg(self._handle_msg) Widget.widgets[self.model_id] = self # first update @@ -169,10 +161,10 @@ class Widget(LoggingConfigurable): Closes the underlying comm. When the comm is closed, all of the widget views are automatically removed from the front-end.""" - if self._comm is not None: + if self.comm is not None: Widget.widgets.pop(self.model_id, None) - self._comm.close() - self._comm = None + self.comm.close() + self.comm = None def send_state(self, key=None): """Sends the widget state, or a piece of it, to the front-end. @@ -393,7 +385,10 @@ class DOMWidget(Widget): selector: unicode (optional, kwarg only) JQuery selector to use to apply the CSS key/value. If no selector is provided, an empty selector is used. An empty selector makes the - front-end try to apply the css to the top-level element. + front-end try to apply the css to a default element. The default + element is an attribute unique to each view, which is a DOM element + of the view that should be styled with common CSS (see + `$el_to_style` in the Javascript code). """ if value is None: css_dict = dict_or_key From f9fa4ada0601971173ed725d2966fcb101ecc8cb Mon Sep 17 00:00:00 2001 From: Sylvain Corlay Date: Fri, 1 Aug 2014 23:16:02 -0400 Subject: [PATCH 3/3] allowing widet id to be set externally --- IPython/html/widgets/widget.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/IPython/html/widgets/widget.py b/IPython/html/widgets/widget.py index f9ee5fc29..f7aae5d03 100644 --- a/IPython/html/widgets/widget.py +++ b/IPython/html/widgets/widget.py @@ -119,6 +119,7 @@ class Widget(LoggingConfigurable): #------------------------------------------------------------------------- def __init__(self, **kwargs): """Public constructor""" + self._model_id = kwargs.pop('model_id', None) super(Widget, self).__init__(**kwargs) self.on_trait_change(self._handle_property_changed, self.keys) @@ -136,8 +137,11 @@ class Widget(LoggingConfigurable): def open(self): """Open a comm to the frontend if one isn't already open.""" if self.comm is None: - # Create a comm. - self.comm = Comm(target_name=self._model_name) + if self._model_id is None: + self.comm = Comm(target_name=self._model_name) + self._model_id = self.model_id + else: + self.comm = Comm(target_name=self._model_name, comm_id=self._model_id) self.comm.on_msg(self._handle_msg) Widget.widgets[self.model_id] = self