From 065c484d49d745f99397a5de6df7805287050d2e Mon Sep 17 00:00:00 2001 From: martinRenou Date: Tue, 20 Jun 2023 10:00:46 +0200 Subject: [PATCH] Make sure to support JupyterLab 3 --- python/jupyterlab_widgets/src/manager.ts | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/python/jupyterlab_widgets/src/manager.ts b/python/jupyterlab_widgets/src/manager.ts index a0c1274ff6..9ee14d76fc 100644 --- a/python/jupyterlab_widgets/src/manager.ts +++ b/python/jupyterlab_widgets/src/manager.ts @@ -455,9 +455,17 @@ export class WidgetManager extends LabWidgetManager { */ private _saveState(): void { const state = this.get_state_sync({ drop_defaults: true }); - this._context.model.setMetadata('widgets', { - 'application/vnd.jupyter.widget-state+json': state, - }); + if (this._context.model.setMetadata) { + this._context.model.setMetadata('widgets', { + 'application/vnd.jupyter.widget-state+json': state, + }); + } else { + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore JupyterLab 3 support + this._context.model.metadata.set('widgets', { + 'application/vnd.jupyter.widget-state+json': state, + }); + } } _handleKernelConnectionStatusChange(status: Kernel.ConnectionStatus): void { @@ -512,7 +520,11 @@ export class WidgetManager extends LabWidgetManager { * Load widget state from notebook metadata */ async _loadFromNotebook(notebook: INotebookModel): Promise { - const widget_md = notebook.getMetadata('widgets') as any; + const widget_md = notebook.getMetadata + ? (notebook.getMetadata('widgets') as any) + : // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore JupyterLab 3 support + 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];