This notebook installs the Pixi software environment required to run the other notebooks in the ASF Cookbook Template.
Overview¶
1. Prerequisites¶
| Prerequisite | Importance | Notes |
|---|---|---|
| Option 1: The Pixi package manager must be installed on the system running Jupyter Lab | Necessary | Pixi may not be supported on all Jupyter Hubs |
| UW Scientific Software Engineering Center’s Pixi guide | Helpful | This is a great resource to get quickly get started with Pixi |
Rough Notebook Time Estimate: 3 minutes
2. Install the environment with the Pixi package manager¶
2a. Install the environment with Pixi¶
!pixi install -m ..2b. Register the environment’s Python kernel with ipykernel¶
!pixi run -m .. register-kernel2c. Ensure notebook shell commands run in the environment¶
This ensures that shell commands executed from inside a notebook with ! run in the notebook kernel’s environment. This works by launching the entire Jupyter kernel process inside the Pixi environment, so the kernel’s PATH, environment variables, and Python executable all come from that Pixi environment, not from the parent JupyterLab environment.
from jupyter_client.kernelspec import KernelSpecManager
import json
from pathlib import Path
env_name = "ASF_SAR_Community_Cookbook"
ksm = KernelSpecManager()
spec = ksm.get_kernel_spec(env_name)
kernel_dir = Path(spec.resource_dir)
kernel_json = kernel_dir / "kernel.json"
data = json.loads(kernel_json.read_text())
orig_argv = data.get("argv", [])
new_argv = [
"pixi",
"run",
"--manifest-path",
str(Path.cwd().parent)
] + orig_argv
data["argv"] = new_argv
kernel_json.write_text(json.dumps(data, indent=2))
print(f"Updated kernel.json at {kernel_json} with Pixi wrapper.")
print("argv:", data["argv"])2d. (Optional) Delete the environment and remove its kernelspec¶
# # Uncomment and run the code below to delete your pixi environment
# import os
# from contextlib import contextmanager
# from pathlib import Path
# @contextmanager
# def work_dir(new_dir):
# old_dir = os.getcwd()
# os.chdir(new_dir)
# try:
# yield
# finally:
# os.chdir(old_dir)
# import os
# from contextlib import contextmanager
# from pathlib import Path
# @contextmanager
# def work_dir(new_dir):
# old_dir = os.getcwd()
# os.chdir(new_dir)
# try:
# yield
# finally:
# os.chdir(old_dir)
# with work_dir(Path.cwd().parent):
# !pixi clean
# !jupyter kernelspec remove ASF_SAR_Community_Cookbook -y