Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
N

nbstripout

> 开发工具
Open source

strip output from Jupyter and IPython notebooks

1.5K stars0 likes3 views
WebsiteGitHub

About

strip output from Jupyter and IPython notebooks

nbstripout: strip output from Jupyter and IPython notebooks

Reads a notebook from a file or stdin, strips output and some metadata, and writes the "cleaned" version of the notebook to the original file or stdout.

Intended to be used as a Git filter or pre-commit hook for users who don't want to track output in Git.

Roughly equivalent to the "Clear All Output" command in the notebook UI, but only "visible" to Git: keep your output in the file on disk, but don't commit the output to Git. This helps minimizing diffs and reduce file size.

Originally based on https://gist.github.com/minrk/6176788.

Python 3 only

As of version 0.4.0, nbstripout supports Python 3 only. If you need to use Python 2.7, install nbstripout 0.3.10:

bash
pip install nbstripout==0.3.10

Screencast

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

Installation

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

bash
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 / Zeppelin notebook (modifies the file in-place):

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

Force processing of non .ipynb files:

nbstripout -f FILE.ipynb.bak

For using Zeppelin mode while processing files with other extensions use:

nbstripout -m zeppelin -f <file.ext>

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

cat FILE.ipynb | nbstripout > OUT.ipynb
cat FILE.zpln | nbstripout -m zeppelin > OUT.zpln

or

nbstripout -t FILE.ipynb | other-command

Do a dry run and only list which files would have been stripped:

nbstripout --dry-run FILE.ipynb [FILE2.ipynb ...]

or

Do a verification run, which works like dry run but will fail if any files would have been stripped:

nbstripout --verify FILE.ipynb [FILE2.ipynb ...]

Operate on all .ipynb files in the current directory and subdirectories recursively:

find . -name '*.ipynb' -exec nbstripout {} +

Print the version:

nbstripout --version

Show help and usage instructions:

nbstripout --help

Using as a Git filter

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

nbstripout --install

Note: The filter is declared as required, meaning the filter must succeed. Failures and misconfigurations will not simply cause the filter to be ignored.

Set up the git filter using .gitattributes:

nbstripout --install --attributes .gitattributes

Specify a different path to the Python interpreter to be used for the git filters (default is the path to the Python interpreter used when nbstripout is installed). This is useful if you have Python installed in different or unusual locations across machines, e.g. /usr/bin/python3 on your machine vs /usr/local/bin/python3 in a container or elsewhere.

nbstripout --install --python python3

Using just python3 lets each machine find its Python itself. However, keep in mind that depending on your setup this might not be the Python version you want or even fail because an absolute path is required.

Set up the git filter in your global ~/.gitconfig:

nbstripout --install --global

Set up the git filter in your system-wide $(prefix)/etc/gitconfig (most installations will require you to sudo):

[sudo] nbstripout --install --system

Remove the git filter and attributes:

nbstripout --uninstall

Remove the git filter from your global ~/.gitconfig and attributes:

nbstripout --uninstall --global

Remove the git filter from your system-wide $(prefix)/etc/gitconfig and attributes:

[sudo] nbstripout --uninstall --system

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

Configuration files

The following table shows in which files the nbstripout filter and attribute configuration is written to for given extra flags to --install and --uninstall:

flags filters attributes
none .git/config .git/info/attributes
--global ~/.gitconfig ~/.config/git/attributes
--system $(prefix)/etc/gitconfig $(prefix)/etc/gitattributes
--attributes=.gitattributes .git/config .gitattributes
--global --attributes=.gitattributes ~/.gitconfig .gitattributes

Install globally

Usually, nbstripout is installed per repository so you can choose where to use it or not. You can choose to set the attributes in .gitattributes and commit this file to your repository, however there is no way to have git set up the filters automatically when someone clones a repository. This is by design, to prevent you from executing arbitrary and potentially malicious code when cloning a repository.

To install nbstripout for all your repositories such that you no longer need to run the installation once per repository, install as follows:

mkdir -p ~/.config/git  # This folder may not exist
nbstripout --install --global --attributes=~/.config/git/attributes

This will set up the filters and diff driver in your ~/.gitconfig and instruct git to apply them to any .ipynb file in any repository.

