All Projects → jpmorganchase → nbcelltests

jpmorganchase / nbcelltests

Licence: Apache-2.0 license
Cell-by-cell testing for production Jupyter notebooks in JupyterLab

Programming Languages

python
139335 projects - #7 most used programming language
typescript
32286 projects
javascript
184084 projects - #8 most used programming language
Makefile
30231 projects
CSS
56736 projects

Projects that are alternatives of or similar to nbcelltests

jupyter-offlinenotebook
Save and load notebooks to local-storage
Stars: ✭ 39 (-40.91%)
Mutual labels:  jupyterlab, jupyter-notebooks, jupyterlab-extension
jupyterlab-kubeflow-kale
JupyterLab extension to provide a Kubeflow specific left area for Notebooks deployment
Stars: ✭ 17 (-74.24%)
Mutual labels:  jupyterlab, jupyterlab-extension
Jupyterlab tensorboard
Tensorboard extension for jupyterlab.
Stars: ✭ 245 (+271.21%)
Mutual labels:  jupyterlab, jupyterlab-extension
jupyterlab-link-share
JupyterLab Extension to easily share a link to a running server on Binder
Stars: ✭ 40 (-39.39%)
Mutual labels:  jupyterlab, jupyterlab-extension
Jupyterlab templates
Support for jupyter notebook templates in jupyterlab
Stars: ✭ 223 (+237.88%)
Mutual labels:  jupyterlab, jupyterlab-extension
Jupyterlab voyager
JupyterLab extension visualize data with Voyager
Stars: ✭ 244 (+269.7%)
Mutual labels:  jupyterlab, jupyterlab-extension
jupyter-extensions
Jupyter extensions for SWAN
Stars: ✭ 56 (-15.15%)
Mutual labels:  jupyter-notebooks, jupyterlab-extension
Awesome Jupyter
A curated list of awesome Jupyter projects, libraries and resources
Stars: ✭ 2,523 (+3722.73%)
Mutual labels:  jupyterlab, jupyterlab-extension
theme-cookiecutter
A cookiecutter template to help you make new JupyterLab theme extensions
Stars: ✭ 47 (-28.79%)
Mutual labels:  jupyterlab, jupyterlab-extension
knowledgelab
KnowledgeRepo + JupyterLab
Stars: ✭ 46 (-30.3%)
Mutual labels:  jupyterlab, jupyterlab-extension
jupyterlab discovery
A JupyterLab extension to facilitate the discovery and installation of other extensions
Stars: ✭ 47 (-28.79%)
Mutual labels:  jupyterlab, jupyterlab-extension
jupyterlab-custom-css
Add custom CSS rules for JupyterLab
Stars: ✭ 32 (-51.52%)
Mutual labels:  jupyterlab, jupyterlab-extension
Best Of Jupyter
🏆 A ranked list of awesome Jupyter Notebook, Hub and Lab projects (extensions, kernels, tools). Updated weekly.
Stars: ✭ 200 (+203.03%)
Mutual labels:  jupyterlab, jupyterlab-extension
jupyterlab-vimrc
add a basic vimrc to jupyterlab vim
Stars: ✭ 59 (-10.61%)
Mutual labels:  jupyterlab, jupyterlab-extension
Awesome Jupyterlab Extension
😎 A curated list of awesome Jupyterlab extension projects. 🌠 Detailed introduction with images.
Stars: ✭ 198 (+200%)
Mutual labels:  jupyterlab, jupyterlab-extension
rk
The remote Jupyter kernel/kernels administration utility
Stars: ✭ 53 (-19.7%)
Mutual labels:  jupyterlab, jupyter-notebooks
Jupyterlab Go To Definition
Navigate to variable's definition with a click in JupyterLab (or with a few key strokes)
Stars: ✭ 180 (+172.73%)
Mutual labels:  jupyterlab, jupyterlab-extension
Jupyterlab Hub
Deprecated: JupyterLab extension for running JupyterLab with JupyterHub
Stars: ✭ 181 (+174.24%)
Mutual labels:  jupyterlab, jupyterlab-extension
jupyterlab iframe
View html as an embedded iframe in JupyterLab
Stars: ✭ 91 (+37.88%)
Mutual labels:  jupyterlab, jupyterlab-extension
perspective-python
Python APIs for perspective front end
Stars: ✭ 14 (-78.79%)
Mutual labels:  jupyterlab, jupyterlab-extension

Cell-by-cell testing for production Jupyter notebooks in JupyterLab

Build Status codecov PyPI PyPI npm

Overview

nbcelltests is designed for writing tests for linearly executed notebooks. Its primary use is for unit testing reports.

Installation

