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

Register all kernels in one plugin #107

Merged
merged 4 commits into from
Dec 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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',
martinRenou marked this conversation as resolved.
Show resolved Hide resolved
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 };
Loading