-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathindex.ts
93 lines (79 loc) · 2.81 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { PageConfig, URLExt } from '@jupyterlab/coreutils';
import {
IServiceWorkerManager,
JupyterLiteServer,
JupyterLiteServerPlugin,
} from '@jupyterlite/server';
import { IKernel, IKernelSpecs } from '@jupyterlite/kernel';
import { IBroadcastChannelWrapper } from '@jupyterlite/contents';
export * as KERNEL_SETTINGS_SCHEMA from '../schema/kernel.v0.schema.json';
import KERNEL_ICON_SVG_STR from '../style/img/pyodide.svg';
const KERNEL_ICON_URL = `data:image/svg+xml;base64,${btoa(KERNEL_ICON_SVG_STR)}`;
/**
* The default CDN fallback for Pyodide
*/
const PYODIDE_CDN_URL = 'https://cdn.jsdelivr.net/pyodide/v0.22.1/full/pyodide.js';
/**
* The id for the extension, and key in the litePlugins.
*/
const PLUGIN_ID = '@jupyterlite/pyodide-kernel-extension:kernel';
/**
* A plugin to register the Pyodide kernel.
*/
const kernel: JupyterLiteServerPlugin<void> = {
id: PLUGIN_ID,
autoStart: true,
requires: [IKernelSpecs],
optional: [IServiceWorkerManager, IBroadcastChannelWrapper],
activate: (
app: JupyterLiteServer,
kernelspecs: IKernelSpecs,
serviceWorker?: IServiceWorkerManager,
broadcastChannel?: IBroadcastChannelWrapper
) => {
const config =
JSON.parse(PageConfig.getOption('litePluginSettings') || '{}')[PLUGIN_ID] || {};
const url = config.pyodideUrl || PYODIDE_CDN_URL;
const pyodideUrl = URLExt.parse(url).href;
const rawPipUrls = config.pipliteUrls || [];
const rawRepoUrls = config.repodataUrls || [];
const pipliteUrls = rawPipUrls.map((pipUrl: string) => URLExt.parse(pipUrl).href);
const repodataUrls = rawRepoUrls.map(
(repoUrl: string) => URLExt.parse(repoUrl).href
);
const disablePyPIFallback = !!config.disablePyPIFallback;
kernelspecs.register({
spec: {
name: 'python',
display_name: 'Python (Pyodide)',
language: 'python',
argv: [],
resources: {
'logo-32x32': KERNEL_ICON_URL,
'logo-64x64': KERNEL_ICON_URL,
},
},
create: async (options: IKernel.IOptions): Promise<IKernel> => {
const { PyodideKernel } = await import('@jupyterlite/pyodide-kernel');
const mountDrive = !!(serviceWorker?.enabled && broadcastChannel?.enabled);
if (mountDrive) {
console.info('Pyodide contents will be synced with Jupyter Contents');
} else {
console.warn('Pyodide contents will NOT be synced with Jupyter Contents');
}
return new PyodideKernel({
...options,
pyodideUrl,
pipliteUrls,
repodataUrls,
disablePyPIFallback,
mountDrive,
});
},
});
},
};
const plugins: JupyterLiteServerPlugin<any>[] = [kernel];
export default plugins;