##// END OF EJS Templates
Enable widget instanciation from front-end.
Jonathan Frederic -
Show More
@@ -187,12 +187,47 b' define(['
187 187
188 188 WidgetManager.prototype._handle_comm_open = function (comm, msg) {
189 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 225 var that = this;
191 226
192 227 var instantiate_model = function(ModelType) {
193 228 var model_id = comm.comm_id;
194 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 231 delete that._models[model_id];
197 232 });
198 233 that._models[model_id] = widget_model;
@@ -18,6 +18,7 b' import collections'
18 18 from IPython.core.getipython import get_ipython
19 19 from IPython.kernel.comm import Comm
20 20 from IPython.config import LoggingConfigurable
21 from IPython.utils.importstring import import_item
21 22 from IPython.utils.traitlets import Unicode, Dict, Instance, Bool, List, \
22 23 CaselessStrEnum, Tuple, CUnicode, Int, Set
23 24 from IPython.utils.py3compat import string_types
@@ -95,6 +96,15 b' class Widget(LoggingConfigurable):'
95 96 if Widget._widget_construction_callback is not None and callable(Widget._widget_construction_callback):
96 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 109 # Traits
100 110 #-------------------------------------------------------------------------
@@ -125,13 +135,14 b' class Widget(LoggingConfigurable):'
125 135 #-------------------------------------------------------------------------
126 136 # (Con/de)structor
127 137 #-------------------------------------------------------------------------
128 def __init__(self, **kwargs):
138 def __init__(self, open_comm=True, **kwargs):
129 139 """Public constructor"""
130 140 self._model_id = kwargs.pop('model_id', None)
131 141 super(Widget, self).__init__(**kwargs)
132 142
133 143 Widget._call_widget_constructed(self)
134 self.open()
144 if open_comm:
145 self.open()
135 146
136 147 def __del__(self):
137 148 """Object disposal"""
@@ -149,15 +160,19 b' class Widget(LoggingConfigurable):'
149 160 'model_module': self._model_module})
150 161 if self._model_id is not None:
151 162 args['comm_id'] = self._model_id
152 self.comm = Comm(**args)
153 self._model_id = self.model_id
163 self.set_comm(Comm(**args))
154 164
155 self.comm.on_msg(self._handle_msg)
156 Widget.widgets[self.model_id] = self
157
158 165 # first update
159 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 176 @property
162 177 def model_id(self):
163 178 """Gets the model id of this widget.
@@ -330,7 +345,7 b' class Widget(LoggingConfigurable):'
330 345 def _handle_custom_msg(self, content):
331 346 """Called when a custom msg is received."""
332 347 self._msg_callbacks(self, content)
333
348
334 349 def _notify_trait(self, name, old_value, new_value):
335 350 """Called when a property has been changed."""
336 351 # Trigger default traitlet callback machinery. This allows any user
@@ -341,10 +356,10 b' class Widget(LoggingConfigurable):'
341 356 # Send the state after the user registered callbacks for trait changes
342 357 # have all fired (allows for user to validate values).
343 358 if self.comm is not None and name in self.keys:
344 # Make sure this isn't information that the front-end just sent us.
359 # Make sure this isn't information that the front-end just sent us.
345 360 if self._should_send_property(name, new_value):
346 # Send new state to front-end
347 self.send_state(key=name)
361 # Send new state to front-end
362 self.send_state(key=name)
348 363
349 364 def _handle_displayed(self, **kwargs):
350 365 """Called when a view has been displayed for this widget instance"""
@@ -107,15 +107,21 b' class CommManager(LoggingConfigurable):'
107 107 iopub_socket=self.iopub_socket,
108 108 primary=False,
109 109 )
110 self.register_comm(comm)
110 111 if f is None:
111 112 self.log.error("No such comm target registered: %s", target_name)
112 comm.close()
113 return
113 else:
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 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 122 comm.close()
123 except:
124 pass # Eat errors, nomnomnom
119 125
120 126 def comm_msg(self, stream, ident, msg):
121 127 """Handler for comm_msg messages"""
General Comments 0
You need to be logged in to leave comments. Login now