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