##// END OF EJS Templates
Enable widget instanciation from front-end.
Jonathan Frederic -
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,12 +135,13 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)
144 if open_comm:
134 self.open()
145 self.open()
135
146
136 def __del__(self):
147 def __del__(self):
@@ -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.comm = Comm(**args)
163 self.set_comm(Comm(**args))
164
165 # first update
166 self.send_state()
167
168 def set_comm(self, comm):
169 """Set's the comm of the widget."""
170 self.comm = comm
153 self._model_id = self.model_id
171 self._model_id = self.model_id
154
172
155 self.comm.on_msg(self._handle_msg)
173 self.comm.on_msg(self._handle_msg)
156 Widget.widgets[self.model_id] = self
174 Widget.widgets[self.model_id] = self
157
175
158 # first update
159 self.send_state()
160
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.
@@ -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 return
114 try:
114 try:
115 f(comm, msg)
115 f(comm, msg)
116 return
116 except Exception:
117 except Exception:
117 self.log.error("Exception opening comm with target: %s", target_name, exc_info=True)
118 self.log.error("Exception opening comm with target: %s", target_name, exc_info=True)
119
120 # Failure.
121 try:
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