Skip to main content

Validation of Jupyter notebooks and kernels

Project description

JUPYTER NOTEBOOK VALIDATION

  1. Project Overview
  2. Install
  3. Usage
  4. Contributing

PROJECT OVERVIEW

This repository contains scripts used to validate ipynb notebooks and Jupyter kernels.

PACKAGE

A python package has been created which can be installed via pip or conda:

  • conda package: conda-forge jnbv)
  • PyPi package: PyPI jnbv

GOALS

The goals of this project are to be able to:

  1. Execute validation of Jupyter kernels using ipynb notebooks in the terminal
  2. Execute validations non-interactively in a CI pipeline
  3. Check execution output for errors
  4. Compare output of an execution to known output
  5. Select different Jupyter kernels to use
  6. Log results of notebook executions and tests

EXAMPLE OUTPUT

Partial output from validation tests run in the terminal:

screenshot_validation_comparison_output_terminal

Output from an ipynb notebook created from failed execution of a notebook:

screenshot_jupyter_notebook_failed

INSTALL

The jnbv module can be installed with pip or conda using one of the following:

  • conda package: conda-forge jnbv)
  • PyPi package: PyPI jnbv
  • gitlab repository: gitlab jnbv

Install with pip

Create a virtual environment if you don't already have one:

python3 -m venv venv

Activate your environment, in this example it's in venv/:

source venv/bin/activate

Install the code from PyPi using pip:

pip install jnbv

Or install from gitlab using pip and git:

pip install git+https://gitlab.com/MAXIV-SCISW/JUPYTERHUB/jnbv.git

With either method, dependencies will also be installed.

Install with conda

Activate and install directly into your environment:

source venv/bin/activate
conda install jnbv -c conda-forge

Or, you can add jnbv via your conda environment yaml file, by adding conda-forge to your list of channels, and jnbv to your list of dependencies, as in this example file:

---
name: my-conda-env
channels:
  - conda-forge
dependencies:
  - jnbv

Then update your conda environment, for example the base conda environment which is in the directory venv/:

venv/bin/conda env update --name base --file my-conda-env.yml

USAGE

jnbv -h

jnbv_help

EXECUTE NOTEBOOK

For this, you will need to have:

  1. A base environment in which jnbv has been installed (see above)
  2. An ipynb notebook file
  3. A Jupyter kernel that will be used to execute the file

Once you have all these items, the base environment should be activated:

source venv/bin/activate

Check to see what kernels you have available:

jupyter kernelspec list
    Available kernels:
      hdf5-kernel    /var/www/jupyterhub/jnbv/venv/share/jupyter/kernels/hdf5-kernel
      python3        /var/www/jupyterhub/jnbv/venv/share/jupyter/kernels/python3

If you don't have any kernels, then install ipykernel into your environment with either pip or conda:

pip install ipykernel
conda install ipykernel

and then you should have the default kernel "python3" available.

If you don't have an ipynb notebook handy, you can get an example notebook file here:

wget https://gitlab.com/MAXIV-SCISW/JUPYTERHUB/jnbv/-/raw/master/development/the-meaning-of-life.ipynb

And then the ipynb notebook can be executed in the terminal, using the default kernel python3 for example:

jnbv the-meaning-of-life.ipynb \
    --kernel_name python3 \
    --execute

screenshot_execute

READ NOTEBOOK

By defualt, the result of executing an ipynb file is a new ipynb file named output.ipynb. It can be read in the terminal with:

jnbv output.ipynb --read

screenshot_read

TEST NOTEBOOK

The same file can be checked for errors:

jnbv output.ipynb --test

screenshot_test

COMPARE NOTEBOOKS

And two ipynb notebooks can be compared:

jnbv output.ipynb --compare the-meaning-of-life.ipynb

screenshot_compare

EXECUTE, TEST, COMPARE, SAVE

All steps can be made to run in succession with one command:

jnbv the-meaning-of-life.ipynb \
    --kernel_name python3 \
    --execute --read --test --compare --save

Or, more simply using the --validate option, which is a combination of 5 other options:

jnbv the-meaning-of-life.ipynb \
    --kernel_name python3 \
    --validate

Note that above the option --save was also added, which then creates the output directory test-results/, and within that creates subdirectories with kernel names, date stamps, and finally log files and new ipynb files.
For example:

test-results/
└── python3/
    └── 2021-06-11_14-39-40/
        ├── the-meaning-of-life.ipynb
        └── the-meaning-of-life.log

Example output from executing a simple notebook using the default kernel:

usage_python3_the-meaning-of-life

CONTRIBUTING

To contribute to this project, see: CONTRIBUTING.md

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

jnbv-2021.7.28.0.tar.gz (15.6 kB view details)

Uploaded Source

Built Distribution

jnbv-2021.7.28.0-py3-none-any.whl (16.0 kB view details)

Uploaded Python 3

File details

Details for the file jnbv-2021.7.28.0.tar.gz.

File metadata

  • Download URL: jnbv-2021.7.28.0.tar.gz
  • Upload date:
  • Size: 15.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/0.0.0 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.8.5

File hashes

Hashes for jnbv-2021.7.28.0.tar.gz
Algorithm Hash digest
SHA256 c6850172300222b3f4b43381e7a03c63a57f0620e736f5d491b13aff48009bd5
MD5 71be6186fcf2a483d1e457aa5d445449
BLAKE2b-256 1acb12c1de1c377a3fc18dc66ffe11c32c779f3a27800635952eec9bc6e079bb

See more details on using hashes here.

File details

Details for the file jnbv-2021.7.28.0-py3-none-any.whl.

File metadata

  • Download URL: jnbv-2021.7.28.0-py3-none-any.whl
  • Upload date:
  • Size: 16.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/0.0.0 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.8.5

File hashes

Hashes for jnbv-2021.7.28.0-py3-none-any.whl
Algorithm Hash digest
SHA256 82de9e441a2e9a0ade46e2c46c01ced7a12a5d654c6df337746b19ed5e4fef16
MD5 477b8405da0fad77fe7001aa1075a9b8
BLAKE2b-256 77eaa5eadbe7ec7c845be651e64759597d66586bcf3461eae4f8c045fedf324c

See more details on using hashes here.

Supported by

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