Skip to main content

Strips outputs from Jupyter and IPython notebooks

Project description

https://travis-ci.org/kynan/nbstripout.svg?branch=master https://img.shields.io/pypi/dm/nbstripout.svg https://img.shields.io/pypi/v/nbstripout.svg https://img.shields.io/badge/license-MIT-blue.svg https://img.shields.io/pypi/pyversions/nbstripout.svg

nbstripout: strip output from Jupyter and IPython notebooks

Opens a notebook, strips its output, and writes the outputless version to the original file.

Useful mainly as a git filter or pre-commit hook for users who don’t want to track output in VCS.

This does mostly the same thing as the Clear All Output command in the notebook UI.

Based on https://gist.github.com/minrk/6176788.

Screencast

This screencast demonstrates the use and working principles behind the nbstripout utility and how to use it as a Git filter:

http://i.imgur.com/7oQHuJ5.png

Installation

You can download and install the latest version of nbstripout from PyPI, the Python package index, as follows:

pip install --upgrade nbstripout

When using the Anaconda Python distribution, install nbstripout via the conda package manager from conda-forge:

conda install -c conda-forge nbstripout

Usage

Strip output from IPython / Jupyter notebook (modifies the files in-place):

nbstripout FILE.ipynb [FILE2.ipynb ...]

Force processing of non .ipynb files:

nbstripout -f FILE.ipynb.bak

Write to stdout e.g. to use as part of a shell pipeline:

cat FILE.ipynb | nbstripout > OUT.ipynb

or

nbstripout -t FILE.ipynb | other-command

Set up the git filter and attributes as described in the manual installation instructions below:

nbstripout --install

Set up the git filter using .gitattributes

nbstripout --install --attributes .gitattributes

Remove the git filter and attributes:

nbstripout --uninstall

Remove the git filter and attributes from .gitattributes:

nbstripout --uninstall --attributes .gitattributes

Check if nbstripout is installed in the current repository (exits with code 0 if installed, 1 otherwise):

nbstripout --is-installed

Print status of nbstripout installation in the current repository and configuration summary of filter and attributes if installed (exits with code 0 if installed, 1 otherwise):

nbstripout --status

Print the version:

nbstripout --version

Show this help page:

nbstripout --help

Apply retroactively

nbstripout can be used to rewrite an existing Git repository using git filter-branch to strip output from existing notebooks. This invocation uses --index-filter and operates on all ipynb-files in the repo:

git filter-branch -f --index-filter '
    git checkout -- :*.ipynb
    find . -name '*.ipynb' -exec nbstripout {} +
    git add . --ignore-removal
'

If the repository is large and the notebooks are in a subdirectory it will run faster with git checkout -- :<subdir>/*.ipynb. You will get a warning for commits that do not contain any notebooks, which can be suppressed by piping stderr to /dev/null.

This is a potentially slower but simpler invocation using --tree-filter:

git filter-branch -f --tree-filter 'find . -name "*.ipynb" -exec nbstripout {} +'

Keeping some output

Do not strip the execution count/prompt number

nbstripout --keep-count

Do not strip the output

nbstripout --keep-output

To mark special cells so that the output is not striped, set the "keep_output": true metadata on the cell. To do this, select the “Edit Metadata” Cell Toolbar, and then use the “Edit Metadata” button on the desired cell to enter something like:

{
  "keep_output": true,
}

Another use-case is to preserve initialization cells that might load customized CSS etc. critical for the display of the notebook. To support this, we also keep output for cells with:

{
  "init_cell": true,
}

This is the same metadata used by the init_cell nbextension.

Manual filter installation

Set up a git filter using nbstripout as follows:

git config filter.nbstripout.clean '/path/to/nbstripout'
git config filter.nbstripout.smudge cat
git config filter.nbstripout.required true

Create a file .gitattributes or .git/info/attributes with:

*.ipynb filter=nbstripout

Apply the filter for git diff of *.ipynb files:

git config diff.ipynb.textconv '/path/to/nbstripout -t'

In file .gitattributes or .git/info/attributes add:

*.ipynb diff=ipynb

Mercurial usage

Mercurial does not have the equivalent of smudge filters. One can use an encode/decode hook but this has some issues. An alternative solution is to provide a set of commands that first run nbstripout, then perform these operations. This is the approach of the mmf-setup package.

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

nbstripout-0.3.3.tar.gz (12.6 kB view details)

Uploaded Source

Built Distribution

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

nbstripout-0.3.3-py2.py3-none-any.whl (11.5 kB view details)

Uploaded Python 2Python 3

File details

Details for the file nbstripout-0.3.3.tar.gz.

File metadata

  • Download URL: nbstripout-0.3.3.tar.gz
  • Upload date:
  • Size: 12.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/36.5.0.post20170921 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.6.2

File hashes

Hashes for nbstripout-0.3.3.tar.gz
Algorithm Hash digest
SHA256 d84659e2778ee2f90c16664e93ba0749ef414149cb607f5f36f2a91deba2ace4
MD5 265d8520a4db4d4cbd8eefd27b3ccf58
BLAKE2b-256 595e71f7268bbda113db94d8733cfe5d00a820daaf5e0d9df308bdd83c1fbca2

See more details on using hashes here.

File details

Details for the file nbstripout-0.3.3-py2.py3-none-any.whl.

File metadata

  • Download URL: nbstripout-0.3.3-py2.py3-none-any.whl
  • Upload date:
  • Size: 11.5 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/36.5.0.post20170921 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.6.2

File hashes

Hashes for nbstripout-0.3.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 392d55bc915a1501f5f4180ef7ddfd3061a1d5a2575ed38f5f2f2f5ee0bb0364
MD5 25741b330cf2b8a703541cc2122d4c3c
BLAKE2b-256 1c372de0322bffacece7f4519486287020a00c0f9887a45c46e43606193b05fb

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