Show More
@@ -187,12 +187,47 b' define([' | |||||
187 |
|
187 | |||
188 | WidgetManager.prototype._handle_comm_open = function (comm, msg) { |
|
188 | WidgetManager.prototype._handle_comm_open = function (comm, msg) { | |
189 | // Handle when a comm is opened. |
|
189 | // Handle when a comm is opened. | |
|
190 | return this.create_model({model_name: msg.content.data.target_name, comm: comm}); | |||
|
191 | }; | |||
|
192 | ||||
|
193 | WidgetManager.prototype.create_model = function (model_name, target_name) { | |||
|
194 | // Create and return a new widget model. | |||
|
195 | // | |||
|
196 | // Parameters | |||
|
197 | // ---------- | |||
|
198 | // model_name: string | |||
|
199 | // Target name of the widget model to create. | |||
|
200 | // target_name: string | |||
|
201 | // Target name of the widget in the back-end. | |||
|
202 | return this._create_model({model_name: model_name, target_name: target_name}); | |||
|
203 | }; | |||
|
204 | ||||
|
205 | WidgetManager.prototype._create_model = function (options) { | |||
|
206 | // Create and return a new widget model. | |||
|
207 | // | |||
|
208 | // Parameters | |||
|
209 | // ---------- | |||
|
210 | // options: dictionary | |||
|
211 | // Dictionary of options with the following contents: | |||
|
212 | // model_name: string | |||
|
213 | // Target name of the widget model to create. | |||
|
214 | // target_name: (optional) string | |||
|
215 | // Target name of the widget in the back-end. | |||
|
216 | // comm: (optional) Comm | |||
|
217 | ||||
|
218 | // Create a comm if it wasn't provided. | |||
|
219 | var comm = options.comm; | |||
|
220 | if (!comm) { | |||
|
221 | comm = this.comm_manager.new_comm('ipython.widget', {'target_name': options.target_name}); | |||
|
222 | } | |||
|
223 | ||||
|
224 | // Create and return a new model that is connected to the comm. | |||
190 | var that = this; |
|
225 | var that = this; | |
191 |
|
226 | |||
192 | var instantiate_model = function(ModelType) { |
|
227 | var instantiate_model = function(ModelType) { | |
193 | var model_id = comm.comm_id; |
|
228 | var model_id = comm.comm_id; | |
194 | var widget_model = new ModelType(that, model_id, comm); |
|
229 | var widget_model = new ModelType(that, model_id, comm); | |
195 | widget_model.on('comm:close', function () { |
|
230 | widget_model.on('comm:close', function () {sss | |
196 | delete that._models[model_id]; |
|
231 | delete that._models[model_id]; | |
197 | }); |
|
232 | }); | |
198 | that._models[model_id] = widget_model; |
|
233 | that._models[model_id] = widget_model; |
@@ -18,6 +18,7 b' import collections' | |||||
18 | from IPython.core.getipython import get_ipython |
|
18 | from IPython.core.getipython import get_ipython | |
19 | from IPython.kernel.comm import Comm |
|
19 | from IPython.kernel.comm import Comm | |
20 | from IPython.config import LoggingConfigurable |
|
20 | from IPython.config import LoggingConfigurable | |
|
21 | from IPython.utils.importstring import import_item | |||
21 | from IPython.utils.traitlets import Unicode, Dict, Instance, Bool, List, \ |
|
22 | from IPython.utils.traitlets import Unicode, Dict, Instance, Bool, List, \ | |
22 | CaselessStrEnum, Tuple, CUnicode, Int, Set |
|
23 | CaselessStrEnum, Tuple, CUnicode, Int, Set | |
23 | from IPython.utils.py3compat import string_types |
|
24 | from IPython.utils.py3compat import string_types | |
@@ -95,6 +96,15 b' class Widget(LoggingConfigurable):' | |||||
95 | if Widget._widget_construction_callback is not None and callable(Widget._widget_construction_callback): |
|
96 | if Widget._widget_construction_callback is not None and callable(Widget._widget_construction_callback): | |
96 | Widget._widget_construction_callback(widget) |
|
97 | Widget._widget_construction_callback(widget) | |
97 |
|
98 | |||
|
99 | @staticmethod | |||
|
100 | def handle_comm_opened(comm, msg): | |||
|
101 | """Static method, called when a widget is constructed.""" | |||
|
102 | target_name = msg['content']['data']['target_name'] | |||
|
103 | widget_class = import_item(target_name) | |||
|
104 | widget = widget_class(open_comm=False) | |||
|
105 | widget.set_comm(comm) | |||
|
106 | ||||
|
107 | ||||
98 | #------------------------------------------------------------------------- |
|
108 | #------------------------------------------------------------------------- | |
99 | # Traits |
|
109 | # Traits | |
100 | #------------------------------------------------------------------------- |
|
110 | #------------------------------------------------------------------------- | |
@@ -125,13 +135,14 b' class Widget(LoggingConfigurable):' | |||||
125 | #------------------------------------------------------------------------- |
|
135 | #------------------------------------------------------------------------- | |
126 | # (Con/de)structor |
|
136 | # (Con/de)structor | |
127 | #------------------------------------------------------------------------- |
|
137 | #------------------------------------------------------------------------- | |
128 | def __init__(self, **kwargs): |
|
138 | def __init__(self, open_comm=True, **kwargs): | |
129 | """Public constructor""" |
|
139 | """Public constructor""" | |
130 | self._model_id = kwargs.pop('model_id', None) |
|
140 | self._model_id = kwargs.pop('model_id', None) | |
131 | super(Widget, self).__init__(**kwargs) |
|
141 | super(Widget, self).__init__(**kwargs) | |
132 |
|
142 | |||
133 | Widget._call_widget_constructed(self) |
|
143 | Widget._call_widget_constructed(self) | |
134 |
|
|
144 | if open_comm: | |
|
145 | self.open() | |||
135 |
|
146 | |||
136 | def __del__(self): |
|
147 | def __del__(self): | |
137 | """Object disposal""" |
|
148 | """Object disposal""" | |
@@ -149,15 +160,19 b' class Widget(LoggingConfigurable):' | |||||
149 | 'model_module': self._model_module}) |
|
160 | 'model_module': self._model_module}) | |
150 | if self._model_id is not None: |
|
161 | if self._model_id is not None: | |
151 | args['comm_id'] = self._model_id |
|
162 | args['comm_id'] = self._model_id | |
152 |
self. |
|
163 | self.set_comm(Comm(**args)) | |
153 | self._model_id = self.model_id |
|
|||
154 |
|
164 | |||
155 | self.comm.on_msg(self._handle_msg) |
|
|||
156 | Widget.widgets[self.model_id] = self |
|
|||
157 |
|
||||
158 | # first update |
|
165 | # first update | |
159 | self.send_state() |
|
166 | self.send_state() | |
160 |
|
167 | |||
|
168 | def set_comm(self, comm): | |||
|
169 | """Set's the comm of the widget.""" | |||
|
170 | self.comm = comm | |||
|
171 | self._model_id = self.model_id | |||
|
172 | ||||
|
173 | self.comm.on_msg(self._handle_msg) | |||
|
174 | Widget.widgets[self.model_id] = self | |||
|
175 | ||||
161 | @property |
|
176 | @property | |
162 | def model_id(self): |
|
177 | def model_id(self): | |
163 | """Gets the model id of this widget. |
|
178 | """Gets the model id of this widget. | |
@@ -330,7 +345,7 b' class Widget(LoggingConfigurable):' | |||||
330 | def _handle_custom_msg(self, content): |
|
345 | def _handle_custom_msg(self, content): | |
331 | """Called when a custom msg is received.""" |
|
346 | """Called when a custom msg is received.""" | |
332 | self._msg_callbacks(self, content) |
|
347 | self._msg_callbacks(self, content) | |
333 |
|
348 | |||
334 | def _notify_trait(self, name, old_value, new_value): |
|
349 | def _notify_trait(self, name, old_value, new_value): | |
335 | """Called when a property has been changed.""" |
|
350 | """Called when a property has been changed.""" | |
336 | # Trigger default traitlet callback machinery. This allows any user |
|
351 | # Trigger default traitlet callback machinery. This allows any user | |
@@ -341,10 +356,10 b' class Widget(LoggingConfigurable):' | |||||
341 | # Send the state after the user registered callbacks for trait changes |
|
356 | # Send the state after the user registered callbacks for trait changes | |
342 | # have all fired (allows for user to validate values). |
|
357 | # have all fired (allows for user to validate values). | |
343 | if self.comm is not None and name in self.keys: |
|
358 | if self.comm is not None and name in self.keys: | |
344 |
|
|
359 | # Make sure this isn't information that the front-end just sent us. | |
345 | if self._should_send_property(name, new_value): |
|
360 | if self._should_send_property(name, new_value): | |
346 |
|
|
361 | # Send new state to front-end | |
347 |
|
|
362 | self.send_state(key=name) | |
348 |
|
363 | |||
349 | def _handle_displayed(self, **kwargs): |
|
364 | def _handle_displayed(self, **kwargs): | |
350 | """Called when a view has been displayed for this widget instance""" |
|
365 | """Called when a view has been displayed for this widget instance""" |
@@ -107,15 +107,21 b' class CommManager(LoggingConfigurable):' | |||||
107 | iopub_socket=self.iopub_socket, |
|
107 | iopub_socket=self.iopub_socket, | |
108 | primary=False, |
|
108 | primary=False, | |
109 | ) |
|
109 | ) | |
|
110 | self.register_comm(comm) | |||
110 | if f is None: |
|
111 | if f is None: | |
111 | self.log.error("No such comm target registered: %s", target_name) |
|
112 | self.log.error("No such comm target registered: %s", target_name) | |
112 | comm.close() |
|
113 | else: | |
113 |
|
|
114 | try: | |
|
115 | f(comm, msg) | |||
|
116 | return | |||
|
117 | except Exception: | |||
|
118 | self.log.error("Exception opening comm with target: %s", target_name, exc_info=True) | |||
|
119 | ||||
|
120 | # Failure. | |||
114 | try: |
|
121 | try: | |
115 | f(comm, msg) |
|
|||
116 | except Exception: |
|
|||
117 | self.log.error("Exception opening comm with target: %s", target_name, exc_info=True) |
|
|||
118 | comm.close() |
|
122 | comm.close() | |
|
123 | except: | |||
|
124 | pass # Eat errors, nomnomnom | |||
119 |
|
125 | |||
120 | def comm_msg(self, stream, ident, msg): |
|
126 | def comm_msg(self, stream, ident, msg): | |
121 | """Handler for comm_msg messages""" |
|
127 | """Handler for comm_msg messages""" |
General Comments 0
You need to be logged in to leave comments.
Login now