Skip to content

Commit

Permalink
Register all kernels in one plug in
Browse files Browse the repository at this point in the history
  • Loading branch information
trungleduc authored Aug 15, 2024
1 parent 6504866 commit 105afd7
Showing 1 changed file with 57 additions and 40 deletions.
97 changes: 57 additions & 40 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,67 @@
// Copyright (c) JupyterLite Contributors
// Distributed under the terms of the Modified BSD License.

import { PageConfig, URLExt } from '@jupyterlab/coreutils';
import { PageConfig, URLExt } from "@jupyterlab/coreutils";
import {
IServiceWorkerManager,
JupyterLiteServer,
JupyterLiteServerPlugin
} from '@jupyterlite/server';
import { IBroadcastChannelWrapper } from '@jupyterlite/contents';
import { IKernel, IKernelSpecs } from '@jupyterlite/kernel';
JupyterLiteServerPlugin,

Check failure on line 9 in src/index.ts

View workflow job for this annotation

GitHub Actions / check_release

'JupyterLiteServerPlugin' is declared but its value is never read.
} from "@jupyterlite/server";
import { IBroadcastChannelWrapper } from "@jupyterlite/contents";
import { IKernel, IKernelSpecs } from "@jupyterlite/kernel";

import { WebWorkerKernel } from './web_worker_kernel';
import { WebWorkerKernel } from "./web_worker_kernel";

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> => {
return {
id: `@jupyterlite/xeus-${kernel}:register`,
autoStart: true,
requires: [IKernelSpecs],
optional: [IServiceWorkerManager, IBroadcastChannelWrapper],
activate: (
app: JupyterLiteServer,
kernelspecs: IKernelSpecs,
serviceWorker?: IServiceWorkerManager,
broadcastChannel?: IBroadcastChannelWrapper
) => {
const kernelPlugin = {
id: `@jupyterlite/xeus-kernel:register`,
autoStart: true,
requires: [IKernelSpecs],
optional: [IServiceWorkerManager, IBroadcastChannelWrapper],
activate: async (
app: JupyterLiteServer,
kernelspecs: IKernelSpecs,
serviceWorker?: IServiceWorkerManager,
broadcastChannel?: IBroadcastChannelWrapper
) => {
// 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 = getJson('xeus/kernels/' + kernel + '/kernel.json');
const kernelspec = await getJson(
"xeus/kernels/" + kernel + "/kernel.json"
);
kernelspec.name = kernel;
kernelspec.dir = kernel;
for (const [key, value] of Object.entries(kernelspec.resources)) {
Expand All @@ -51,9 +71,6 @@ const plugins = kernel_list.map((kernel): JupyterLiteServerPlugin<void> => {
value as string
);
}

const contentsManager = app.serviceManager.contents;

kernelspecs.register({
spec: kernelspec,
create: async (options: IKernel.IOptions): Promise<IKernel> => {
Expand All @@ -76,12 +93,12 @@ const plugins = kernel_list.map((kernel): JupyterLiteServerPlugin<void> => {
...options,
contentsManager,
mountDrive,
kernelspec
kernelspec,
});
}
},
});
}
};
});
},
};

export default plugins;
export default kernelPlugin;

0 comments on commit 105afd7

Please sign in to comment.