Skip to content

Commit 0c68668

Browse files
committed
Add XeusAddon.package_url_factory config
1 parent f061568 commit 0c68668

File tree

5 files changed

+94
-69
lines changed

5 files changed

+94
-69
lines changed

jupyterlite_xeus/add_on.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""a JupyterLite addon for creating the env for xeus kernels"""
2+
23
import json
34
import os
45
from pathlib import Path
@@ -18,7 +19,7 @@
1819
SHARE_LABEXTENSIONS,
1920
UTF8,
2021
)
21-
from traitlets import Bool, List, Unicode
22+
from traitlets import Bool, Callable, List, Unicode
2223

2324
from .create_conda_env import (
2425
create_conda_env_from_env_file,
@@ -102,6 +103,13 @@ class XeusAddon(FederatedExtensionAddon):
102103
description="A comma-separated list of mount points, in the form <host_path>:<mount_path> to mount in the wasm prefix",
103104
)
104105

106+
package_url_factory = Callable(
107+
None,
108+
allow_none=True,
109+
config=True,
110+
description="Factory to generate package download URL from package metadata. This is used to load python packages from external host",
111+
)
112+
105113
def __init__(self, *args, **kwargs):
106114
super().__init__(*args, **kwargs)
107115
self.xeus_output_dir = Path(self.manager.output_dir) / "xeus"
@@ -290,6 +298,7 @@ def pack_prefix(self, kernel_dir):
290298
relocate_prefix="/",
291299
outdir=out_path,
292300
use_cache=False,
301+
package_url_factory=self.package_url_factory,
293302
**pack_kwargs,
294303
)
295304

@@ -430,4 +439,4 @@ def dedupe_federated_extensions(self, config):
430439
if os.path.exists(self.output_extensions / ext["name"] / ext["load"]):
431440
named[ext["name"]] = ext
432441

433-
config[FEDERATED_EXTENSIONS] = sorted(named.values(), key=lambda x: x["name"])
442+
config[FEDERATED_EXTENSIONS] = sorted(named.values(), key=lambda x: x["name"])

src/index.ts

Lines changed: 67 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -30,78 +30,86 @@ try {
3030
throw err;
3131
}
3232

33-
const plugins = kernel_list.map((kernel): JupyterLiteServerPlugin<void| IEmpackEnvMetaFile> => {
34-
return {
35-
id: `@jupyterlite/xeus-${kernel}:register`,
36-
autoStart: true,
37-
requires: [IKernelSpecs],
38-
optional: [IServiceWorkerManager, IBroadcastChannelWrapper, IEmpackEnvMetaFile],
39-
activate: (
40-
app: JupyterLiteServer,
41-
kernelspecs: IKernelSpecs,
42-
serviceWorker?: IServiceWorkerManager,
43-
broadcastChannel?: IBroadcastChannelWrapper,
44-
empackEnvMetaFile?: IEmpackEnvMetaFile
45-
) => {
46-
// Fetch kernel spec
47-
const kernelspec = getJson('xeus/kernels/' + kernel + '/kernel.json');
48-
kernelspec.name = kernel;
49-
kernelspec.dir = kernel;
50-
for (const [key, value] of Object.entries(kernelspec.resources)) {
51-
kernelspec.resources[key] = URLExt.join(
52-
PageConfig.getBaseUrl(),
53-
value as string
54-
);
55-
}
56-
57-
const contentsManager = app.serviceManager.contents;
58-
kernelspecs.register({
59-
spec: kernelspec,
60-
create: async (options: IKernel.IOptions): Promise<IKernel> => {
61-
const mountDrive = !!(
62-
(serviceWorker?.enabled && broadcastChannel?.enabled) ||
63-
crossOriginIsolated
33+
const plugins = kernel_list.map(
34+
(kernel): JupyterLiteServerPlugin<void | IEmpackEnvMetaFile> => {
35+
return {
36+
id: `@jupyterlite/xeus-${kernel}:register`,
37+
autoStart: true,
38+
requires: [IKernelSpecs],
39+
optional: [
40+
IServiceWorkerManager,
41+
IBroadcastChannelWrapper,
42+
IEmpackEnvMetaFile
43+
],
44+
activate: (
45+
app: JupyterLiteServer,
46+
kernelspecs: IKernelSpecs,
47+
serviceWorker?: IServiceWorkerManager,
48+
broadcastChannel?: IBroadcastChannelWrapper,
49+
empackEnvMetaFile?: IEmpackEnvMetaFile
50+
) => {
51+
// Fetch kernel spec
52+
const kernelspec = getJson('xeus/kernels/' + kernel + '/kernel.json');
53+
kernelspec.name = kernel;
54+
kernelspec.dir = kernel;
55+
for (const [key, value] of Object.entries(kernelspec.resources)) {
56+
kernelspec.resources[key] = URLExt.join(
57+
PageConfig.getBaseUrl(),
58+
value as string
6459
);
60+
}
6561

66-
if (mountDrive) {
67-
console.info(
68-
`${kernelspec.name} contents will be synced with Jupyter Contents`
62+
const contentsManager = app.serviceManager.contents;
63+
kernelspecs.register({
64+
spec: kernelspec,
65+
create: async (options: IKernel.IOptions): Promise<IKernel> => {
66+
const mountDrive = !!(
67+
(serviceWorker?.enabled && broadcastChannel?.enabled) ||
68+
crossOriginIsolated
6969
);
70-
} else {
71-
console.warn(
72-
`${kernelspec.name} contents will NOT be synced with Jupyter Contents`
73-
);
74-
}
75-
const link = empackEnvMetaFile ? await empackEnvMetaFile.getLink(kernelspec.dir): '';
7670

77-
return new WebWorkerKernel({
78-
...options,
79-
contentsManager,
80-
mountDrive,
81-
kernelSpec: kernelspec,
82-
empackEnvMetaLink: link
83-
});
84-
}
85-
});
86-
}
87-
};
88-
});
71+
if (mountDrive) {
72+
console.info(
73+
`${kernelspec.name} contents will be synced with Jupyter Contents`
74+
);
75+
} else {
76+
console.warn(
77+
`${kernelspec.name} contents will NOT be synced with Jupyter Contents`
78+
);
79+
}
80+
const link = empackEnvMetaFile
81+
? await empackEnvMetaFile.getLink(kernelspec.dir)
82+
: '';
83+
84+
return new WebWorkerKernel({
85+
...options,
86+
contentsManager,
87+
mountDrive,
88+
kernelSpec: kernelspec,
89+
empackEnvMetaLink: link
90+
});
91+
}
92+
});
93+
}
94+
};
95+
}
96+
);
8997

9098
const empackEnvMetaPlugin: JupyterLiteServerPlugin<IEmpackEnvMetaFile> = {
91-
id: `@jupyterlite/xeus-python:empack-env-meta`,
99+
id: '@jupyterlite/xeus-python:empack-env-meta',
92100
autoStart: true,
93101
provides: IEmpackEnvMetaFile,
94102
activate: (): IEmpackEnvMetaFile => {
95103
return {
96104
getLink: async (kernel?: string) => {
97-
const kernel_root_url = URLExt.join(
98-
PageConfig.getBaseUrl(),
99-
`xeus/kernels/${kernel}`
100-
);
101-
return `${kernel_root_url}`;
105+
const kernel_root_url = URLExt.join(
106+
PageConfig.getBaseUrl(),
107+
`xeus/kernels/${kernel}`
108+
);
109+
return `${kernel_root_url}`;
102110
}
103111
};
104-
},
112+
}
105113
};
106114

107115
plugins.push(empackEnvMetaPlugin);

src/tokens.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,6 @@ export interface IEmpackEnvMetaFile {
9797
getLink: (kernel?: string) => Promise<string>;
9898
}
9999

100-
export const IEmpackEnvMetaFile = new Token<IEmpackEnvMetaFile>('@jupyterlite/xeus-python:IEmpackEnvMetaFile');
100+
export const IEmpackEnvMetaFile = new Token<IEmpackEnvMetaFile>(
101+
'@jupyterlite/xeus-python:IEmpackEnvMetaFile'
102+
);

src/web_worker_kernel.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,15 @@ export class WebWorkerKernel implements IKernel {
2929
* @param options The instantiation options for a new WebWorkerKernel
3030
*/
3131
constructor(options: WebWorkerKernel.IOptions) {
32-
const { id, name, sendMessage, location, kernelSpec, contentsManager, empackEnvMetaLink } =
33-
options;
32+
const {
33+
id,
34+
name,
35+
sendMessage,
36+
location,
37+
kernelSpec,
38+
contentsManager,
39+
empackEnvMetaLink
40+
} = options;
3441
this._id = id;
3542
this._name = name;
3643
this._location = location;
@@ -105,7 +112,7 @@ export class WebWorkerKernel implements IKernel {
105112
kernelSpec: this._kernelSpec,
106113
baseUrl: PageConfig.getBaseUrl(),
107114
mountDrive: options.mountDrive,
108-
empackEnvMetaLink: this._empackEnvMetaLink,
115+
empackEnvMetaLink: this._empackEnvMetaLink
109116
})
110117
.then(this._ready.resolve.bind(this._ready));
111118

src/worker.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,9 @@ export class XeusRemoteKernel {
125125
// This function is usually implemented in the pre/post.js
126126
// in the emscripten build of that kernel
127127
if (globalThis.Module['async_init'] !== undefined) {
128-
const kernel_root_url = empackEnvMetaLink ? empackEnvMetaLink : URLExt.join(
129-
baseUrl,
130-
`xeus/kernels/${kernelSpec.dir}`
131-
);
128+
const kernel_root_url = empackEnvMetaLink
129+
? empackEnvMetaLink
130+
: URLExt.join(baseUrl, `xeus/kernels/${kernelSpec.dir}`);
132131
const pkg_root_url = URLExt.join(baseUrl, 'xeus/kernel_packages');
133132
const verbose = true;
134133
await globalThis.Module['async_init'](

0 commit comments

Comments
 (0)