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).
These notebooks are part of the regular CI testing. Please try to keep the notebook execution time low.
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:
.py files are preferred but Markdown-based or .ipynb files notebooks are also possible)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 -->web_subsection needs 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).Tests/Data only: Static images e.g. for the gallery image or to be used in Markdown cells have to be located in either images- or figures-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.
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_dir as above.
Assume that ogs and other tools are in the PATH.
Don’t rely on Jupyter magic commands as the notebooks get executed as regular python scripts as well.
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 --preset release -t jupyter
# or in the build directory:
ninja jupyterAdd 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()NOTEBOOKFILE is relative to Tests/Data.PYTHON_PACKAGES.web/content it is important to prefix the notebook file name with notebook-! The prefix is required to indicate Hugo that this is a notebook and not a regular markdown page.RUNTIME larger 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 browserMake 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.pyOn the web site or MR web previews on pages generated by a notebook there is a new banner:
bilke/binder-ogs-requirements at GitHubThis article was written by Lars Bilke. If you are missing something or you find an error please let us know.
Generated with Hugo 0.150.1
in CI job 743172
|
Last revision: February 5, 2026
Commit: [py,cmake] Allow Python 3.14. 4d949dd99c
| Edit this page on