Skip to content

Commit

Permalink
Bootstrap environments using mambajs, allowing libraries handling for…
Browse files Browse the repository at this point in the history
… other kernels than xeus-python (#130)

* Publish 3.0.0a0

SHA256 hashes:

jupyterlite-xeus-2.1.2.tgz: 7b4c609535e58427961dcd91f4f17f1f1772d2e5138d269a2eb731269169f0ad

jupyterlite-xeus-extension-2.1.2.tgz: 75a9bc44d1692f89d7cf66d4868c3c801e4a4fd7917079dd8e13e4fb8d67a76d

jupyterlite_xeus-3.0.0a0-py3-none-any.whl: 0e7bde6fcf00d7645180bd24e442f1d8e154ede9a79bdc28e6be050259b29b50

jupyterlite_xeus-3.0.0a0.tar.gz: 6b145833b74eaaf194a0dddd6e67d10c2e7b7e722217165e514aee6e8a34946f

* Make use of mambajs

Author: Anastasia Sliusar <[email protected]>

* Add mambajs to package.json, clean code

* Fix eslint, add condifition whethere loading sharing libs is available

* Remove try-catch block

* Remove a comment

* Update yarn

* Fix eslint

---------

Co-authored-by: martinRenou <[email protected]>
Co-authored-by: martinRenou <[email protected]>
  • Loading branch information
3 people authored Dec 17, 2024
1 parent 34cbc95 commit 8ad7f33
Show file tree
Hide file tree
Showing 5 changed files with 499 additions and 24 deletions.
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

0 comments on commit 8ad7f33

Please sign in to comment.