Skip to content

Commit

Permalink
Dynamically load IPyWidgets 7 (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
DonJayamanne authored Feb 7, 2023
1 parent 157f013 commit bbf1ab0
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 259 deletions.
1 change: 1 addition & 0 deletions .azure-pipelines/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ trigger:
branches:
include:
- main
- release/ipywidgets7.2
pr: none

resources:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ npm-debug.log
bin/**
obj/**
tmp/**
tsconfig.tsbuildinfo
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@vscode/jupyter-ipywidgets7",
"version": "1.0.3",
"version": "2.0.0",
"main": "dist/ipywidgets.js",
"license": "MIT",
"repository": {
Expand Down
119 changes: 0 additions & 119 deletions src/embed.ts

This file was deleted.

84 changes: 47 additions & 37 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,60 @@
import * as base from '@jupyter-widgets/base';
import * as widgets from '@jupyter-widgets/controls';
import * as outputWidgets from '@jupyter-widgets/jupyterlab-manager/lib/output';
import * as embed from './embed';
import { WidgetManager } from './manager';
import './widgets.css';

type KernelMessagingApi = {
postKernelMessage: (data: unknown) => void;
};

// Default logger is console.log
let logger = console.log;
let loaded = false;
function load() {
if (loaded) {
console.warn('Already loaded ipywidgets7');
return;
}
require('./widgets.css');
require('../node_modules/@fortawesome/fontawesome-free/css/all.min.css');
// Export the following for `requirejs`.
// eslint-disable-next-line @typescript-eslint/no-explicit-any, no-empty,@typescript-eslint/no-empty-function
const define = (window as any).define || function () {};
define('@jupyter-widgets/controls', () => widgets);
define('@jupyter-widgets/base', () => base);
define('@jupyter-widgets/output', () => outputWidgets);

// Export the following for `requirejs`.
// eslint-disable-next-line @typescript-eslint/no-explicit-any, no-empty,@typescript-eslint/no-empty-function
const define = (window as any).define || function () {};
define('@jupyter-widgets/controls', () => widgets);
define('@jupyter-widgets/base', () => base);
define('@jupyter-widgets/output', () => outputWidgets);
// Create our window export. Necessary for the ipywidget code loading in the output to find our widget manager
// Jupyter extension will look for the WidgetManager in this object.

// Render existing widgets without a kernel and pull in the correct css files
// This is not done yet. See this issue here: https://github.com/microsoft/vscode-python/issues/10794
// Likely we'll do this in a different spot.
if (document.readyState === 'complete') {
embed.renderWidgets(document.documentElement, logger);
} else {
window.addEventListener('load', () => {
embed.renderWidgets(document.documentElement, logger);
});
// tslint:disable-next-line: no-any
(window as any).vscIPyWidgets = {
WidgetManager
};
loaded = true;
}
function unload() {
if (!loaded) {
return;
}

// Create our window export. Necessary for the ipywidget code loading in the output to find our widget manager
// tslint:disable-next-line: no-any
(window as any).vscIPyWidgets = {
WidgetManager
};
try {
const undef = (window as any).undef || function () {};
undef('@jupyter-widgets/controls');
undef('@jupyter-widgets/base');
undef('@jupyter-widgets/output');
loaded = false;
} catch (e) {
console.warn(`Failed to unload IPYWidgets 7`, e);
}
}

// Has to be this form for VS code to load it correctly
export function activate(context?: KernelMessagingApi) {
// Setup the logger function
if (context) {
logger = (msg) => {
context.postKernelMessage({
type: 'IPyWidgets_logMessage',
payload: msg
});
/**
* Activate function is required by VS Code.
*/
export function activate() {
// Expose an object with load and unload functions.
// So that we can dynamically load and unload them.
// This script file along with the ipywidgets 8 version will be loaded into the same webpage
// However depending on the version of IPyWidgets we load only one of them.
if (!(window as any).vscIPyWidgets7) {
(window as any).vscIPyWidgets7 = {
load,
unload
};
}
}
91 changes: 0 additions & 91 deletions src/libembed.ts

This file was deleted.

Loading

0 comments on commit bbf1ab0

Please sign in to comment.