Python package installation: pip install nbcelltests

To use in JupyterLab, you will also need the lab and server extensions. Typically, these are automatically installed alongside nbcelltests, so you should not need to do anything special to use them. The lab extension will require a rebuild of JupyterLab, which you'll be prompted to do on starting JupyterLab the first time after installing celltests (or you can do manually with jupyter lab build). Note that you must have node.js installed (as for any lab extension).

To see what extensions you have, check the output of jupyter labextension list (look for jupyterlab_celltests), and jupyter serverextension list (look for nbcelltests). If for some reason you need to manually install the extensions, you can do so as follows:

jupyter labextension install jupyterlab_celltests
jupyter serverextension enable --py nbcelltests

(Note: if using in an environment, you might wish to add --sys-prefix to the serverextension command.)

"Linearly executed notebooks?"

When converting notebooks into html/pdf/email reports, they are executed top-to-bottom one time, and are expected to contain as little code as reasonably possible, focusing primarily on the plotting and markdown bits. Libraries for this type of thing include Papermill, JupyterLab Emails, etc.

Doesn't this already exist?

Nbval is a great product (we leverage it in this project) and I recommend using it for notebook regression tests. But it only allows for testing for unexpected failures or simple output equality tests.

So why do I want this again?

This doesn't necessarily help you if your data sources go down, but its likely you'll notice this anyway. Where this comes in handy is:

  • when the environment (e.g. package versions) are changing in your system
  • when you play around in the notebook (e.g. nonlinear execution) but aren't sure if your reports will still generate
  • when your software lifecycle systems have a hard time dealing with notebooks (can't lint/audit them as code unless integrated nbdime/nbconvert to script, tough to test, tough to ensure what works today works tomorrow)

So what does this do?

Given a notebook, you can write mocks and assertions for individual cells. You can then generate a testing script for this notebook, allowing you to hook it into your testing system and thereby provide unittests of your report.

Writing tests

When you write tests for a cell, we create a new method on a unittest class corresponding to the index of your cell, and including the cumulative tests for all previous cells (to mimic what has happened so far in the notebook's linear execution). You can write whatever mocking and asserts you like, and can call %cell to inject the contents of the cell into your test. The tests themselves are stored in the cell metadata, similar to celltags, slide information, etc.

Running tests

You can run the tests offline from an .ipynb file, or you can execute them from the browser and view the results of pytest-html's html plugin.

Extra Tests

  • Max number of lines per cell
  • Max number of cells per notebook
  • Max number of function definitions per notebook
  • Max number of class definitions per notebook
  • Percentage of cells tested

Example

In the committed examples/Example.ipynb notebook, but modified so that cell 0 has its import statement copied 10 times (to trigger test and lint failures):

Tests

The following output is generated by running nbcelltests test examples/Example.ipynb

examples/_Example_test.py::TestNotebook::test_cell_coverage PASSED                                                                               [ 20%]
examples/_Example_test.py::TestNotebook::test_code_cell_1 PASSED                                                                                 [ 40%]
examples/_Example_test.py::TestNotebook::test_code_cell_2 PASSED                                                                                 [ 60%]
examples/_Example_test.py::TestNotebook::test_code_cell_3 PASSED                                                                                 [ 80%]
examples/_Example_test.py::TestNotebook::test_code_cell_4 PASSED                                                                                 [100%]

Lint

The following output is generated by running nbcelltests lint examples/Example.ipynb

PASSED: Checking lines in cell (max=10; actual=2) (Cell 1)
PASSED: Checking lines in cell (max=10; actual=1) (Cell 2)
PASSED: Checking lines in cell (max=10; actual=1) (Cell 3)
PASSED: Checking lines in cell (max=10; actual=1) (Cell 4)
PASSED: Checking cells per notebook (max=10; actual=4)
PASSED: Checking functions per notebook (max=10; actual=0)
PASSED: Checking classes per notebook (max=10; actual=0)
FAILED: Checking lint:
	examples/Example.ipynb (in /var/folders/s3/1mjw0y192zg3450tkkn1yfnm0000gn/T/tmpp91li59p.py):32:1: F821 undefined name 'test3'
	examples/Example.ipynb (in /var/folders/s3/1mjw0y192zg3450tkkn1yfnm0000gn/T/tmpp91li59p.py):32:6: W291 trailing whitespace

NB: In jupyterlab, notebooks will be lint checked in-process using the version of python that is running jupyter lab itself. A notebook intended to be run with a Python 2 kernel could therefore generate syntax errors during lint checking.

Development

See CONTRIBUTING.md for guidelines.

License

This software is licensed under the Apache 2.0 license. See the LICENSE and AUTHORS files for details.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].