Clean Jupyter notebooks for versioning
nb-clean cleans Jupyter notebooks of cell execution counts, metadata, outputs,
and (optionally) empty cells, preparing them for committing to version control.
It provides a Git filter to automatically clean notebooks before they're staged,
and can also be used with other version control systems, as a command line tool,
and as a Python library. It can determine if a notebook is clean or not, which
can be used as a check in your continuous integration pipelines.
nb-clean 2.0.0 introduced a new command line interface to make
cleaning notebooks in place easier. If you upgrade from a previous release,
you'll need to migrate to the new interface as described under
python3 -m pip install nb-clean
Alternately, in Python projects using Poetry or Pipenv for dependency
nb-clean as a development dependency with
poetry add --dev nb-clean or
pipenv install --dev nb-clean.
requires Python 3.6 or later.
To add a filter to an existing Git repository to automatically clean notebooks when they're staged, run the following from the working tree:
This will configure a filter to remove cell execution counts, metadata, and outputs. To also remove empty cells, use:
nb-clean add-filter --remove-empty-cells
To preserve cell metadata, such as that required by tools such as papermill, use:
nb-clean add-filter --preserve-cell-metadata
nb-clean will configure a filter in the Git repository in which it is run, and
won't mutate your global or system Git configuration. To remove the filter, run:
Aside from usage from a filter in a Git repository, you can also clean up a Jupyter notebook with:
nb-clean clean notebook.ipynb
This cleans the notebook in place. You can also pass the notebook content on standard input, in which case the cleaned notebook is written to standard output:
nb-clean clean < original.ipynb > cleaned.ipynb
To also remove empty cells, add the
--remove-empty-cells flag. To
preserve cell metadata, add the
You can check if a notebook is clean with:
nb-clean check notebook.ipynb
or by passing the notebook contents on standard input:
nb-clean check < notebook.ipynb
To also check for empty cells, add the
--remove-empty-cells flag. To
ignore cell metadata, add the
nb-clean will exit with status code 0 if the notebook is clean, and status
code 1 if it is not.
nb-clean will also print details of cell execution
counts, metadata, outputs, and empty cells it finds.
The following table maps from the command line interface of
nb-clean 1.6.0 to
|Clean notebook (remove empty cells)||
|Clean notebook (preserve cell metadata)||
|Check notebook (remove empty cells)||
|Check notebook (preserve cell metadata)||
|Add Git filter to clean notebooks||
|Remove Git filter||
Copyright © 2017-2020 Scott Stevenson.
nb-clean is distributed under the terms of the ISC licence.
Release history Release notifications | RSS feed
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
|Filename, size||File type||Python version||Upload date||Hashes|
|Filename, size nb_clean-2.0.2-py3-none-any.whl (6.9 kB)||File type Wheel||Python version py3||Upload date||Hashes View|
|Filename, size nb-clean-2.0.2.tar.gz (6.8 kB)||File type Source||Python version None||Upload date||Hashes View|