Jupyter notebooks as documentation and tests
Introduction
Jupyter notebooks are interactive computing environments where prose and code can be combined. In the OGS project notebooks can be used to define complex benchmark workflows and its results can be converted to be shown on the OGS web page (see an example here ).
Executed notebooks
These notebooks are part of the regular CI testing. Please try to keep the notebook execution time low.
Create a new notebook
Create a new notebook file in Tests/Data (if it should appear in the benchmark gallery) or in web/content/docs (e.g. for tutorials). Create it as a regular Python-file with Python code blocks and Markdown blocks for text. The notebook execution and conversion is done via Jupytext
. See examples:
- SimpleMechanics.py
(notebooks written as regular
.pyfiles are preferred but Markdown-based or.ipynbfiles notebooks are also possible) - Linear_Disc_with_hole.md (Jupytext-based benchmark notebook in Markdown)
- notebook-bhe_meshing.md (Jupytext-based tutorial notebook in Markdown)
Add web meta information
If the notebook result should appear as a page on the web documentation a frontmatter with some meta information (similar to regular web pages ) is required as the first cell in the notebook:
+++
title = "SimplePETSc"
date = "2021-11-09"
author = "Lars Bilke"
image = "optional_gallery_image.png"
web_subsection = "small-deformations" # required for notebooks in Tests/Data only
+++
<-- Add Two newlines here to separate -->
<-- the frontmatter as its own cell -->- Frontmatter needs to be in TOML -format.
- For notebooks describing benchmarks
web_subsectionneeds to be set to a sub-folder in web/content/docs/benchmarks (if not set the notebook page will not be linked from navigation bar / benchmark gallery on the web page).
Notebook setup
Markdown cells
- HTML inside Markdown cells may be used for specific reasons (e.g. better image formatting).
- For notebooks in
Tests/Dataonly: Static images e.g. for the gallery image or to be used in Markdown cells have to be located in eitherimages- orfigures-subdirectories beneath the Notebook file! Otherwise they will not show up on the web site.-
For image captions add a title in quotation marks after the image path, e.g.
 -
Please note that in merge request web previews static images are not shown at all.
-
Python cells
-
Do not use machine-specific or absolute paths! See the following example to set up notebook output paths:
import os from pathlib import Path # On CI out_dir is set to the notebooks directory inside the build directory # similar to regular benchmark tests. On local testing it will output to the # notebooks source directory under a _out-subdirectory. out_dir = Path(os.environ.get("OGS_TESTRUNNER_OUT_DIR", "_out")) out_dir.mkdir(parents=True, exist_ok=True) # ... # Run ogs; get input data from current directory; write to `out_dir` !bash ogs my_project.prj -o {out_dir} > {out_dir}/log.txt # OR with ogstools: # ... setup model ... import ogstools as ot model = ogs.Project(input_file="input.prj", output_file=out_dir / "output.prj") model.write_input() model.run_model(logfile=out_dir / "log.txt", args=f"-o {out_dir} -m .") # Verify results; on failure assert with: assert False # or raise SystemExit() -
Do not write anything into the source directories. Use an
out_diras above. -
Assume that
ogsand other tools are in thePATH. -
Don’t rely on Jupyter magic commands as the notebooks get executed as regular python scripts as well.
-
If you want to write files accessible to the converted web page, e.g. for writing gif-Animations, use the environment variable
OGS_TESTRUNNER_WEB_OUT_DIR.
Execution environment
In CI the notebooks are executed with all dependencies installed into a virtual environment in the build directory. The installed packages are defined in Test/Data/pyproject.toml and are installed with uv
. The same setup can be enabled locally by setting the CMake option OGS_USE_PIP=ON. E.g.
cmake --preset release -DOGS_USE_PIP=ON # Creates the virtual environment
source ../build/release/.venv/bin/activate # Activates the virtual environment
jupyter lab [path-to-source-directory] # Starts Jupyter LabInstead of activating the environment and manually running jupyter lab you can also just build the jupyter target:
cmake --build --preset release -t jupyter
# or in the build directory:
ninja jupyterRegister with CTest
Add the notebook to CTest (example ) with e.g.:
if(NOT OGS_USE_PETSC)
NotebookTest(NOTEBOOKFILE Mechanics/Linear/SimpleMechanics.py RUNTIME 10)
# Notebooks in web/content need to be prefixed with 'notebook-'!
NotebookTest(NOTEBOOKFILE ../../web/content/docs/tutorials/bhe_meshing/notebook-bhe_meshing.md
PYTHON_PACKAGES openpyxl
RUNTIME 10)
endif()NOTEBOOKFILEis relative toTests/Data.- If your notebook requires additional dependencies add them with
PYTHON_PACKAGES. - If the notebook is in
web/contentit is important to prefix the notebook file name withnotebook-! The prefix is required to indicate Hugo that this is a notebook and not a regular markdown page. RUNTIMElarger than 600 s is executed as large benchmark job.
If your notebook should not appear on the website add the SKIP_WEB-option to NotebookTest(). This may be useful if the notebook serves as CI test only, e.g. comparing multiple simulation runs or doing performance measurements. But please also note that there will be no artifact produced (except for notebook errors which get reported as usual).
Then e.g. run all notebook test (-R nb) in parallel (-j 4) with:
# cd into build directory
ctest -R nb -j 4 --output-on-failureTo view the notebook as a web preview run:
ninja preview-web
# Open http://localhost:1313 in your browserAdvanced topics
Useful tools
Make sure to have uv installed.
Convert .ipynb to .py with jupytext
:
uvx jupytext --to py NOTEBOOKFILE.ipynbAutomatically fix code style issues with ruff
:
uvx ruff check --fix [--unsafe-fixes] NOTEBOOKFILE.pyFormat the notebook with black
:
uvx black NOTEBOOKFILE.pyRun a notebook in BinderHub
On the web site or MR web previews on pages generated by a notebook there is a new banner:
- Click the button to launch the notebook in BinderHub.
- The environment running in BinderHub is defined in
bilke/binder-ogs-requirementsat GitHub - When clicking the link it launches a Jupyter Lab instance pre-configured with OGS via wheel , clones the current OGS repo in it and opens the respective notebook ready to run. Please note that startup times may be several minutes (if the execution environment is not already built and cached) and the computing resources are limited. Currently only the serial OGS configuration is available.
This article was written by Lars Bilke. If you are missing something or you find an error please reach out
to us on our forum.
Generated with Hugo 0.164.0
in CI job 825193
|
Last revision: August 26, 2026
Commit: [web] Typo in jupyter docs. 428baeb75
| Edit this page on