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

Bootstrap environments using mambajs, allowing libraries handling for other kernels than xeus-python #130

Merged
merged 11 commits into from
Dec 17, 2024
35 changes: 35 additions & 0 deletions packages/xeus-extension/lab.webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const path = require('path');
const CopyPlugin = require('copy-webpack-plugin');

const wasmPath = [
__dirname,
'..',
'..',
'node_modules',
'@emscripten-forge',
'mambajs',
'lib',
'*.wasm'
];
const staticPath = [
__dirname,
'..',
'..',
'jupyterlite_xeus',
'labextension',
'static',
'[name].wasm'
];

module.exports = {
plugins: [
new CopyPlugin({
patterns: [
{
from: path.resolve(...wasmPath),
to: path.join(...staticPath)
}
]
})
]
};
2 changes: 2 additions & 0 deletions packages/xeus-extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"@types/json-schema": "^7.0.11",
"@types/react": "^18.0.26",
"@types/react-addons-linked-state-mixin": "^0.14.22",
"copy-webpack-plugin": "^12.0.2",
"css-loader": "^6.7.1",
"npm-run-all": "^4.1.5",
"rimraf": "^5.0.1",
Expand All @@ -65,6 +66,7 @@
"jupyterlab": {
"extension": true,
"outputDir": "../../jupyterlite_xeus/labextension",
"webpackConfig": "lab.webpack.config.js",
"sharedPackages": {
"@jupyterlite/kernel": {
"bundled": false,
Expand Down
1 change: 1 addition & 0 deletions packages/xeus/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"watch:src": "tsc -w --sourceMap"
},
"dependencies": {
"@emscripten-forge/mambajs": "^0.1.0",
"@jupyterlab/coreutils": "^6",
"@jupyterlab/services": "~7.2.5",
"@jupyterlite/contents": "^0.2.0 || ^0.3.0 || ^0.4.5",
Expand Down
41 changes: 27 additions & 14 deletions packages/xeus/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
import { URLExt } from '@jupyterlab/coreutils';

import { IXeusWorkerKernel } from './interfaces';

import {
bootstrapFromEmpackPackedEnvironment,
IPackagesInfo
} from '@emscripten-forge/mambajs';
globalThis.Module = {};

// when a toplevel cell uses an await, the cell is implicitly
Expand Down Expand Up @@ -118,22 +121,34 @@ export class XeusRemoteKernel {
});
try {
await waitRunDependency();

// each kernel can have a `async_init` function
// which can do kernel specific **async** initialization
// This function is usually implemented in the pre/post.js
// in the emscripten build of that kernel
if (globalThis.Module['async_init'] !== undefined) {
if (
globalThis.Module.FS !== undefined &&
globalThis.Module.loadDynamicLibrary !== undefined
) {
const kernel_root_url = empackEnvMetaLink
? empackEnvMetaLink
: URLExt.join(baseUrl, `xeus/kernels/${kernelSpec.dir}`);
const pkg_root_url = URLExt.join(baseUrl, 'xeus/kernel_packages');
const verbose = true;
await globalThis.Module['async_init'](
kernel_root_url,
pkg_root_url,
verbose
const packagesJsonUrl = `${kernel_root_url}/empack_env_meta.json`;
const pkgRootUrl = URLExt.join(baseUrl, 'xeus/kernel_packages');

let packageData: IPackagesInfo = {};
packageData = await bootstrapFromEmpackPackedEnvironment(
packagesJsonUrl,
verbose,
false,
globalThis.Module,
pkgRootUrl
);

if (kernelSpec.name === 'xpython') {
if (Object.keys(packageData).length) {
const { pythonVersion, prefix } = packageData;
if (pythonVersion) {
globalThis.Module.init_phase_2(prefix, pythonVersion, verbose);
}
}
}
}

await waitRunDependency();
Expand All @@ -147,10 +162,8 @@ export class XeusRemoteKernel {
} catch (e) {
if (typeof e === 'number') {
const msg = globalThis.Module.get_exception_message(e);
console.error(msg);
throw new Error(msg);
} else {
console.error(e);
throw e;
}
}
Expand Down
Loading
Loading