Skip to content

Commit

Permalink
Iterate
Browse files Browse the repository at this point in the history
  • Loading branch information
martinRenou committed Jul 15, 2024
1 parent e1732b0 commit ea04b47
Show file tree
Hide file tree
Showing 7 changed files with 976 additions and 10 deletions.
1 change: 1 addition & 0 deletions packages/voila/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"@jupyterlab/apputils-extension": "^4.0.0",
"@jupyterlab/codemirror": "^4.0.3",
"@jupyterlab/codemirror-extension": "^4.0.0",
"@jupyterlab/console": "^4.0.0",
"@jupyterlab/coreutils": "^6.0.0",
"@jupyterlab/docregistry": "^4.0.0",
"@jupyterlab/javascript-extension": "^4.0.0",
Expand Down
76 changes: 76 additions & 0 deletions packages/voila/src/ipywidgets8.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/***************************************************************************
* Copyright (c) 2024, Voilà contributors *
* Copyright (c) 2024, QuantStack *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/

import * as base from '@jupyter-widgets/base';
import { JUPYTER_CONTROLS_VERSION } from '@jupyter-widgets/controls/lib/version';
import {
JupyterFrontEnd,
JupyterFrontEndPlugin
} from '@jupyterlab/application';

/**
* The base widgets.
*/
export const baseWidgets8Plugin: JupyterFrontEndPlugin<void> = {
id: `@jupyter-widgets/jupyterlab-manager:base-${base.JUPYTER_WIDGETS_VERSION}`,
requires: [base.IJupyterWidgetRegistry],
autoStart: true,
activate: (
app: JupyterFrontEnd,
registry: base.IJupyterWidgetRegistry
): void => {
registry.registerWidget({
name: '@jupyter-widgets/base',
version: base.JUPYTER_WIDGETS_VERSION,
exports: {
WidgetModel: base.WidgetModel as any,
WidgetView: base.WidgetView as any,
DOMWidgetView: base.DOMWidgetView as any,
DOMWidgetModel: base.DOMWidgetModel as any,
LayoutModel: base.LayoutModel as any,
LayoutView: base.LayoutView as any,
StyleModel: base.StyleModel as any,
StyleView: base.StyleView as any
}
});
}
};

/**
* The control widgets.
*/
export const controlWidgets8Plugin: JupyterFrontEndPlugin<void> = {
id: `@jupyter-widgets/jupyterlab-manager:controls-${JUPYTER_CONTROLS_VERSION}`,
requires: [base.IJupyterWidgetRegistry],
autoStart: true,
activate: (
app: JupyterFrontEnd,
registry: base.IJupyterWidgetRegistry
): void => {
registry.registerWidget({
name: '@jupyter-widgets/controls',
version: JUPYTER_CONTROLS_VERSION,
exports: () => {
return new Promise((resolve, reject) => {
(require as any).ensure(
['@jupyter-widgets/controls'],
(require: NodeRequire) => {
// eslint-disable-next-line @typescript-eslint/no-var-requires
resolve(require('@jupyter-widgets/controls'));
},
(err: any) => {
reject(err);
},
'@jupyter-widgets/controls'
);
});
}
});
}
};
17 changes: 10 additions & 7 deletions packages/voila/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ import {
activePlugins,
createModule,
isIpywidgets7extension,
isIpywidgets8extension,
loadComponent,
shouldUseMathJax2
} from './tools';
import { baseWidgets8Plugin, controlWidgets8Plugin } from './ipywidgets8';

//Inspired by: https://github.com/jupyterlab/jupyterlab/blob/master/dev_mode/index.js

Expand Down Expand Up @@ -87,7 +89,6 @@ async function main() {
}

const data = p.value;
console.log('LOADING EXTENSION ', data.name);

if (data.extension) {
federatedExtensionPromises.push(createModule(data.name, data.extension));
Expand All @@ -109,20 +110,22 @@ async function main() {
federatedExtensions.forEach((p) => {
if (p.status === 'fulfilled') {
const plugins = p.value;

// Special case for ipywidgets
// Case for ipywidgets 7 federated ext: we disabled the entire @jupyter-widgets/jupyterlab-manager and mock it
// Case for ipywidgets 8 federated ext: we load all plugins but @jupyter-widgets/jupyterlab-manager:plugin
if (isIpywidgets7extension(plugins)) {
mods.push(baseWidgets7Plugin);
mods.push(controlWidgets7Plugin);

return;
}
if (isIpywidgets8extension(plugins)) {
mods.push(baseWidgets8Plugin);
mods.push(controlWidgets8Plugin);

return;
}

// Whichever the ipywidgets version, we disable @jupyter-widgets/jupyterlab-manager:plugin
for (const plugin of activePlugins(plugins, [
'@jupyter-widgets/jupyterlab-manager:plugin'
])) {
for (const plugin of activePlugins(plugins, [])) {
mods.push(plugin);
}
} else {
Expand Down
4 changes: 4 additions & 0 deletions packages/voila/src/sharedscope.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Ideally we would remove this but the ipywidgets labextension requires it
import '@jupyterlab/notebook';
import '@jupyterlab/console';

import '@lumino/algorithm';
import '@lumino/application';
import '@lumino/coreutils';
Expand Down
21 changes: 21 additions & 0 deletions packages/voila/src/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,27 @@ export function isIpywidgets7extension(extension: any) {
);
}

export function isIpywidgets8extension(extension: any) {
// Handle commonjs or es2015 modules
let exports;
if (Object.prototype.hasOwnProperty.call(extension, '__esModule')) {
exports = extension.default;
} else {
// CommonJS exports.
exports = extension;
}

const plugins = Array.isArray(exports) ? exports : [exports];
const pluginIds = plugins.map((plugin) => {
return plugin.id;
});

return (
pluginIds.includes('@jupyter-widgets/jupyterlab-manager:plugin') &&
pluginIds.length !== 1
);
}

/**
* Iterate over active plugins in an extension.
*
Expand Down
1 change: 1 addition & 0 deletions packages/voila/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ const distRoot = path.resolve(

const shared = {};
for (const dependency of Object.keys(data.dependencies)) {
// TODO Why can we not share those?
if (
['@jupyter-widgets/base7', '@jupyter-widgets/controls7'].includes(
dependency
Expand Down
Loading

0 comments on commit ea04b47

Please sign in to comment.