From a66d36209e4e2c08ad83ef45dc4ce7e6a4ab33ef Mon Sep 17 00:00:00 2001 From: martinRenou Date: Thu, 23 May 2024 15:43:16 +0200 Subject: [PATCH] Backward compatibility --- python/jupyterlab_widgets/src/plugin.ts | 56 +++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 3 deletions(-) diff --git a/python/jupyterlab_widgets/src/plugin.ts b/python/jupyterlab_widgets/src/plugin.ts index 7eca855210..8dd7d6c5d4 100644 --- a/python/jupyterlab_widgets/src/plugin.ts +++ b/python/jupyterlab_widgets/src/plugin.ts @@ -3,6 +3,8 @@ import { ISettingRegistry } from '@jupyterlab/settingregistry'; +import { DocumentRegistry } from '@jupyterlab/docregistry'; + import * as nbformat from '@jupyterlab/nbformat'; import { @@ -12,6 +14,7 @@ import { } from '@jupyterlab/console'; import { + INotebookModel, INotebookTracker, Notebook, NotebookPanel, @@ -224,7 +227,54 @@ async function registerWidgetHandler( }); } -export async function registerWidgetManager( +// Kept for backward compat ipywidgets<=8, but not used here anymore +export function registerWidgetManager( + context: DocumentRegistry.IContext, + rendermime: IRenderMimeRegistry, + renderers: IterableIterator +): DisposableDelegate { + let wManager: WidgetManager; + const managerReady = getWidgetManagerOwner(context.sessionContext).then( + (wManagerOwner) => { + const currentManager = Private.widgetManagerProperty.get( + wManagerOwner + ) as WidgetManager; + if (!currentManager) { + wManager = new WidgetManager(context, rendermime, SETTINGS); + WIDGET_REGISTRY.forEach((data) => wManager!.register(data)); + Private.widgetManagerProperty.set(wManagerOwner, wManager); + } else { + wManager = currentManager; + } + + for (const r of renderers) { + r.manager = wManager; + } + + // Replace the placeholder widget renderer with one bound to this widget + // manager. + rendermime.removeMimeType(WIDGET_VIEW_MIMETYPE); + rendermime.addFactory( + { + safe: false, + mimeTypes: [WIDGET_VIEW_MIMETYPE], + createRenderer: (options) => new WidgetRenderer(options, wManager), + }, + -10 + ); + } + ); + + return new DisposableDelegate(async () => { + await managerReady; + if (rendermime) { + rendermime.removeMimeType(WIDGET_VIEW_MIMETYPE); + } + wManager!.dispose(); + }); +} + +export async function registerNotebookWidgetManager( panel: NotebookPanel, renderers: IterableIterator ): Promise { @@ -367,11 +417,11 @@ function activateWidgetExtension( outputViews(app, panel.context.path) ); tracker.forEach(async (panel) => { - await registerWidgetManager(panel, rendererIterator(panel)); + await registerNotebookWidgetManager(panel, rendererIterator(panel)); bindUnhandledIOPubMessageSignal(panel); }); tracker.widgetAdded.connect(async (sender, panel) => { - await registerWidgetManager(panel, rendererIterator(panel)); + await registerNotebookWidgetManager(panel, rendererIterator(panel)); bindUnhandledIOPubMessageSignal(panel); }); }