Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

7.x: Fix widget state saving logic for JupyterLab 4 #3943

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading