Skip to content

Commit

Permalink
Fix widget state saving logic for JupyterLab 4
Browse files Browse the repository at this point in the history
  • Loading branch information
martinRenou committed Aug 22, 2024
1 parent 5bc4379 commit e7e001d
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions jupyterlab_widgets/src/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,19 @@ class WidgetManager extends ManagerBase<Widget> implements IDisposable {
*/
private _saveState() {
const state = this.get_state_sync({ drop_defaults: true });
this._context.model.metadata.set('widgets', {
'application/vnd.jupyter.widget-state+json' : state
});
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore JupyterLab 4 support
if (this._context.model.setMetadata) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore JupyterLab 4 support
this._context.model.setMetadata('widgets', {
'application/vnd.jupyter.widget-state+json': state,
});
} else {
this._context.model.metadata.set('widgets', {
'application/vnd.jupyter.widget-state+json' : state
});
}
}

/**
Expand Down Expand Up @@ -228,7 +238,13 @@ class WidgetManager extends ManagerBase<Widget> implements IDisposable {
* Load widget state from notebook metadata
*/
async _loadFromNotebook(notebook: INotebookModel): Promise<void> {
const widget_md = notebook.metadata.get('widgets') as any;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore JupyterLab 4 support
const widget_md = notebook.getMetadata
? // eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore JupyterLab 4 support
(notebook.getMetadata('widgets') as any)
: notebook.metadata.get('widgets');
// Restore any widgets from saved state that are not live
if (widget_md && widget_md[WIDGET_STATE_MIMETYPE]) {
let state = widget_md[WIDGET_STATE_MIMETYPE];
Expand Down

0 comments on commit e7e001d

Please sign in to comment.