Skip to content

Commit

Permalink
Register all kernels in one plugin (#107)
Browse files Browse the repository at this point in the history
* Register all kernels in one plugin

* Refresh spec

* Export `IEmpackEnvMetaFile` token from index

---------

Co-authored-by: martinRenou <martin.renou@gmail.com>
  • Loading branch information
trungleduc and martinRenou authored Dec 13, 2024
1 parent 39e9b2a commit 3a02d65
Showing 1 changed file with 90 additions and 75 deletions.
165 changes: 90 additions & 75 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,86 +14,103 @@ 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);
const xhr = new XMLHttpRequest();
xhr.open('GET', json_url, false);
xhr.send(null);
return JSON.parse(xhr.responseText);
}
/**
* Fetches JSON data from the specified URL asynchronously.
*
* This function constructs the full URL using the base URL from the PageConfig and
* the provided relative URL. It then performs a GET request using the Fetch API
* and returns the parsed JSON data.
*
* @param {string} url - The relative URL to fetch the JSON data from.
* @returns {Promise<any>} - A promise that resolves to the parsed JSON data.
* @throws {Error} - Throws an error if the HTTP request fails.
*
*/
async function getJson(url: string) {
const jsonUrl = URLExt.join(PageConfig.getBaseUrl(), url);
const response = await fetch(jsonUrl, { method: 'GET' });

if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}

let kernel_list: string[] = [];
try {
kernel_list = getJson('xeus/kernels.json');
} catch (err) {
console.log(`Could not fetch xeus/kernels.json: ${err}`);
throw err;
const data = await response.json();
return data;
}

const plugins = kernel_list.map(
(kernel): JupyterLiteServerPlugin<void | IEmpackEnvMetaFile> => {
return {
id: `@jupyterlite/xeus-${kernel}:register`,
autoStart: true,
requires: [IKernelSpecs],
optional: [
IServiceWorkerManager,
IBroadcastChannelWrapper,
IEmpackEnvMetaFile
],
activate: (
app: JupyterLiteServer,
kernelspecs: IKernelSpecs,
serviceWorker?: IServiceWorkerManager,
broadcastChannel?: IBroadcastChannelWrapper,
empackEnvMetaFile?: IEmpackEnvMetaFile
) => {
// Fetch kernel spec
const kernelspec = getJson('xeus/kernels/' + kernel + '/kernel.json');
kernelspec.name = kernel;
kernelspec.dir = kernel;
for (const [key, value] of Object.entries(kernelspec.resources)) {
kernelspec.resources[key] = URLExt.join(
PageConfig.getBaseUrl(),
value as string
const kernelPlugin: JupyterLiteServerPlugin<void> = {
id: '@jupyterlite/xeus-kernel:register',
autoStart: true,
requires: [IKernelSpecs],
optional: [
IServiceWorkerManager,
IBroadcastChannelWrapper,
IEmpackEnvMetaFile
],
activate: async (
app: JupyterLiteServer,
kernelspecs: IKernelSpecs,
serviceWorker?: IServiceWorkerManager,
broadcastChannel?: IBroadcastChannelWrapper,
empackEnvMetaFile?: IEmpackEnvMetaFile
) => {
// Fetch kernel list
let kernelList: string[] = [];
try {
kernelList = await getJson('xeus/kernels.json');
} catch (err) {
console.log(`Could not fetch xeus/kernels.json: ${err}`);
throw err;
}
const contentsManager = app.serviceManager.contents;

for (const kernel of kernelList) {
// Fetch kernel spec
const kernelspec = await getJson(
'xeus/kernels/' + kernel + '/kernel.json'
);
kernelspec.name = kernel;
kernelspec.dir = kernel;
for (const [key, value] of Object.entries(kernelspec.resources)) {
kernelspec.resources[key] = URLExt.join(
PageConfig.getBaseUrl(),
value as string
);
}
kernelspecs.register({
spec: kernelspec,
create: async (options: IKernel.IOptions): Promise<IKernel> => {
const mountDrive = !!(
(serviceWorker?.enabled && broadcastChannel?.enabled) ||
crossOriginIsolated
);
}

const contentsManager = app.serviceManager.contents;
kernelspecs.register({
spec: kernelspec,
create: async (options: IKernel.IOptions): Promise<IKernel> => {
const mountDrive = !!(
(serviceWorker?.enabled && broadcastChannel?.enabled) ||
crossOriginIsolated
if (mountDrive) {
console.info(
`${kernelspec.name} contents will be synced with Jupyter Contents`
);
} else {
console.warn(
`${kernelspec.name} contents will NOT be synced with Jupyter Contents`
);

if (mountDrive) {
console.info(
`${kernelspec.name} contents will be synced with Jupyter Contents`
);
} else {
console.warn(
`${kernelspec.name} contents will NOT be synced with Jupyter Contents`
);
}
const link = empackEnvMetaFile
? await empackEnvMetaFile.getLink(kernelspec)
: '';

return new WebWorkerKernel({
...options,
contentsManager,
mountDrive,
kernelSpec: kernelspec,
empackEnvMetaLink: link
});
}
});
}
};
const link = empackEnvMetaFile
? await empackEnvMetaFile.getLink(kernelspec)
: '';

return new WebWorkerKernel({
...options,
contentsManager,
mountDrive,
kernelSpec: kernelspec,
empackEnvMetaLink: link
});
}
});
}
await app.serviceManager.kernelspecs.refreshSpecs();
}
);
};

const empackEnvMetaPlugin: JupyterLiteServerPlugin<IEmpackEnvMetaFile> = {
id: '@jupyterlite/xeus:empack-env-meta',
Expand All @@ -113,7 +130,5 @@ const empackEnvMetaPlugin: JupyterLiteServerPlugin<IEmpackEnvMetaFile> = {
}
};

plugins.push(empackEnvMetaPlugin);

export default plugins;
export default [empackEnvMetaPlugin, kernelPlugin];
export { IEmpackEnvMetaFile };

0 comments on commit 3a02d65

Please sign in to comment.