Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamically load IPyWidgets 7 #21

Merged
merged 12 commits into from
Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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",
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Introduced some API changes (changes to how the script is loaded an the API to communicate with this)
Hence the major bump

"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;
};
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dead code


// 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