-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathindex.ts
More file actions
118 lines (98 loc) · 3.57 KB
/
index.ts
File metadata and controls
118 lines (98 loc) · 3.57 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// 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.27.2/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 contentsManager = app.serviceManager.contents;
const config =
JSON.parse(PageConfig.getOption('litePluginSettings') || '{}')[PLUGIN_ID] || {};
const baseUrl = PageConfig.getBaseUrl();
const url = config.pyodideUrl || PYODIDE_CDN_URL;
const pyodideUrl = URLExt.parse(url).href;
const pipliteWheelUrl = config.pipliteWheelUrl
? URLExt.parse(config.pipliteWheelUrl).href
: undefined;
const rawPipUrls = config.pipliteUrls || [];
const pipliteUrls = rawPipUrls.map((pipUrl: string) => URLExt.parse(pipUrl).href);
const disablePyPIFallback = !!config.disablePyPIFallback;
const loadPyodideOptions = config.loadPyodideOptions || {};
const pipliteInstallDefaultOptions = config.pipliteInstallDefaultOptions || {};
// Parse any configured index URLs
const indexUrls = pipliteInstallDefaultOptions.indexUrls || [];
for (const [key, value] of Object.entries(loadPyodideOptions)) {
if (key.endsWith('URL') && typeof value === 'string') {
loadPyodideOptions[key] = new URL(value, baseUrl).href;
}
}
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) ||
crossOriginIsolated
);
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,
pipliteWheelUrl,
pipliteUrls,
disablePyPIFallback,
mountDrive,
loadPyodideOptions,
contentsManager,
pipliteInstallDefaultOptions: {
indexUrls,
...pipliteInstallDefaultOptions,
},
});
},
});
},
};
const plugins: JupyterLiteServerPlugin<any>[] = [kernel];
export default plugins;