Skip to content

Commit 84031ad

Browse files
committed
Fix state.state errors.
Thanks to @maartenbreddels for his changes at jasongrout#1
1 parent b68ca45 commit 84031ad

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

jupyter-js-widgets/src/manager-base.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -405,22 +405,30 @@ abstract class ManagerBase<T> {
405405
/**
406406
* Set the widget manager state.
407407
*
408+
* @param state - a Javascript object conforming to the application/vnd.jupyter.widget-state+json spec.
409+
*
408410
* Reconstructs all of the widget models in the state, merges that with the
409411
* current manager state, and then attempts to redisplay the widgets in the
410412
* state.
411413
*/
412414
set_state(state, displayOptions) {
415+
416+
// Check to make sure that it's the same version we are parsing.
417+
if (!(state.version_major && state.version_major <= 2)) {
418+
throw "Unsupported widget state format";
419+
}
420+
let models = state.state;
413421
// Recreate all the widget models for the given widget manager state.
414422
let all_models = this._get_comm_info().then(live_comms => {
415-
return Promise.all(Object.keys(state).map(model_id => {
423+
return Promise.all(Object.keys(models).map(model_id => {
416424

417425
// First put back the binary buffers
418426
let decode = {'base64': toByteArray, 'hex': utils.hexToBuffer};
419-
let modelState = state[model_id].state;
427+
let modelState = models[model_id].state;
420428
if (modelState.buffers) {
421429
let bufferPaths = modelState.buffers.map(b => b.path);
422430
let buffers = modelState.buffers.map(b => decode[b.encoding](b.data));
423-
utils.put_buffers(modelState, bufferPaths, buffers);
431+
utils.put_buffers(modelState.state, bufferPaths, buffers);
424432
}
425433

426434
// If the model has already been created, set its state and then
@@ -439,17 +447,17 @@ abstract class ManagerBase<T> {
439447
return this._create_comm(this.comm_target_name, model_id).then(new_comm => {
440448
return this.new_model({
441449
comm: new_comm,
442-
model_name: state[model_id].model_name,
443-
model_module: state[model_id].model_module,
444-
model_module_version: state[model_id].model_module_version
450+
model_name: models[model_id].model_name,
451+
model_module: models[model_id].model_module,
452+
model_module_version: models[model_id].model_module_version
445453
});
446454
});
447455
} else { // dead comm
448456
return this.new_model({
449457
model_id: model_id,
450-
model_name: state[model_id].model_name,
451-
model_module: state[model_id].model_module,
452-
model_module_version: state[model_id].model_module_version
458+
model_name: models[model_id].model_name,
459+
model_module: models[model_id].model_module,
460+
model_module_version: models[model_id].model_module_version
453461
}, modelState);
454462
}
455463
}));

0 commit comments

Comments
 (0)