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

Passing the link of empack_env_meta.json file #120

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
32 changes: 27 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { IBroadcastChannelWrapper } from '@jupyterlite/contents';
import { IKernel, IKernelSpecs } from '@jupyterlite/kernel';

import { WebWorkerKernel } from './web_worker_kernel';
import { IEmpackEnvMetaFile } from './tokens';

function getJson(url: string) {
const json_url = URLExt.join(PageConfig.getBaseUrl(), url);
Expand All @@ -29,17 +30,18 @@ try {
throw err;
}

const plugins = kernel_list.map((kernel): JupyterLiteServerPlugin<void> => {
const plugins = kernel_list.map((kernel): JupyterLiteServerPlugin<void| IEmpackEnvMetaFile> => {
return {
id: `@jupyterlite/xeus-${kernel}:register`,
autoStart: true,
requires: [IKernelSpecs],
optional: [IServiceWorkerManager, IBroadcastChannelWrapper],
optional: [IServiceWorkerManager, IBroadcastChannelWrapper, IEmpackEnvMetaFile],
activate: (
app: JupyterLiteServer,
kernelspecs: IKernelSpecs,
serviceWorker?: IServiceWorkerManager,
broadcastChannel?: IBroadcastChannelWrapper
broadcastChannel?: IBroadcastChannelWrapper,
empackEnvMetaFile?: IEmpackEnvMetaFile
) => {
// Fetch kernel spec
const kernelspec = getJson('xeus/kernels/' + kernel + '/kernel.json');
Expand All @@ -53,7 +55,6 @@ const plugins = kernel_list.map((kernel): JupyterLiteServerPlugin<void> => {
}

const contentsManager = app.serviceManager.contents;

kernelspecs.register({
spec: kernelspec,
create: async (options: IKernel.IOptions): Promise<IKernel> => {
Expand All @@ -71,17 +72,38 @@ const plugins = kernel_list.map((kernel): JupyterLiteServerPlugin<void> => {
`${kernelspec.name} contents will NOT be synced with Jupyter Contents`
);
}
const link = empackEnvMetaFile ? await empackEnvMetaFile.getLink(kernelspec.dir): '';

return new WebWorkerKernel({
...options,
contentsManager,
mountDrive,
kernelSpec: kernelspec
kernelSpec: kernelspec,
empackEnvMetaLink: link
});
}
});
}
};
});

const empackEnvMetaPlugin: JupyterLiteServerPlugin<IEmpackEnvMetaFile> = {
id: `@jupyterlite/xeus-python:empack-env-meta`,
autoStart: true,
provides: IEmpackEnvMetaFile,
activate: (): IEmpackEnvMetaFile => {
return {
getLink: async (kernel?: string) => {
const kernel_root_url = URLExt.join(
Copy link
Member

Choose a reason for hiding this comment

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

👌🏽

PageConfig.getBaseUrl(),
`xeus/kernels/${kernel}`
);
return `${kernel_root_url}`;
}
};
},
};

plugins.push(empackEnvMetaPlugin);

export default plugins;
11 changes: 11 additions & 0 deletions src/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
} from '@jupyterlite/contents';

import { IWorkerKernel } from '@jupyterlite/kernel';
import { Token } from '@lumino/coreutils';

/**
* An interface for Xeus workers.
Expand Down Expand Up @@ -85,5 +86,15 @@ export namespace IXeusWorkerKernel {
baseUrl: string;
kernelSpec: any;
mountDrive: boolean;
empackEnvMetaLink?: string;
}
}

export interface IEmpackEnvMetaFile {
/**
* Get empack_env_meta link.
*/
getLink: (kernel?: string) => Promise<string>;
}

export const IEmpackEnvMetaFile = new Token<IEmpackEnvMetaFile>('@jupyterlite/xeus-python:IEmpackEnvMetaFile');
10 changes: 7 additions & 3 deletions src/web_worker_kernel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@ export class WebWorkerKernel implements IKernel {
* @param options The instantiation options for a new WebWorkerKernel
*/
constructor(options: WebWorkerKernel.IOptions) {
const { id, name, sendMessage, location, kernelSpec, contentsManager } =
const { id, name, sendMessage, location, kernelSpec, contentsManager, empackEnvMetaLink } =
options;
this._id = id;
this._name = name;
this._location = location;
this._kernelSpec = kernelSpec;
this._contentsManager = contentsManager;
this._sendMessage = sendMessage;
this._empackEnvMetaLink = empackEnvMetaLink;
this._worker = this.initWorker(options);
this._remoteKernel = this.initRemote(options);

this.initFileSystem(options);
}

Expand Down Expand Up @@ -99,11 +99,13 @@ export class WebWorkerKernel implements IKernel {
};
remote = wrap(this._worker) as Remote<IXeusWorkerKernel>;
}

remote
.initialize({
kernelSpec: this._kernelSpec,
baseUrl: PageConfig.getBaseUrl(),
mountDrive: options.mountDrive
mountDrive: options.mountDrive,
empackEnvMetaLink: this._empackEnvMetaLink,
})
.then(this._ready.resolve.bind(this._ready));

Expand Down Expand Up @@ -290,6 +292,7 @@ export class WebWorkerKernel implements IKernel {
| undefined = undefined;
private _parent: KernelMessage.IMessage | undefined = undefined;
private _ready = new PromiseDelegate<void>();
private _empackEnvMetaLink: string | undefined;
}

/**
Expand All @@ -303,5 +306,6 @@ export namespace WebWorkerKernel {
contentsManager: Contents.IManager;
mountDrive: boolean;
kernelSpec: any;
empackEnvMetaLink?: string | undefined;
}
}
4 changes: 2 additions & 2 deletions src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export class XeusRemoteKernel {
}

async initialize(options: IXeusWorkerKernel.IOptions): Promise<void> {
const { baseUrl, kernelSpec } = options;
const { baseUrl, kernelSpec, empackEnvMetaLink } = options;
// location of the kernel binary on the server
const binary_js = URLExt.join(baseUrl, kernelSpec.argv[0]);
const binary_wasm = binary_js.replace('.js', '.wasm');
Expand All @@ -125,7 +125,7 @@ export class XeusRemoteKernel {
// This function is usually implemented in the pre/post.js
// in the emscripten build of that kernel
if (globalThis.Module['async_init'] !== undefined) {
const kernel_root_url = URLExt.join(
const kernel_root_url = empackEnvMetaLink ? empackEnvMetaLink : URLExt.join(
baseUrl,
`xeus/kernels/${kernelSpec.dir}`
);
Expand Down
Loading