rules_jupyter
Bazel rules for running Jupyter notebooks and generating reports.
Setup
To use the rules, add the following to your MODULE.bazelrc.
bazel_dep(name = "rules_jupyter", version = "{version}")
Toolchains
These rules rely heavily on a jupyter_toolchain which will need to be setup.
In your MODULE.bazel, configure the convenience extensions. If Pandoc and Playwright browsers
are available by some other means this can be skipped:
# Configure Pandoc (required for markdown reports)
jupyter = use_extension("@rules_jupyter//jupyter:extensions.bzl", "jupyter")
jupyter.pandoc(name = "pandoc")
use_repo(jupyter, "pandoc")
# Configure Playwright (required for WebPDF reports)
playwright = use_extension("@rules_jupyter//jupyter:extensions.bzl", "playwright")
playwright.toolchain(name = "playwright_toolchains", version = "1.57.0")
use_repo(playwright, "playwright_toolchains")
register_toolchains("@playwright_toolchains//:all")
Python dependencies will also be required for the toolchain. Rules such as rules_req_compile can be used for this.
In your BUILD.bazel:
load("@rules_jupyter//jupyter:jupyter_toolchain.bzl", "jupyter_toolchain")
jupyter_toolchain(
name = "jupyter_toolchain",
jupyter = "@pip_deps//jupyter",
jupytext = "@pip_deps//jupytext",
# Produced by rules_jupyter module extension.
pandoc = "@pandoc",
# Produced by rules_jupyter module extension.
playwright_browsers_dir = "@rules_jupyter//playwright:current_browsers_dir",
)
toolchain(
name = "jupyter_toolchain",
toolchain = ":jupyter_toolchain",
toolchain_type = "@rules_jupyter//jupyter:toolchain_type",
)
Register it in MODULE.bazel:
register_toolchains(
"//:jupyter_toolchain",
)
Jupyter rules
Rules
- jupyter_lab
- jupyter_notebook
- jupyter_notebook_binary
- jupyter_notebook_test
- jupyter_report
- jupyter_toolchain
jupyter_lab
load("@rules_jupyter//jupyter:defs.bzl", "jupyter_lab")
jupyter_lab(name, data, app_settings, cwd_mode, env, env_inherit, execute, notebook, run_mode)
Launches a Jupyter Lab server for interactive notebook development.
This rule creates an executable target that, when run via bazel run, starts a Jupyter Lab
server with the notebook's dependencies available in the virtualenv. By default, when a user
opens the printed URL in their browser, all notebook cells are automatically executed — exactly
as if the user clicked "Run All Cells" in Lab's Run menu.
Example:
jupyter_notebook(
name = "my_notebook",
src = "notebook.py",
deps = ["@pip_deps//polars"],
)
jupyter_lab(
name = "my_lab",
notebook = ":my_notebook",
)
Then run: bazel run :my_lab
ATTRIBUTES
| Name | Description | Type | Mandatory | Default |
|---|---|---|---|---|
| name | A unique name for this target. | Name | required | |
| data | Additional data files required by the notebook (e.g., input data files, images, configuration files). | List of labels | optional | [] |
| app_settings | A JupyterLab overrides.json file providing default settings. Keys are @jupyterlab/<extension>:<plugin> and values are settings objects. | Label | optional | "@rules_jupyter//jupyter:lab_app_settings" |
| cwd_mode | The working directory mode for notebook execution. If not specified, uses the toolchain's default_cwd. execution_root sets the working directory to Bazel's execution root, while notebook_root sets it to the notebook's parent directory. | String | optional | "" |
| env | Environment variables to set when launching the server. Values support location expansion (e.g., $(location :target)). | Dictionary: String -> String | optional | {} |
| env_inherit | Specifies additional environment variables to inherit from the external environment. | List of strings | optional | [] |
| execute | Whether to automatically execute all notebook cells when the user opens the notebook in their browser. When True, Lab triggers 'Run All Cells' as soon as the notebook loads. Can be overridden at runtime with -- --no-execute. | Boolean | optional | True |
| notebook | The notebook to serve. Must be a jupyter_notebook target. | Label | required | |
| run_mode | Controls where the notebook is served from. runfiles (default) copies the notebook to a temp directory with symlinked data dependencies. source serves the notebook directly from the source tree for in-place editing with unrestricted file access; requires a .ipynb source. | String | optional | "runfiles" |
jupyter_notebook
load("@rules_jupyter//jupyter:defs.bzl", "jupyter_notebook")
jupyter_notebook(name, deps, src, data, kernel)
Compiles a Jupyter notebook from source and prepares it for execution.
This rule processes notebook source files and converts them to the standard .ipynb format.
If the source is a .py file in Jupytext format (with # %% cell markers), it will be converted
to .ipynb format. If the source is already a .ipynb file, it is used directly.
Example:
jupyter_notebook(
name = "my_notebook",
src = "notebook.py", # or "notebook.ipynb"
kernel = "python3", # optional
deps = ["@pip_deps//polars"],
data = ["data.csv"],
)
ATTRIBUTES
| Name | Description | Type | Mandatory | Default |
|---|---|---|---|---|
| name | A unique name for this target. | Name | required | |
| deps | Python dependencies required by the notebook. These are typically Python packages needed for code execution. | List of labels | optional | [] |
| src | The notebook source file. Can be either a .ipynb file or a .py file in Jupytext format (with # %% cell markers). | Label | required | |
| data | Additional data files required by the notebook (e.g., input data files, images, configuration files). | List of labels | optional | [] |
| kernel | The name of the Jupyter kernel to use for executing the notebook (e.g., python3, rust). If not specified, the default kernel from the toolchain is used. | String | optional | "" |
jupyter_notebook_binary
load("@rules_jupyter//jupyter:defs.bzl", "jupyter_notebook_binary")
jupyter_notebook_binary(name, data, cwd_mode, env, env_inherit, notebook, out_dir, reports)
An executable rule that runs a Jupyter notebook and optionally generates reports.
Unlike jupyter_notebook_test, this rule creates an executable target for use with bazel run.
Outputs (executed notebook and reports) are written to a configurable output directory, defaulting
to the caller's working directory (BUILD_WORKING_DIRECTORY).
Example:
jupyter_notebook(
name = "my_notebook",
src = "notebook.py",
deps = ["@pip_deps//polars"],
)
jupyter_notebook_binary(
name = "run_notebook",
notebook = ":my_notebook",
reports = ["html"],
)
Then run: bazel run :run_notebook or bazel run :run_notebook -- --my-flag value
ATTRIBUTES
| Name | Description | Type | Mandatory | Default |
|---|---|---|---|---|
| name | A unique name for this target. | Name | required | |
| data | Additional data files required by the notebook (e.g., input data files, images, configuration files). | List of labels | optional | [] |
| cwd_mode | The working directory mode for notebook execution. If not specified, uses the toolchain's default_cwd. execution_root sets the working directory to the workspace root, while notebook_root sets it to the notebook's parent directory. This affects how relative paths in the notebook are resolved. | String | optional | "" |
| env | Environment variables to set when executing the notebook. Values support location expansion (e.g., $(location :target)). | Dictionary: String -> String | optional | {} |
| env_inherit | Specifies additional environment variables to inherit from the external environment. | List of strings | optional | [] |
| notebook | The notebook to execute. Must be a jupyter_notebook target. | Label | required | |
| out_dir | Directory to write output files to. If not specified, outputs are written to a subdirectory named after the target under the caller's working directory (e.g., bazel run :run_notebook writes to ./run_notebook/). Absolute paths (e.g., /tmp) are used as-is; relative paths are resolved against the caller's working directory. | String | optional | "" |
| reports | List of report types to generate after successful notebook execution. Valid values: 'html', 'markdown', 'latex', 'webpdf'. | List of strings | optional | [] |
jupyter_notebook_test
load("@rules_jupyter//jupyter:defs.bzl", "jupyter_notebook_test")
jupyter_notebook_test(name, data, cwd_mode, env, env_inherit, notebook, reports)
A test rule that executes a Jupyter notebook and optionally generates reports. The test fails if any notebook cell raises an error.
ATTRIBUTES
| Name | Description | Type | Mandatory | Default |
|---|---|---|---|---|
| name | A unique name for this target. | Name | required | |
| data | Additional data files required by the notebook (e.g., input data files, images, configuration files). | List of labels | optional | [] |
| cwd_mode | The working directory mode for notebook execution. If not specified, uses the toolchain's default_cwd. | String | optional | "" |
| env | Environment variables to set when executing the notebook. Values support location expansion (e.g., $(location :target)). | Dictionary: String -> String | optional | {} |
| env_inherit | Specifies additional environment variables to inherit from the external environment when the test is executed by bazel test. | List of strings | optional | [] |
| notebook | The notebook to execute and test. Must be a jupyter_notebook target. | Label | required | |
| reports | List of report types to generate after successful notebook execution. Valid values: 'html', 'markdown', 'latex'. | List of strings | optional | ["webpdf"] |
jupyter_report
load("@rules_jupyter//jupyter:defs.bzl", "jupyter_report")
jupyter_report(name, data, args, cwd_mode, env, notebook, out_html, out_html_template_type,
out_latex, out_latex_template_type, out_markdown, out_notebook, out_rst, out_webpdf)
Executes a Jupyter notebook and generates reports in various formats (HTML, Markdown, LaTeX, PDF, RST, WebPDF).
ATTRIBUTES
| Name | Description | Type | Mandatory | Default |
|---|---|---|---|---|
| name | A unique name for this target. | Name | required | |
| data | Additional data files required by the notebook (e.g., input data files, images, configuration files). | List of labels | optional | [] |
| args | Additional command-line arguments to pass to the notebook execution. | List of strings | optional | [] |
| cwd_mode | The working directory mode for notebook execution. If not specified, uses the jupyter_toolchain.cwd. | String | optional | "" |
| env | Environment variables to set when executing the notebook. Values support location expansion (e.g., $(location :target)). | Dictionary: String -> String | optional | {} |
| notebook | The notebook to execute and convert. Must be a jupyter_notebook target. | Label | required | |
| out_html | Output path for an HTML report. If specified, the notebook will be converted to HTML format. | Label; nonconfigurable | optional | None |
| out_html_template_type | Template type for HTML output. | String | optional | "" |
| out_latex | Output path for a LaTeX report. If specified, the notebook will be converted to LaTeX format. | Label; nonconfigurable | optional | None |
| out_latex_template_type | Template type for LaTeX output. | String | optional | "" |
| out_markdown | Output path for a Markdown report. If specified, the notebook will be converted to Markdown format. | Label; nonconfigurable | optional | None |
| out_notebook | Output path for the executed notebook (.ipynb file with cell outputs). If not specified, a default name is generated. | Label; nonconfigurable | optional | None |
| out_rst | Output path for a reStructuredText report. If specified, the notebook will be converted to RST format. | Label; nonconfigurable | optional | None |
| out_webpdf | Output path for a WebPDF report (generated via Playwright/Chromium). If specified, the notebook will be converted to PDF format using headless browser rendering. | Label; nonconfigurable | optional | None |
jupyter_toolchain
load("@rules_jupyter//jupyter:defs.bzl", "jupyter_toolchain")
jupyter_toolchain(name, cwd_mode, jupyter, jupytext, kernel, pandoc, playwright_browsers_dir,
playwright_ld_library_dir)
Defines a Jupyter toolchain that provides Jupyter, Jupytext, Pandoc, and Playwright browser support.
ATTRIBUTES
| Name | Description | Type | Mandatory | Default |
|---|---|---|---|---|
| name | A unique name for this target. | Name | required | |
| cwd_mode | The default working directory mode for notebook execution. This value is used when cwd_mode is not specified in jupyter_report or jupyter_notebook_test rules. workspace_root sets the working directory to the workspace root, while notebook_root sets it to the notebook's parent directory. This affects how relative paths in notebooks are resolved. | String | optional | "execution_root" |
| jupyter | The Jupyter Python package providing notebook execution capabilities. | Label | required | |
| jupytext | The Jupytext Python package for converting between notebook formats (e.g., .py to .ipynb). | Label | required | |
| kernel | Default kernel name to use for notebook execution if not specified in the notebook (e.g., 'python3', 'rust'). | String | optional | "" |
| pandoc | The Pandoc executable for converting notebooks to various output formats (HTML, LaTeX, PDF, etc.). | Label | optional | None |
| playwright_browsers_dir | A directory containing the results of playwright install. | Label | optional | None |
| playwright_ld_library_dir | A directory of shared libraries to prepend to LD_LIBRARY_PATH when launching browsers. | Label | optional | None |