Skip to content

Commit

Permalink
Handle .data file for kernels that require preloading (#145)
Browse files Browse the repository at this point in the history
  • Loading branch information
anutosh491 authored Jan 14, 2025
1 parent f224bc4 commit 98caa17
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
30 changes: 24 additions & 6 deletions jupyterlite_xeus/add_on.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@


def get_kernel_binaries(path):
"""return path to the kernel binary (js and wasm) if they exist, else None"""
"""Return paths to the kernel binaries (js, wasm, and optionally data) if they exist, else None."""
json_file = path / "kernel.json"
if json_file.exists():
kernel_spec = json.loads(json_file.read_text(**UTF8))
Expand All @@ -50,9 +50,16 @@ def get_kernel_binaries(path):

kernel_binary_js = Path(kernel_binary + ".js")
kernel_binary_wasm = Path(kernel_binary + ".wasm")
kernel_binary_data = Path(kernel_binary + ".data")

if kernel_binary_js.exists() and kernel_binary_wasm.exists():
return kernel_binary_js, kernel_binary_wasm
# Return all three, with None for .data if it doesn't exist
# as this might not be neccessary for all kernels.
return (
kernel_binary_js,
kernel_binary_wasm,
kernel_binary_data if kernel_binary_data.exists() else None,
)
else:
warnings.warn(f"kernel binaries not found for {path.name}")

Expand All @@ -61,7 +68,6 @@ def get_kernel_binaries(path):

return None


class MountPoints(List):
def from_string(self, s):
return s.split(",")
Expand Down Expand Up @@ -181,10 +187,10 @@ def copy_kernels_from_prefix(self):
for kernel_dir in kernel_spec_path.iterdir():
kernel_binaries = get_kernel_binaries(kernel_dir)
if kernel_binaries:
kernel_js, kernel_wasm = kernel_binaries
kernel_js, kernel_wasm, kernel_data = kernel_binaries
all_kernels.append(kernel_dir.name)
# take care of each kernel
yield from self.copy_kernel(kernel_dir, kernel_wasm, kernel_js)
yield from self.copy_kernel(kernel_dir, kernel_wasm, kernel_js, kernel_data)

# write the kernels.json file
kernel_file = Path(self.cwd.name) / "kernels.json"
Expand All @@ -196,7 +202,7 @@ def copy_kernels_from_prefix(self):
],
)

def copy_kernel(self, kernel_dir, kernel_wasm, kernel_js):
def copy_kernel(self, kernel_dir, kernel_wasm, kernel_js, kernel_data):
kernel_spec = json.loads((kernel_dir / "kernel.json").read_text(**UTF8))

# update kernel_executable path in kernel.json
Expand Down Expand Up @@ -249,6 +255,18 @@ def copy_kernel(self, kernel_dir, kernel_wasm, kernel_js):
],
)

# copy the kernel.data file to the bin dir if present
if kernel_data:
yield dict(
name=f"copy:{kernel_dir.name}:data",
actions=[
(
self.copy_one,
[kernel_data, self.xeus_output_dir / "bin" / kernel_data.name],
),
],
)

# copy the kernel.json file
yield dict(
name=f"copy:{kernel_dir.name}:kernel.json",
Expand Down
4 changes: 4 additions & 0 deletions packages/xeus/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,16 @@ export class XeusRemoteKernel {
// location of the kernel binary on the server
const binary_js = URLExt.join(baseUrl, kernelSpec.argv[0]);
const binary_wasm = binary_js.replace('.js', '.wasm');
const binary_data = binary_js.replace('.js', '.data');

importScripts(binary_js);
globalThis.Module = await createXeusModule({
locateFile: (file: string) => {
if (file.endsWith('.wasm')) {
return binary_wasm;
} else if (file.endsWith('.data')) {
// Handle the .data file if it exists
return binary_data;
}
return file;
}
Expand Down

0 comments on commit 98caa17

Please sign in to comment.