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

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

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
dataAdditional data files required by the notebook (e.g., input data files, images, configuration files).List of labelsoptional[]
app_settingsA JupyterLab overrides.json file providing default settings. Keys are @jupyterlab/<extension>:<plugin> and values are settings objects.Labeloptional"@rules_jupyter//jupyter:lab_app_settings"
cwd_modeThe 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.Stringoptional""
envEnvironment variables to set when launching the server. Values support location expansion (e.g., $(location :target)).Dictionary: String -> Stringoptional{}
env_inheritSpecifies additional environment variables to inherit from the external environment.List of stringsoptional[]
executeWhether 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.BooleanoptionalTrue
notebookThe notebook to serve. Must be a jupyter_notebook target.Labelrequired
run_modeControls 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.Stringoptional"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

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
depsPython dependencies required by the notebook. These are typically Python packages needed for code execution.List of labelsoptional[]
srcThe notebook source file. Can be either a .ipynb file or a .py file in Jupytext format (with # %% cell markers).Labelrequired
dataAdditional data files required by the notebook (e.g., input data files, images, configuration files).List of labelsoptional[]
kernelThe 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.Stringoptional""

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

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
dataAdditional data files required by the notebook (e.g., input data files, images, configuration files).List of labelsoptional[]
cwd_modeThe 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.Stringoptional""
envEnvironment variables to set when executing the notebook. Values support location expansion (e.g., $(location :target)).Dictionary: String -> Stringoptional{}
env_inheritSpecifies additional environment variables to inherit from the external environment.List of stringsoptional[]
notebookThe notebook to execute. Must be a jupyter_notebook target.Labelrequired
out_dirDirectory 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.Stringoptional""
reportsList of report types to generate after successful notebook execution. Valid values: 'html', 'markdown', 'latex', 'webpdf'.List of stringsoptional[]

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

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
dataAdditional data files required by the notebook (e.g., input data files, images, configuration files).List of labelsoptional[]
cwd_modeThe working directory mode for notebook execution. If not specified, uses the toolchain's default_cwd.Stringoptional""
envEnvironment variables to set when executing the notebook. Values support location expansion (e.g., $(location :target)).Dictionary: String -> Stringoptional{}
env_inheritSpecifies additional environment variables to inherit from the external environment when the test is executed by bazel test.List of stringsoptional[]
notebookThe notebook to execute and test. Must be a jupyter_notebook target.Labelrequired
reportsList of report types to generate after successful notebook execution. Valid values: 'html', 'markdown', 'latex'.List of stringsoptional["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

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
dataAdditional data files required by the notebook (e.g., input data files, images, configuration files).List of labelsoptional[]
argsAdditional command-line arguments to pass to the notebook execution.List of stringsoptional[]
cwd_modeThe working directory mode for notebook execution. If not specified, uses the jupyter_toolchain.cwd.Stringoptional""
envEnvironment variables to set when executing the notebook. Values support location expansion (e.g., $(location :target)).Dictionary: String -> Stringoptional{}
notebookThe notebook to execute and convert. Must be a jupyter_notebook target.Labelrequired
out_htmlOutput path for an HTML report. If specified, the notebook will be converted to HTML format.Label; nonconfigurableoptionalNone
out_html_template_typeTemplate type for HTML output.Stringoptional""
out_latexOutput path for a LaTeX report. If specified, the notebook will be converted to LaTeX format.Label; nonconfigurableoptionalNone
out_latex_template_typeTemplate type for LaTeX output.Stringoptional""
out_markdownOutput path for a Markdown report. If specified, the notebook will be converted to Markdown format.Label; nonconfigurableoptionalNone
out_notebookOutput path for the executed notebook (.ipynb file with cell outputs). If not specified, a default name is generated.Label; nonconfigurableoptionalNone
out_rstOutput path for a reStructuredText report. If specified, the notebook will be converted to RST format.Label; nonconfigurableoptionalNone
out_webpdfOutput path for a WebPDF report (generated via Playwright/Chromium). If specified, the notebook will be converted to PDF format using headless browser rendering.Label; nonconfigurableoptionalNone

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

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
cwd_modeThe 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.Stringoptional"execution_root"
jupyterThe Jupyter Python package providing notebook execution capabilities.Labelrequired
jupytextThe Jupytext Python package for converting between notebook formats (e.g., .py to .ipynb).Labelrequired
kernelDefault kernel name to use for notebook execution if not specified in the notebook (e.g., 'python3', 'rust').Stringoptional""
pandocThe Pandoc executable for converting notebooks to various output formats (HTML, LaTeX, PDF, etc.).LabeloptionalNone
playwright_browsers_dirA directory containing the results of playwright install.LabeloptionalNone
playwright_ld_library_dirA directory of shared libraries to prepend to LD_LIBRARY_PATH when launching browsers.LabeloptionalNone