##// END OF EJS Templates
Custom widget persistence
Sylvain Corlay -
Show More
@@ -282,9 +282,10 b' define(['
282 * Handle when a comm is opened.
282 * Handle when a comm is opened.
283 */
283 */
284 return this.create_model({
284 return this.create_model({
285 model_name: msg.content.data.model_name,
285 model_name: msg.content.data.model_name,
286 model_module: msg.content.data.model_module,
286 model_module: msg.content.data.model_module,
287 comm: comm}).catch(utils.reject("Couldn't create a model.", true));
287 comm: comm,
288 }).catch(utils.reject("Couldn't create a model.", true));
288 };
289 };
289
290
290 WidgetManager.prototype.create_model = function (options) {
291 WidgetManager.prototype.create_model = function (options) {
@@ -298,10 +299,11 b' define(['
298 * --------
299 * --------
299 * JS:
300 * JS:
300 * IPython.notebook.kernel.widget_manager.create_model({
301 * IPython.notebook.kernel.widget_manager.create_model({
301 * model_name: 'WidgetModel',
302 * model_name: 'WidgetModel',
302 * widget_class: 'IPython.html.widgets.widget_int.IntSlider'})
303 * widget_class: 'IPython.html.widgets.widget_int.IntSlider'
303 * .then(function(model) { console.log('Create success!', model); },
304 * })
304 * $.proxy(console.error, console));
305 * .then(function(model) { console.log('Create success!', model); },
306 * $.proxy(console.error, console));
305 *
307 *
306 * Parameters
308 * Parameters
307 * ----------
309 * ----------
@@ -403,7 +405,7 b' define(['
403 return Promise.all(model_promises).then(function() { return state; });
405 return Promise.all(model_promises).then(function() { return state; });
404 }).catch(utils.reject('Could not get state of widget manager', true));
406 }).catch(utils.reject('Could not get state of widget manager', true));
405 };
407 };
406
408
407 WidgetManager.prototype.set_state = function(state) {
409 WidgetManager.prototype.set_state = function(state) {
408 /**
410 /**
409 * Set the notebook's state.
411 * Set the notebook's state.
@@ -411,18 +413,13 b' define(['
411 * Reconstructs all of the widget models and attempts to redisplay the
413 * Reconstructs all of the widget models and attempts to redisplay the
412 * widgets in the appropriate cells by cell index.
414 * widgets in the appropriate cells by cell index.
413 */
415 */
414
416
415 // Get the kernel when it's available.
417 // Get the kernel when it's available.
416 var that = this;
418 var that = this;
417 return this._get_connected_kernel().then(function(kernel) {
419 return this._get_connected_kernel().then(function(kernel) {
418
420
419 // Recreate all the widget models for the given state and
421 // Recreate all the widget models for the given notebook state.
420 // display the views.
422 var all_models = Promise.all(_.map(Object.keys(state), function (model_id) {
421 that.all_views = [];
422 var model_ids = Object.keys(state);
423 for (var i = 0; i < model_ids.length; i++) {
424 var model_id = model_ids[i];
425
426 // Recreate a comm using the widget's model id (model_id == comm_id).
423 // Recreate a comm using the widget's model id (model_id == comm_id).
427 var new_comm = new comm.Comm(kernel.widget_manager.comm_target_name, model_id);
424 var new_comm = new comm.Comm(kernel.widget_manager.comm_target_name, model_id);
428 kernel.comm_manager.register_comm(new_comm);
425 kernel.comm_manager.register_comm(new_comm);
@@ -431,36 +428,31 b' define(['
431 // created we don't know yet if the comm is valid so set_comm_live
428 // created we don't know yet if the comm is valid so set_comm_live
432 // false. Once we receive the first state push from the back-end
429 // false. Once we receive the first state push from the back-end
433 // we know the comm is alive.
430 // we know the comm is alive.
434 var views = kernel.widget_manager.create_model({
431 return kernel.widget_manager.create_model({
435 comm: new_comm,
432 comm: new_comm,
436 model_name: state[model_id].model_name,
433 model_name: state[model_id].model_name,
437 model_module: state[model_id].model_module})
434 model_module: state[model_id].model_module,
438 .then(function(model) {
435 }).then(function(model) {
439
440 model.set_comm_live(false);
436 model.set_comm_live(false);
441 var view_promise = Promise.resolve().then(function() {
437 model.set_state(state[model.id].state);
442 return model.set_state(state[model.id].state);
438 return model.request_state().then(function() {
443 }).then(function() {
439 model.set_comm_live(true);
444 model.request_state().then(function() {
440 return model;
445 model.set_comm_live(true);
446 });
447
448 // Display the views of the model.
449 var views = [];
450 var model_views = state[model.id].views;
451 for (var j=0; j<model_views.length; j++) {
452 var cell_index = model_views[j];
453 var cell = that.notebook.get_cell(cell_index);
454 views.push(that.display_view_in_cell(cell, model));
455 }
456 return Promise.all(views);
457 });
441 });
458 return view_promise;
459 });
442 });
460 that.all_views.push(views);
443 }, this));
461 }
444
462 return Promise.all(that.all_views);
445 // Display all the views
463 }).catch(utils.reject('Could not set widget manager state.', true));
446 return all_models.then(function(models) {
447 return Promise.all(_.map(models, function(model) {
448 // Display the views of the model.
449 return Promise.all(_.map(state[model.id].views, function(cell_index) {
450 var cell = that.notebook.get_cell(cell_index);
451 return that.display_view_in_cell(cell, model);
452 }));
453 }));
454 });
455 }).catch(utils.reject('Could not set widget manager state.', true));
464 };
456 };
465
457
466 WidgetManager.prototype._get_connected_kernel = function() {
458 WidgetManager.prototype._get_connected_kernel = function() {
@@ -469,8 +461,8 b' define(['
469 */
461 */
470 var that = this;
462 var that = this;
471 return new Promise(function(resolve, reject) {
463 return new Promise(function(resolve, reject) {
472 if (that.comm_manager &&
464 if (that.comm_manager &&
473 that.comm_manager.kernel &&
465 that.comm_manager.kernel &&
474 that.comm_manager.kernel.is_connected()) {
466 that.comm_manager.kernel.is_connected()) {
475
467
476 resolve(that.comm_manager.kernel);
468 resolve(that.comm_manager.kernel);
@@ -478,7 +470,7 b' define(['
478 that.notebook.events.on('kernel_connected.Kernel', function(event, data) {
470 that.notebook.events.on('kernel_connected.Kernel', function(event, data) {
479 resolve(data.kernel);
471 resolve(data.kernel);
480 });
472 });
481 }
473 }
482 });
474 });
483 };
475 };
484
476
General Comments 0
You need to be logged in to leave comments. Login now