Show More
@@ -101,7 +101,7 b' class Widget(LoggingConfigurable):' | |||||
101 | registered in the front-end to create and sync this widget with.""") |
|
101 | registered in the front-end to create and sync this widget with.""") | |
102 | _view_name = Unicode('WidgetView', help="""Default view registered in the front-end |
|
102 | _view_name = Unicode('WidgetView', help="""Default view registered in the front-end | |
103 | to use to represent the widget.""", sync=True) |
|
103 | to use to represent the widget.""", sync=True) | |
104 |
|
|
104 | comm = Instance('IPython.kernel.comm.Comm') | |
105 |
|
105 | |||
106 | msg_throttle = Int(3, sync=True, help="""Maximum number of msgs the |
|
106 | msg_throttle = Int(3, sync=True, help="""Maximum number of msgs the | |
107 | front-end can send before receiving an idle msg from the back-end.""") |
|
107 | front-end can send before receiving an idle msg from the back-end.""") | |
@@ -121,10 +121,12 b' class Widget(LoggingConfigurable):' | |||||
121 | #------------------------------------------------------------------------- |
|
121 | #------------------------------------------------------------------------- | |
122 | def __init__(self, **kwargs): |
|
122 | def __init__(self, **kwargs): | |
123 | """Public constructor""" |
|
123 | """Public constructor""" | |
|
124 | self._model_id = kwargs.pop('model_id', None) | |||
124 | super(Widget, self).__init__(**kwargs) |
|
125 | super(Widget, self).__init__(**kwargs) | |
125 |
|
126 | |||
126 | self.on_trait_change(self._handle_property_changed, self.keys) |
|
127 | self.on_trait_change(self._handle_property_changed, self.keys) | |
127 | Widget._call_widget_constructed(self) |
|
128 | Widget._call_widget_constructed(self) | |
|
129 | self.open() | |||
128 |
|
130 | |||
129 | def __del__(self): |
|
131 | def __del__(self): | |
130 | """Object disposal""" |
|
132 | """Object disposal""" | |
@@ -134,21 +136,20 b' class Widget(LoggingConfigurable):' | |||||
134 | # Properties |
|
136 | # Properties | |
135 | #------------------------------------------------------------------------- |
|
137 | #------------------------------------------------------------------------- | |
136 |
|
138 | |||
137 | @property |
|
139 | def open(self): | |
138 | def comm(self): |
|
140 | """Open a comm to the frontend if one isn't already open.""" | |
139 | """Gets the Comm associated with this widget. |
|
141 | if self.comm is None: | |
140 |
|
142 | if self._model_id is None: | ||
141 | If a Comm doesn't exist yet, a Comm will be created automagically.""" |
|
143 | self.comm = Comm(target_name=self._model_name) | |
142 | if self._comm is None: |
|
144 | self._model_id = self.model_id | |
143 |
|
|
145 | else: | |
144 |
self. |
|
146 | self.comm = Comm(target_name=self._model_name, comm_id=self._model_id) | |
145 |
self. |
|
147 | self.comm.on_msg(self._handle_msg) | |
146 | Widget.widgets[self.model_id] = self |
|
148 | Widget.widgets[self.model_id] = self | |
147 |
|
149 | |||
148 | # first update |
|
150 | # first update | |
149 | self.send_state() |
|
151 | self.send_state() | |
150 | return self._comm |
|
152 | ||
151 |
|
||||
152 | @property |
|
153 | @property | |
153 | def model_id(self): |
|
154 | def model_id(self): | |
154 | """Gets the model id of this widget. |
|
155 | """Gets the model id of this widget. | |
@@ -166,10 +167,10 b' class Widget(LoggingConfigurable):' | |||||
166 | Closes the underlying comm. |
|
167 | Closes the underlying comm. | |
167 | When the comm is closed, all of the widget views are automatically |
|
168 | When the comm is closed, all of the widget views are automatically | |
168 | removed from the front-end.""" |
|
169 | removed from the front-end.""" | |
169 |
if self. |
|
170 | if self.comm is not None: | |
170 | Widget.widgets.pop(self.model_id, None) |
|
171 | Widget.widgets.pop(self.model_id, None) | |
171 |
self. |
|
172 | self.comm.close() | |
172 |
self. |
|
173 | self.comm = None | |
173 |
|
174 | |||
174 | def send_state(self, key=None): |
|
175 | def send_state(self, key=None): | |
175 | """Sends the widget state, or a piece of it, to the front-end. |
|
176 | """Sends the widget state, or a piece of it, to the front-end. | |
@@ -416,7 +417,10 b' class DOMWidget(Widget):' | |||||
416 | selector: unicode (optional, kwarg only) |
|
417 | selector: unicode (optional, kwarg only) | |
417 | JQuery selector to use to apply the CSS key/value. If no selector |
|
418 | JQuery selector to use to apply the CSS key/value. If no selector | |
418 | is provided, an empty selector is used. An empty selector makes the |
|
419 | is provided, an empty selector is used. An empty selector makes the | |
419 |
front-end try to apply the css to |
|
420 | front-end try to apply the css to a default element. The default | |
|
421 | element is an attribute unique to each view, which is a DOM element | |||
|
422 | of the view that should be styled with common CSS (see | |||
|
423 | `$el_to_style` in the Javascript code). | |||
420 | """ |
|
424 | """ | |
421 | if value is None: |
|
425 | if value is None: | |
422 | css_dict = dict_or_key |
|
426 | css_dict = dict_or_key |
@@ -23,7 +23,7 b' class Comm(LoggingConfigurable):' | |||||
23 | return self.shell.kernel.iopub_socket |
|
23 | return self.shell.kernel.iopub_socket | |
24 | session = Instance('IPython.kernel.zmq.session.Session') |
|
24 | session = Instance('IPython.kernel.zmq.session.Session') | |
25 | def _session_default(self): |
|
25 | def _session_default(self): | |
26 | if self.shell is None: |
|
26 | if self.shell is None or not hasattr(self.shell, 'kernel'): | |
27 | return |
|
27 | return | |
28 | return self.shell.kernel.session |
|
28 | return self.shell.kernel.session | |
29 |
|
29 | |||
@@ -56,15 +56,16 b' class Comm(LoggingConfigurable):' | |||||
56 |
|
56 | |||
57 | def _publish_msg(self, msg_type, data=None, metadata=None, **keys): |
|
57 | def _publish_msg(self, msg_type, data=None, metadata=None, **keys): | |
58 | """Helper for sending a comm message on IOPub""" |
|
58 | """Helper for sending a comm message on IOPub""" | |
59 | data = {} if data is None else data |
|
59 | if self.session is not None: | |
60 |
|
|
60 | data = {} if data is None else data | |
61 | content = json_clean(dict(data=data, comm_id=self.comm_id, **keys)) |
|
61 | metadata = {} if metadata is None else metadata | |
62 | self.session.send(self.iopub_socket, msg_type, |
|
62 | content = json_clean(dict(data=data, comm_id=self.comm_id, **keys)) | |
63 | content, |
|
63 | self.session.send(self.iopub_socket, msg_type, | |
64 | metadata=json_clean(metadata), |
|
64 | content, | |
65 | parent=self.shell.get_parent(), |
|
65 | metadata=json_clean(metadata), | |
66 |
|
|
66 | parent=self.shell.get_parent(), | |
67 | ) |
|
67 | ident=self.topic, | |
|
68 | ) | |||
68 |
|
69 | |||
69 | def __del__(self): |
|
70 | def __del__(self): | |
70 | """trigger close on gc""" |
|
71 | """trigger close on gc""" | |
@@ -77,7 +78,9 b' class Comm(LoggingConfigurable):' | |||||
77 | if data is None: |
|
78 | if data is None: | |
78 | data = self._open_data |
|
79 | data = self._open_data | |
79 | self._closed = False |
|
80 | self._closed = False | |
80 | get_ipython().comm_manager.register_comm(self) |
|
81 | ip = get_ipython() | |
|
82 | if hasattr(ip, 'comm_manager'): | |||
|
83 | ip.comm_manager.register_comm(self) | |||
81 | self._publish_msg('comm_open', data, metadata, target_name=self.target_name) |
|
84 | self._publish_msg('comm_open', data, metadata, target_name=self.target_name) | |
82 |
|
85 | |||
83 | def close(self, data=None, metadata=None): |
|
86 | def close(self, data=None, metadata=None): | |
@@ -88,7 +91,9 b' class Comm(LoggingConfigurable):' | |||||
88 | if data is None: |
|
91 | if data is None: | |
89 | data = self._close_data |
|
92 | data = self._close_data | |
90 | self._publish_msg('comm_close', data, metadata) |
|
93 | self._publish_msg('comm_close', data, metadata) | |
91 | get_ipython().comm_manager.unregister_comm(self) |
|
94 | ip = get_ipython() | |
|
95 | if hasattr(ip, 'comm_manager'): | |||
|
96 | ip.comm_manager.unregister_comm(self) | |||
92 | self._closed = True |
|
97 | self._closed = True | |
93 |
|
98 | |||
94 | def send(self, data=None, metadata=None): |
|
99 | def send(self, data=None, metadata=None): |
General Comments 0
You need to be logged in to leave comments.
Login now