Show More
@@ -53,7 +53,7 b' define([' | |||
|
53 | 53 | " message was from. Widget will not be displayed"); |
|
54 | 54 | } else { |
|
55 | 55 | var that = this; |
|
56 |
this.create_view(model, {cell: cell, |
|
|
56 | this.create_view(model, {cell: cell, success: function(view) { | |
|
57 | 57 | that._handle_display_view(view); |
|
58 | 58 | if (cell.widget_subarea) { |
|
59 | 59 | cell.widget_subarea.append(view.$el); |
@@ -84,7 +84,7 b' define([' | |||
|
84 | 84 | |
|
85 | 85 | var view_name = model.get('_view_name'); |
|
86 | 86 | var view_mod = model.get('_view_module'); |
|
87 |
var err |
|
|
87 | var error = options.error || function(error) { console.log(error); }; | |
|
88 | 88 | |
|
89 | 89 | var instantiate_view = function(ViewType) { |
|
90 | 90 | if (ViewType) { |
@@ -100,9 +100,11 b' define([' | |||
|
100 | 100 | var view = new ViewType(parameters); |
|
101 | 101 | view.render(); |
|
102 | 102 | model.on('destroy', view.remove, view); |
|
103 |
options. |
|
|
103 | if (options.success) { | |
|
104 | options.success(view); | |
|
105 | } | |
|
104 | 106 | } else { |
|
105 |
err |
|
|
107 | error({unknown_view: true, view_name: view_name, | |
|
106 | 108 | view_module: view_mod}); |
|
107 | 109 | } |
|
108 | 110 | }; |
@@ -110,7 +112,7 b' define([' | |||
|
110 | 112 | if (view_mod) { |
|
111 | 113 | require([view_mod], function(module) { |
|
112 | 114 | instantiate_view(module[view_name]); |
|
113 |
}, err |
|
|
115 | }, error); | |
|
114 | 116 | } else { |
|
115 | 117 | instantiate_view(WidgetManager._view_types[view_name]); |
|
116 | 118 | } |
@@ -157,7 +159,7 b' define([' | |||
|
157 | 159 | handle_clear_output = $.proxy(cell.output_area.handle_clear_output, cell.output_area); |
|
158 | 160 | } |
|
159 | 161 | |
|
160 | // Create callback dict using what is known | |
|
162 | // Create callback dictionary using what is known | |
|
161 | 163 | var that = this; |
|
162 | 164 | callbacks = { |
|
163 | 165 | iopub : { |
@@ -187,7 +189,10 b' define([' | |||
|
187 | 189 | |
|
188 | 190 | WidgetManager.prototype._handle_comm_open = function (comm, msg) { |
|
189 | 191 | // Handle when a comm is opened. |
|
190 | return this.create_model({model_name: msg.content.data.model_name, comm: comm}); | |
|
192 | this.create_model({ | |
|
193 | model_name: msg.content.data.model_name, | |
|
194 | model_module: msg.content.data.model_module, | |
|
195 | comm: comm}); | |
|
191 | 196 | }; |
|
192 | 197 | |
|
193 | 198 | WidgetManager.prototype.create_model = function (options) { |
@@ -199,7 +204,7 b' define([' | |||
|
199 | 204 | // Example |
|
200 | 205 | // -------- |
|
201 | 206 | // JS: |
|
202 |
// |
|
|
207 | // IPython.notebook.kernel.widget_manager.create_model({ | |
|
203 | 208 | // model_name: 'WidgetModel', |
|
204 | 209 | // widget_class: 'IPython.html.widgets.widget_int.IntSlider', |
|
205 | 210 | // init_state_callback: function(model) { console.log('Create success!', model); }}); |
@@ -210,23 +215,31 b' define([' | |||
|
210 | 215 | // Dictionary of options with the following contents: |
|
211 | 216 | // model_name: string |
|
212 | 217 | // Target name of the widget model to create. |
|
218 | // model_module: (optional) string | |
|
219 | // Module name of the widget model to create. | |
|
213 | 220 | // widget_class: (optional) string |
|
214 | 221 | // Target name of the widget in the back-end. |
|
215 | 222 | // comm: (optional) Comm |
|
223 | // success: (optional) callback | |
|
224 | // Callback for when the model was created successfully. | |
|
225 | // error: (optional) callback | |
|
226 | // Callback for when the model wasn't created. | |
|
216 | 227 | // init_state_callback: (optional) callback |
|
217 | 228 | // Called when the first state push from the back-end is |
|
218 | 229 | // recieved. Allows you to modify the model after it's |
|
219 | 230 | // complete state is filled and synced. |
|
220 | 231 | |
|
232 | // Make default callbacks if not specified. | |
|
233 | var error = options.error || function(error) { console.log(error); }; | |
|
234 | ||
|
221 | 235 | // Create a comm if it wasn't provided. |
|
222 | 236 | var comm = options.comm; |
|
223 | 237 | if (!comm) { |
|
224 | 238 | comm = this.comm_manager.new_comm('ipython.widget', {'widget_class': options.widget_class}); |
|
225 | 239 | } |
|
226 | 240 | |
|
227 |
// Create a |
|
|
241 | // Create a new model that is connected to the comm. | |
|
228 | 242 | var that = this; |
|
229 | ||
|
230 | 243 | var instantiate_model = function(ModelType) { |
|
231 | 244 | var model_id = comm.comm_id; |
|
232 | 245 | var widget_model = new ModelType(that, model_id, comm, options.init_state_callback); |
@@ -234,22 +247,27 b' define([' | |||
|
234 | 247 | delete that._models[model_id]; |
|
235 | 248 | }); |
|
236 | 249 | that._models[model_id] = widget_model; |
|
250 | if (options.success) { | |
|
251 | options.success(widget_model); | |
|
252 | } | |
|
237 | 253 | }; |
|
238 | 254 | |
|
239 | var widget_type_name = msg.content.data.model_name; | |
|
240 |
var widget_m |
|
|
241 | ||
|
255 | // Get the model type using require or through the registry. | |
|
256 | var widget_type_name = options.model_name; | |
|
257 | var widget_module = options.model_module; | |
|
242 | 258 | if (widget_module) { |
|
259 | ||
|
243 | 260 | // Load the module containing the widget model |
|
244 | 261 | require([widget_module], function(mod) { |
|
245 | 262 | if (mod[widget_type_name]) { |
|
246 | 263 | instantiate_model(mod[widget_type_name]); |
|
247 | 264 | } else { |
|
248 |
|
|
|
265 | error("Error creating widget model: " + widget_type_name | |
|
249 | 266 | + " not found in " + widget_module); |
|
250 | 267 | } |
|
251 | }, function(err) { console.log(err); }); | |
|
268 | }, error); | |
|
252 | 269 | } else { |
|
270 | ||
|
253 | 271 | // No module specified, load from the global models registry |
|
254 | 272 | instantiate_model(WidgetManager._model_types[widget_type_name]); |
|
255 | 273 | } |
@@ -327,7 +327,7 b' define(["widgets/js/manager",' | |||
|
327 | 327 | // to the subview without having to add it here. |
|
328 | 328 | var that = this; |
|
329 | 329 | var old_callback = options.callback || function(view) {}; |
|
330 |
options = $.extend({ parent: this, |
|
|
330 | options = $.extend({ parent: this, success: function(child_view) { | |
|
331 | 331 | // Associate the view id with the model id. |
|
332 | 332 | if (that.child_model_views[child_model.id] === undefined) { |
|
333 | 333 | that.child_model_views[child_model.id] = []; |
@@ -1,7 +1,6 b'' | |||
|
1 | 1 | // Test the widget manager. |
|
2 | 2 | casper.notebook_test(function () { |
|
3 | 3 | var index; |
|
4 | var slider = {}; | |
|
5 | 4 | |
|
6 | 5 | this.then(function () { |
|
7 | 6 | |
@@ -16,12 +15,22 b' casper.notebook_test(function () {' | |||
|
16 | 15 | }), 'Notebook widget manager instantiated'); |
|
17 | 16 | |
|
18 | 17 | // Try creating a widget from Javascript. |
|
19 |
|
|
|
20 |
|
|
|
18 | this.evaluate(function() { | |
|
19 | IPython.notebook.kernel.widget_manager.create_model({ | |
|
21 | 20 |
|
|
22 | 21 |
|
|
23 |
|
|
|
24 | return slider.id; | |
|
22 | init_state_callback: function(model) { | |
|
23 | console.log('Create success!', model); | |
|
24 | window.slider_id = model.id; | |
|
25 | } | |
|
26 | }); | |
|
27 | }); | |
|
28 | }); | |
|
29 | ||
|
30 | // Wait for the state to be recieved. | |
|
31 | this.waitFor(function check() { | |
|
32 | return this.evaluate(function() { | |
|
33 | return window.slider_id !== undefined; | |
|
25 | 34 | }); |
|
26 | 35 | }); |
|
27 | 36 | |
@@ -31,6 +40,7 b' casper.notebook_test(function () {' | |||
|
31 | 40 | 'print(widget.model_id)'); |
|
32 | 41 | this.execute_cell_then(index, function(index) { |
|
33 | 42 | var output = this.get_output_cell(index).text.trim(); |
|
34 | this.test.assertEquals(output, slider.id, "Widget created from the front-end."); | |
|
43 | var slider_id = this.evaluate(function() { return window.slider_id; }); | |
|
44 | this.test.assertEquals(output, slider_id, "Widget created from the front-end."); | |
|
35 | 45 | }); |
|
36 | 46 | }); |
@@ -133,7 +133,7 b' class Widget(LoggingConfigurable):' | |||
|
133 | 133 | #------------------------------------------------------------------------- |
|
134 | 134 | # (Con/de)structor |
|
135 | 135 | #------------------------------------------------------------------------- |
|
136 |
def __init__(self, |
|
|
136 | def __init__(self, **kwargs): | |
|
137 | 137 | """Public constructor""" |
|
138 | 138 | self._model_id = kwargs.pop('model_id', None) |
|
139 | 139 | super(Widget, self).__init__(**kwargs) |
General Comments 0
You need to be logged in to leave comments.
Login now