Skip to main content

Pytest plugin for testing notebooks

Project description

nbmake

codecov PyPI versions PyPI versions Twitter PyPI Downloads

What? Pytest plugin for testing and releasing notebook documentation

Why? To raise the quality of scientific material through better automation

Who is this for? Research/Machine Learning Software Engineers who maintain packages/teaching materials with documentation written in notebooks.

Functionality

  1. Executes notebooks using pytest and nbclient, allowing parallel notebook testing
  2. Optionally writes back to the repo, allowing faster building of nbsphinx or jupyter book docs

Quick Start

pip install pytest nbmake
pytest --nbmake **/*ipynb

Allow errors and Configure Cell Timeouts

Each notebook can be configured to allow errors and fail if running exceeds a timeout.

This configuration must be placed in the notebook's top-level metadata (not cell-level metadata).

Your notebook should look like this:

{
  "cells": [ ... ],
  "metadata": {
    "kernelspec": { ... },
    "execution": {
      "allow_errors": true,
      "timeout": 300
    }
  }
}

Add Missing Jupyter Kernel to Your CI Environment

If you are using a kernel name other than the default ‘python3’. You will see an error message when executing your notebooks in a fresh CI environment: Error - No such kernel: 'mycustomkernel'

Use ipykernel to install the custom kernel:

python -m ipykernel install --user --name mycustomkernel

If you are using another language such as c++ in your notebooks, you may have a different process for installing your kernel.

Parallelisation

Parallelisation with xdist is experimental upon initial release, but you can try it out:

pip install pytest-xdist

pytest --nbmake -n=auto

It is also possible to parallelise at a CI-level using strategies, see example

Build Jupyter Books Faster

Using xdist and the --overwrite flag let you build a large jupyter book repo faster:

pytest --nbmake --overwrite -n=auto examples
jb build examples

Advice on Usage

nbmake is best used in a scenario where you use the ipynb files only for development. Consumption of notebooks is primarily done via a docs site, built through jupyter book, nbsphinx, or some other means. If using one of these tools, you are able to write assertion code in cells which will be hidden from readers.

Pre-commit

Treating notebooks like source files lets you keep your repo minimal. Some tools, such as plotly may drop several megabytes of javascript in your output cells, as a result, stripping out notebooks on pre-commit is advisable:

# .pre-commit-config.yaml
repos:
  - repo: https://github.com/kynan/nbstripout
    rev: master
    hooks:
      - id: nbstripout

See https://pre-commit.com/ for more...

Disable Nbmake

Implicitly:

pytest

Explicitly:

pytest -p no:nbmake

See Also:

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nbmake-0.7.tar.gz (9.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nbmake-0.7-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

Details for the file nbmake-0.7.tar.gz.

File metadata

  • Download URL: nbmake-0.7.tar.gz
  • Upload date:
  • Size: 9.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.7

File hashes

Hashes for nbmake-0.7.tar.gz
Algorithm Hash digest
SHA256 b03adba337dad79bec3c9a5cb52d0d52859c793c74656795018ad37a32570d41
MD5 43202378c84727c5ecd25d204e784be2
BLAKE2b-256 1b1a985cecfa4cda5059c9562dfe4256cce2b32b157594273bd4f2ace53279ea

See more details on using hashes here.

File details

Details for the file nbmake-0.7-py3-none-any.whl.

File metadata

  • Download URL: nbmake-0.7-py3-none-any.whl
  • Upload date:
  • Size: 10.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.7

File hashes

Hashes for nbmake-0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 dbde6ab506c0133a84cab6911dff0bc75ca814ba386f162a900f3b5e7b6c1b7f
MD5 f6a769794baabddbea6c47a0c7195869
BLAKE2b-256 9aaa462be8e18d97e7b673e8e85a53a5a102d3e22ac02bc879742c625fadee2f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page