Skip to main content

Basic notebook checks. Do they run? Do they contain lint?

Project description

PyPI PyPI Downloads conda-forge conda-forge downloads Build Status on Travis CI (Linux) Build Status on AppVeyor (Windows) See coverage stats on Coveralls

nbsmoke

Basic notebook smoke tests: Do they run ok? Do they contain lint?


This Pytest plugin was generated with Cookiecutter along with @hackebrot’s Cookiecutter-pytest-plugin template.

Installation

You can install nbsmoke via pip from PyPI:

$ pip install nbsmoke

Or you can install nbsmoke via conda from anaconda.org:

$ conda install -c conda-forge nbsmoke

Usage

Check all notebooks run without errors:

$ pytest --nbsmoke-run

Check all notebooks run without errors, and store html to look at afterwards:

$ pytest --nbsmoke-run --store-html=/scratch

Lint check notebooks:

$ pytest --nbsmoke-lint

Lint failures as warnings only:

$ pytest --nbsmoke-lint --nbsmoke-lint-onlywarn

Instead of all files in a directory, you can specify a list e.g.:

$ pytest --nbsmoke-run notebooks/Untitled*.ipynb

If you want to restrict pytest to running only your notebook tests, use -k, e.g.:

$ pytest --nbsmoke-run -k ".ipynb"

Additional options are available by standard pytest ‘ini’ configuration in setup.cfg, pytest.ini, or tox.ini:

[pytest]
# when running, seconds allowed per cell (see nbconvert timeout)
nbsmoke_cell_timeout = 600

# notebooks to skip running; one case insensitive re to match per line
nbsmoke_skip_run = ^.*skipme\.ipynb$
                   ^.*skipmetoo.*$

# case insensitive re to match for file to be considered notebook;
# defaults to ``^.*\.ipynb``
it_is_nb_file = ^.*\.something$

# flakes you don't want to hear about (regex)
nbsmoke_flakes_to_ignore = .*hvplot.* imported but unused.*

# line magics to treat as being flakes (i.e. magics you don't want in your notebooks)
nbsmoke_flakes_line_magics_blacklist = pylab

# cell magics to treat as being flakes (i.e. magics you don't want in your notebooks)
nbsmoke_flakes_cell_magics_blacklist = bash
                                       ruby

# add your own magic handlers (python file containing line_magic_handlers and cell_magic_handlers as dictionaries magic_name: callable)
nbsmoke_magic_handlers = path/to/file.py

nbsmoke supports # noqa comments to mark that something should be ignored during lint checking.

The nbsmoke_skip_run list in a project’s config can be ignored by passing --ignore-nbsmoke-skip-run (useful if sometimes you want to run all notebooks for a project where many are typically skipped).

What’s the point?

Although more sophisticated testing of notebooks is possible (e.g. see nbval), just checking that notebooks run from start to finish without error in a fresh kernel (or on a neutral CI service) can be useful during development. Practical experience of working on several projects with notebooks confirms this, but that’s all the evidence I have.

Checking notebooks for lint might seem trivial/pointless, but it frequently uncovers unused names (typically unused imports). It’s also quite common to find python 2 vs 3 problems, and sometimes undefined names - in a way that’s faster than running the notebook (over multiple versions of python).

Unused imports/names themselves might seem trivial, but they can hinder understanding of a notebook by readers, or add dependencies that are not required.

Hopefully you don’t have mysterious (unused) imports in your notebook, but if you do, you can add # noqa: explanation to stop flake errors. E.g. if you’re importing something for its side effects, it’s very helpful to inform the reader of that.

Pyflakes is used as the underlying linter because “Pyflakes makes a simple promise: it will never complain about style, and it will try very, very hard to never emit false positives.”

Contributing

First, install using pip install -e .. Then run the tests using tox or pytest -v tests/.

New release to PyPI: git tag -a vX.Y.Z -m "Something about release" && git push --tags. Then a PR will auto-open on conda-forge, which should be merged.

Get some help to debug apparently incorrect flakes by adding --nbsmoke-lint-debug, e.g. pytest -v --nbsmoke-lint --nbsmoke-lint-debug examples.

License

Distributed under the terms of the BSD-3 license, “nbsmoke” is free and open source software.

Issues

If you encounter any problems, please file an issue (ideally including a copy of any problematic notebook).

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

nbsmoke-0.5.0a2.tar.gz (31.3 kB view details)

Uploaded Source

Built Distribution

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

nbsmoke-0.5.0a2-py2.py3-none-any.whl (27.5 kB view details)

Uploaded Python 2Python 3

File details

Details for the file nbsmoke-0.5.0a2.tar.gz.

File metadata

  • Download URL: nbsmoke-0.5.0a2.tar.gz
  • Upload date:
  • Size: 31.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.1

File hashes

Hashes for nbsmoke-0.5.0a2.tar.gz
Algorithm Hash digest
SHA256 0390ab58462d4ceea07a48eaf95cd2e88b6224cd228d69cb8f1573ca117460ff
MD5 f100baee8295e25b9ee553c4ca074f48
BLAKE2b-256 108373aa00d2cf493dba58686e4e9325783158d7934ac59447cec1ac670c2b84

See more details on using hashes here.

File details

Details for the file nbsmoke-0.5.0a2-py2.py3-none-any.whl.

File metadata

  • Download URL: nbsmoke-0.5.0a2-py2.py3-none-any.whl
  • Upload date:
  • Size: 27.5 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.1

File hashes

Hashes for nbsmoke-0.5.0a2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 f4833fe7e454973c57a2eeac8e365e3105392648dfbd1ddbc5abd790a79d533f
MD5 15444918be573fc116c599d6c1f18f6f
BLAKE2b-256 78f537a56e381e774c46a794621d94eea78f32d9c78a67e6b3dfbaa7164a7c57

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