Skip to content

Commit 0d403ef

Browse files
authored
Fix widget state saving logic for JupyterLab 4 (#3943)
1 parent 5bc4379 commit 0d403ef

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

jupyterlab_widgets/src/manager.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,19 @@ class WidgetManager extends ManagerBase<Widget> implements IDisposable {
139139
*/
140140
private _saveState() {
141141
const state = this.get_state_sync({ drop_defaults: true });
142-
this._context.model.metadata.set('widgets', {
143-
'application/vnd.jupyter.widget-state+json' : state
144-
});
142+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
143+
// @ts-ignore JupyterLab 4 support
144+
if (this._context.model.setMetadata) {
145+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
146+
// @ts-ignore JupyterLab 4 support
147+
this._context.model.setMetadata('widgets', {
148+
'application/vnd.jupyter.widget-state+json': state,
149+
});
150+
} else {
151+
this._context.model.metadata.set('widgets', {
152+
'application/vnd.jupyter.widget-state+json' : state
153+
});
154+
}
145155
}
146156

147157
/**
@@ -228,7 +238,13 @@ class WidgetManager extends ManagerBase<Widget> implements IDisposable {
228238
* Load widget state from notebook metadata
229239
*/
230240
async _loadFromNotebook(notebook: INotebookModel): Promise<void> {
231-
const widget_md = notebook.metadata.get('widgets') as any;
241+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
242+
// @ts-ignore JupyterLab 4 support
243+
const widget_md = notebook.getMetadata
244+
? // eslint-disable-next-line @typescript-eslint/ban-ts-comment
245+
// @ts-ignore JupyterLab 4 support
246+
(notebook.getMetadata('widgets') as any)
247+
: notebook.metadata.get('widgets');
232248
// Restore any widgets from saved state that are not live
233249
if (widget_md && widget_md[WIDGET_STATE_MIMETYPE]) {
234250
let state = widget_md[WIDGET_STATE_MIMETYPE];

0 commit comments

Comments
 (0)