From 16c366d1b5357e04c363e9e2da38e8d4bbb96107 Mon Sep 17 00:00:00 2001 From: Jason Grout Date: Thu, 17 Jul 2014 19:09:50 +0000 Subject: [PATCH] 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.