Note that you need to uninstall with the same flags:

nbstripout --uninstall --global --attributes=~/.config/git/attributes

Install system-wide

To install nbstripout system-wide so that it applies to all repositories for all users, install as follows (most installations will require you to sudo):

[sudo] nbstripout --install --system

This will set up the filters and diff driver in $(prefix)/etc/gitconfig and instruct git to apply them to any .ipynb file in any repository for any user.

Note that you need to uninstall with the same flags:

[sudo] nbstripout --uninstall --system

Apply retroactively

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

…

[!WARNING]

This will affect the history of the repository, so use with caution!

Removing empty cells

Drop empty cells i.e. cells where source is either empty or only contains whitespace:

nbstripout --drop-empty-cells

Removing [init]{.title-ref} cells

By default nbstripout will keep cells with init_cell: true metadata. To disable this behavior use:

nbstripout --strip-init-cells

Removing entire cells

In certain conditions it might be handy to remove not only the output, but the entire cell, e.g. when developing exercises.

To drop all cells tagged with "solution" run:

nbstripout --drop-tagged-cells="solution"

The option accepts a list of tags separated by whitespace.

Forcing UNIX newlines

Force UNIX (LF) newlines in the output (useful on Windows to keep consistent line endings in filtered output or textconv diffs):

nbstripout --unix-newlines FILE.ipynb

Keeping some output

Do not strip the execution count/prompt number:

nbstripout --keep-count

Do not strip outputs that are smaller that a given max size (useful for removing only large outputs like images):

nbstripout --max-size 1k

Do not strip the output, only metadata:

nbstripout --keep-output

Output Types

When keeping the output, drop a specific output_type, like error or stream

nbstripout --drop-output-type error stream

Drop all output except specific output types:

nbstripout --keep-output-type execute_result

Note: --keep-output-type will override --max-size for outputs that match.

For stripping certain outputs that have names (like stream which can be stderr or stdout) you can use a colon to specify the name. The following would strip all stderr output.

nbstripout --drop-output-type stream:stderr

stream:stdout would strip all stdout output, including print statements.

Cell IDs

Do not reassign the cell ids to be sequential (which is the default behavior):

nbstripout --keep-id

Keeping Output on Specific Cells

To mark special cells so that the output is not stripped, you can either:

  1. Set the keep_output tag on the cell. To do this, enable the tags toolbar (View > Cell Toolbar > Tags) and then add the keep_output tag for each cell you would like to keep the output for.

  2. 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,
    }

You can also keep output for an entire notebook. This is useful if you want to strip output by default in an automated environment (e.g. CI pipeline), but want to be able to keep outputs for some notebooks. To do so, add the option above to the notebook metadata instead. (You can also explicitly remove outputs from a particular cell in these notebooks by adding a cell-level metadata entry.)

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.

Stripping metadata

The following metadata is stripped by default:

  • Notebook metadata: signature, widgets
  • Cell metadata: ExecuteTime, collapsed, execution, heading_collapsed, hidden, scrolled

Additional metadata to be stripped can be configured via either

  • git config (--global/--system) filter.nbstripout.extrakeys, e.g. :

    bash
    git config --global filter.nbstripout.extrakeys '
      metadata.celltoolbar
      metadata.kernelspec
      metadata.language_info.codemirror_mode.version
      metadata.language_info.pygments_lexer
      metadata.language_info.version
      metadata.toc
      metadata.notify_time
      metadata.varInspector
      cell.metadata.heading_collapsed
      cell.metadata.hidden
      cell.metadata.code_folding
      cell.metadata.tags
      cell.metadata.init_cell'
    
  • the --extra-keys flag, which takes a space-delimited string as an argument, e.g. :

    --extra-keys="metadata.celltoolbar cell.metadata.heading_collapsed"

Note: Only notebook and cell metadata is currently supported and every key specified via filter.nbstripout.extrakeys or --extra-keys must start with metadata. for notebook and cell.metadata. for cell metadata.

You c

Issues· 10 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Pythonfiltergithacktoberfesthooks

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category开发工具
PricingOpen source

> Related tools

V
VS Code
流行的开源代码编辑器
G
Git
分布式版本控制系统
V
Vite
下一代前端构建工具