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