nbstripout


Namenbstripout JSON
Version 0.8.1 PyPI version JSON
download
home_pagehttps://github.com/kynan/nbstripout
SummaryStrips outputs from Jupyter and IPython notebooks
upload_time2024-11-17 10:38:33
maintainerNone
docs_urlNone
authorFlorian Rathgeber
requires_python>=3.8
licenseLicense :: OSI Approved :: MIT License
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            [![tests](https://github.com/kynan/nbstripout/actions/workflows/tests.yml/badge.svg)](https://github.com/kynan/nbstripout/actions/workflows/tests.yml)
[![downloads](https://img.shields.io/pypi/dm/nbstripout)](https://pypi.org/project/nbstripout)
[![PyPI version](https://img.shields.io/pypi/v/nbstripout)](https://pypi.org/project/nbstripout)
[![conda-forge version](https://img.shields.io/conda/vn/conda-forge/nbstripout)](https://anaconda.org/conda-forge/nbstripout)
[![supported Python versions](https://img.shields.io/pypi/pyversions/nbstripout)](https://pypi.org/project/nbstripout)
[![Python package formats](https://img.shields.io/pypi/format/nbstripout)](https://pypi.org/project/nbstripout)
[![license](https://img.shields.io/pypi/l/nbstripout)](https://raw.githubusercontent.com/kynan/nbstripout/main/LICENSE.txt)
[![GitHub stars](https://img.shields.io/github/stars/kynan/nbstripout?style=social)](https://github.com/kynan/nbstripout/stargazers)
[![GitHub forks](https://img.shields.io/github/forks/kynan/nbstripout?style=social)](https://github.com/kynan/nbstripout/network/members)

# 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:

    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:

[![image](https://i.imgur.com/7oQHuJ5.png)](https://www.youtube.com/watch?v=BEMP4xacrVc)

## Installation

You can download and install the latest version of `nbstripout` from the Python
package index [PyPI](https://pypi.org/project/nbstripout/) as follows:

    pip install --upgrade nbstripout

When using the [Anaconda](https://www.anaconda.com/download) Python
distribution, install `nbstripout` via the [conda](https://docs.conda.io)
package manager from [conda-forge](https://conda-forge.org):

    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](https://git-scm.com/docs/gitattributes#_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`](https://github.com/newren/git-filter-repo) to strip output
from existing notebooks. This invocation operates on all ipynb files in the repo:

```sh
    #!/usr/bin/env bash
    # get lint-history with callback from https://github.com/newren/git-filter-repo/pull/542
    ./lint-history.py --relevant 'return filename.endswith(b".ipynb")' --callback '
    import json, warnings, nbformat
    from nbstripout import strip_output
    from nbformat.reader import NotJSONError
    try:
        with warnings.catch_warnings():
            warnings.simplefilter("ignore", category=UserWarning)
            notebook = nbformat.reads(blob.data, as_version=nbformat.NO_CONVERT)
        # customize to your needs
        strip_output(notebook, keep_output=False, keep_count=False, keep_id=False, extra_keys=["metadata.widgets","metadata.execution","cell.attachments"], drop_empty_cells=True,  drop_tagged_cells=[],strip_init_cells=False, max_size=0)
        old_len = len(blob.data)
        blob.data = (nbformat.writes(notebook) + "\n").encode("utf-8")
        if old_len != len(blob.data):
            print(change.blob_id, change.filename, old_len, len(blob.data))
    except NotJSONError as e:
         print("ERROR", type(e), change.blob_id, change.filename)
    '
```

### 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.

### 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

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

    nbstripout --keep-id

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](https://github.com/ipython-contrib/jupyter_contrib_nbextensions/tree/master/src/jupyter_contrib_nbextensions/nbextensions/init_cell).

### 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. :

        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 can keep certain metadata that would be stripped by default with either

-   `git config (--global/--system) filter.nbstripout.keepmetadatakeys`, e.g.:

        git config --global filter.nbstripout.keepmetadatakeys '
          cell.metadata.collapsed
          cell.metadata.scrolled'

-   the `--keep-metadata-keys` flag, which takes a space-delimited string as an
    argument, e.g.:

        --keep-metadata-keys="cell.metadata.collapsed cell.metadata.scrolled"

Note: Previous versions of Jupyter used `metadata.kernel_spec` for kernel
metadata. Prefer stripping `kernelspec` entirely: only stripping some attributes
inside `kernelspec` may lead to errors when opening the notebook in Jupyter (see
[#141](https://github.com/kynan/nbstripout/issues/141)).

### Excluding files and folders

To exclude specific files or folders from being processed by the `nbstripout`
filters, add the path and exception to your filter specifications defined in
`.git/info/attributes` or `.gitattributes`:

    docs/** filter= diff=

This will disable `nbstripout` for any file in the `docs` directory.:

    notebooks/Analysis.ipynb filter= diff=

This will disable `nbstripout` for the file `Analysis.ipynb` located in the
`notebooks` directory.

To check which attributes a given file has with the current config, run:

    git check-attr -a -- path/to/file

For a file to which the filter applies you will see the following:

    $ git check-attr -a -- foo.ipynb
    foo.ipynb: diff: ipynb
    foo.ipynb: filter: nbstripout

For a file in your excluded folder you will see the following:

    $ git check-attr -a -- docs/foo.ipynb
    foo.ipynb: diff:
    foo.ipynb: filter:

## Manual filter installation

Set up a git filter and diff driver using nbstripout as follows:

    git config filter.nbstripout.clean '/path/to/nbstripout'
    git config filter.nbstripout.smudge cat
    git config filter.nbstripout.required true
    git config diff.ipynb.textconv '/path/to/nbstripout -t'

This will add a section to the `.git/config` file of the current repository.

If you want the filter to be installed globally for your user, add the
`--global` flag to the `git config` invocations above to have the configuration
written to your `~/.gitconfig` and apply to all repositories.

If you want the filter to be installed system-wide, add the `--system` flag to
the `git config` invocations above to have the configuration written to
`$(prefix)/etc/gitconfig` and apply to all repositories for all users.

Create a file `.gitattributes` (if you want it versioned with the repository) or
`.git/info/attributes` (to apply it only to the current repository) with the
following content:

    *.ipynb filter=nbstripout
    *.ipynb diff=ipynb

This instructs git to use the filter named `nbstripout` and the diff driver
named `ipynb` set up in the git config above for every `.ipynb` file in the
repository.

If you want the attributes be set for `.ipynb` files in any of your git
repositories, add those two lines to `~/.config/git/attributes`. Note that this
file and the `~/.config/git` directory may not exist.

If you want the attributes be set for `.ipynb` files in any git repository on
your system, add those two lines to `$(prefix)/etc/gitattributes`. Note that
this file may not exist.

## Using `nbstripout` as a pre-commit hook

[pre-commit](https://pre-commit.com) is a framework for managing git
[pre-commit hooks](https://git-scm.com/docs/githooks).

Once you have [pre-commit](https://pre-commit.com) installed, add the following
to the `.pre-commit-config.yaml` in your repository:

    repos:
    - repo: https://github.com/kynan/nbstripout
      rev: 0.8.1
      hooks:
        - id: nbstripout

Then run `pre-commit install` to activate the hook.

> [!WARNING]
>
> In this mode, `nbstripout` is used as a git hook to strip any `.ipynb` files
> before committing. This also modifies your working copy!
>
> In its regular mode, `nbstripout` acts as a filter and only modifies what git
> gets to see for committing or diffing. The working copy stays intact.

## Troubleshooting

### Known issues

Certain Git workflows are not well supported by `nbstripout`:

-   Local changes to notebook files that are made invisible to Git due to the
    `nbstripout` filter do still cause conflicts when attempting to sync
    upstream changes (`git pull`, `git merge` etc.). This is because Git has no
    way of resolving a conflict caused by a non-stripped local file being merged
    with a stripped upstream file.  Addressing this issue is out of scope for
    `nbstripout`. Read more and find workarounds in
    [#108](https://github.com/kynan/nbstripout/issues/108).

### Show files processed by nbstripout filter

Git has [no builtin support](https://stackoverflow.com/a/52065333/396967) for
listing files a clean or smudge filter operates on. As a workaround, change the
setup of your filter in `.git/config`, `~/.gitconfig` or
`$(prefix)/etc/gitconfig` as follows to see the filenames either filter operates
on:

    [filter "nbstripout"]
        clean  = "f() { echo >&2 \"clean: nbstripout $1\"; nbstripout; }; f %f"
        smudge = "f() { echo >&2 \"smudge: cat $1\"; cat; }; f %f"
        required = true

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/kynan/nbstripout",
    "name": "nbstripout",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": null,
    "author": "Florian Rathgeber",
    "author_email": "florian.rathgeber@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/92/6e/05d7e0e35598bd0d423167295f978005912a2dcd137c88ebf36e34047dc7/nbstripout-0.8.1.tar.gz",
    "platform": null,
    "description": "[![tests](https://github.com/kynan/nbstripout/actions/workflows/tests.yml/badge.svg)](https://github.com/kynan/nbstripout/actions/workflows/tests.yml)\n[![downloads](https://img.shields.io/pypi/dm/nbstripout)](https://pypi.org/project/nbstripout)\n[![PyPI version](https://img.shields.io/pypi/v/nbstripout)](https://pypi.org/project/nbstripout)\n[![conda-forge version](https://img.shields.io/conda/vn/conda-forge/nbstripout)](https://anaconda.org/conda-forge/nbstripout)\n[![supported Python versions](https://img.shields.io/pypi/pyversions/nbstripout)](https://pypi.org/project/nbstripout)\n[![Python package formats](https://img.shields.io/pypi/format/nbstripout)](https://pypi.org/project/nbstripout)\n[![license](https://img.shields.io/pypi/l/nbstripout)](https://raw.githubusercontent.com/kynan/nbstripout/main/LICENSE.txt)\n[![GitHub stars](https://img.shields.io/github/stars/kynan/nbstripout?style=social)](https://github.com/kynan/nbstripout/stargazers)\n[![GitHub forks](https://img.shields.io/github/forks/kynan/nbstripout?style=social)](https://github.com/kynan/nbstripout/network/members)\n\n# nbstripout: strip output from Jupyter and IPython notebooks\n\nReads a notebook from a file or stdin, strips output and some metadata, and\nwrites the \"cleaned\" version of the notebook to the original file or stdout.\n\nIntended to be used as a Git filter or pre-commit hook for users who don't want\nto track output in Git.\n\nRoughly equivalent to the \"Clear All Output\" command in the notebook UI, but\nonly \"visible\" to Git: keep your output in the file on disk, but don't commit\nthe output to Git. This helps minimizing diffs and reduce file size.\n\nOriginally based on <https://gist.github.com/minrk/6176788>.\n\n## Python 3 only\n\nAs of version 0.4.0, nbstripout supports Python 3 *only*. If you need to use\nPython 2.7, install nbstripout 0.3.10:\n\n    pip install nbstripout==0.3.10\n\n## Screencast\n\nThis screencast demonstrates the use and working principles behind the\nnbstripout utility and how to use it as a Git filter:\n\n[![image](https://i.imgur.com/7oQHuJ5.png)](https://www.youtube.com/watch?v=BEMP4xacrVc)\n\n## Installation\n\nYou can download and install the latest version of `nbstripout` from the Python\npackage index [PyPI](https://pypi.org/project/nbstripout/) as follows:\n\n    pip install --upgrade nbstripout\n\nWhen using the [Anaconda](https://www.anaconda.com/download) Python\ndistribution, install `nbstripout` via the [conda](https://docs.conda.io)\npackage manager from [conda-forge](https://conda-forge.org):\n\n    conda install -c conda-forge nbstripout\n\n## Usage\n\nStrip output from IPython / Jupyter / Zeppelin notebook (modifies the file\nin-place):\n\n    nbstripout FILE.ipynb [FILE2.ipynb ...]\n    nbstripout FILE.zpln\n\nForce processing of non `.ipynb` files:\n\n    nbstripout -f FILE.ipynb.bak\n\nFor using Zeppelin mode while processing files with other extensions use:\n\n    nbstripout -m zeppelin -f <file.ext>\n\nWrite to stdout e.g. to use as part of a shell pipeline:\n\n    cat FILE.ipynb | nbstripout > OUT.ipynb\n    cat FILE.zpln | nbstripout -m zeppelin > OUT.zpln\n\nor\n\n    nbstripout -t FILE.ipynb | other-command\n\nDo a dry run and only list which files would have been stripped:\n\n    nbstripout --dry-run FILE.ipynb [FILE2.ipynb ...]\n\nor\n\nDo a verification run, which works like dry run but will fail\nif any files would have been stripped:\n\n    nbstripout --verify FILE.ipynb [FILE2.ipynb ...]\n\nOperate on all `.ipynb` files in the current directory and subdirectories\nrecursively:\n\n    find . -name '*.ipynb' -exec nbstripout {} +\n\nPrint the version:\n\n    nbstripout --version\n\nShow help and usage instructions:\n\n    nbstripout --help\n\n### Using as a Git filter\n\nSet up the [git filter](https://git-scm.com/docs/gitattributes#_filter) and\nattributes as described in the manual installation instructions below:\n\n    nbstripout --install\n\nNote: The filter is declared as `required`, meaning the filter *must* succeed.\nFailures and misconfigurations will not simply cause the filter to be ignored.\n\nSet up the git filter using `.gitattributes`:\n\n    nbstripout --install --attributes .gitattributes\n\nSpecify a different path to the Python interpreter to be used for the git\nfilters (default is the path to the Python interpreter used when `nbstripout` is\ninstalled). This is useful if you have Python installed in different or unusual\nlocations across machines, e.g. `/usr/bin/python3` on your machine vs\n`/usr/local/bin/python3` in a container or elsewhere.\n\n    nbstripout --install --python python3\n\nUsing just `python3` lets each machine find its Python itself. However, keep in\nmind that depending on your setup this might not be the Python version you want\nor even fail because an absolute path is required.\n\nSet up the git filter in your global `~/.gitconfig`:\n\n    nbstripout --install --global\n\nSet up the git filter in your system-wide `$(prefix)/etc/gitconfig` (most\ninstallations will require you to `sudo`):\n\n    [sudo] nbstripout --install --system\n\nRemove the git filter and attributes:\n\n    nbstripout --uninstall\n\nRemove the git filter from your global `~/.gitconfig` and attributes:\n\n    nbstripout --uninstall --global\n\nRemove the git filter from your system-wide `$(prefix)/etc/gitconfig` and\nattributes:\n\n    [sudo] nbstripout --uninstall --system\n\nRemove the git filter and attributes from `.gitattributes`:\n\n    nbstripout --uninstall --attributes .gitattributes\n\nCheck if `nbstripout` is installed in the current repository (exits with code 0\nif installed, 1 otherwise):\n\n    nbstripout --is-installed\n\nPrint status of `nbstripout` installation in the current repository and\nconfiguration summary of filter and attributes if installed (exits with code 0\nif installed, 1 otherwise):\n\n    nbstripout --status\n\n### Configuration files\n\nThe following table shows in which files the `nbstripout` filter and attribute\nconfiguration is written to for given extra flags to `--install` and\n`--uninstall`:\n\n| flags                                    | filters                     | attributes                      |\n| ---------------------------------------- | --------------------------- | ------------------------------- |\n| none                                     | `.git/config`               | `.git/info/attributes`          |\n| `--global`                               | `~/.gitconfig`              | `~/.config/git/attributes`      |\n| `--system`                               | `$(prefix)/etc/gitconfig`   | `$(prefix)/etc/gitattributes`   |\n| `--attributes=.gitattributes`            | `.git/config`               | `.gitattributes`                |\n| `--global --attributes=.gitattributes`   | `~/.gitconfig`              | `.gitattributes`                |\n\n### Install globally\n\nUsually, `nbstripout` is installed per repository so you can choose where to use\nit or not. You can choose to set the attributes in `.gitattributes` and commit\nthis file to your repository, however there is no way to have git set up the\nfilters automatically when someone clones a repository. This is by design, to\nprevent you from executing arbitrary and potentially malicious code when cloning\na repository.\n\nTo install `nbstripout` for all your repositories such that you no longer need\nto run the installation once per repository, install as follows:\n\n    mkdir -p ~/.config/git  # This folder may not exist\n    nbstripout --install --global --attributes=~/.config/git/attributes\n\nThis will set up the filters and diff driver in your `~/.gitconfig` and instruct\ngit to apply them to any `.ipynb` file in any repository.\n\nNote that you need to uninstall with the same flags:\n\n    nbstripout --uninstall --global --attributes=~/.config/git/attributes\n\n### Install system-wide\n\nTo install `nbstripout` system-wide so that it applies to all repositories for\nall users, install as follows (most installations will require you to `sudo`):\n\n    [sudo] nbstripout --install --system\n\nThis will set up the filters and diff driver in `$(prefix)/etc/gitconfig` and\ninstruct git to apply them to any `.ipynb` file in any repository for any user.\n\nNote that you need to uninstall with the same flags:\n\n    [sudo] nbstripout --uninstall --system\n\n### Apply retroactively\n\n`nbstripout` can be used to rewrite an existing Git repository using\n[`git filter-repo`](https://github.com/newren/git-filter-repo) to strip output\nfrom existing notebooks. This invocation operates on all ipynb files in the repo:\n\n```sh\n    #!/usr/bin/env bash\n    # get lint-history with callback from https://github.com/newren/git-filter-repo/pull/542\n    ./lint-history.py --relevant 'return filename.endswith(b\".ipynb\")' --callback '\n    import json, warnings, nbformat\n    from nbstripout import strip_output\n    from nbformat.reader import NotJSONError\n    try:\n        with warnings.catch_warnings():\n            warnings.simplefilter(\"ignore\", category=UserWarning)\n            notebook = nbformat.reads(blob.data, as_version=nbformat.NO_CONVERT)\n        # customize to your needs\n        strip_output(notebook, keep_output=False, keep_count=False, keep_id=False, extra_keys=[\"metadata.widgets\",\"metadata.execution\",\"cell.attachments\"], drop_empty_cells=True,  drop_tagged_cells=[],strip_init_cells=False, max_size=0)\n        old_len = len(blob.data)\n        blob.data = (nbformat.writes(notebook) + \"\\n\").encode(\"utf-8\")\n        if old_len != len(blob.data):\n            print(change.blob_id, change.filename, old_len, len(blob.data))\n    except NotJSONError as e:\n         print(\"ERROR\", type(e), change.blob_id, change.filename)\n    '\n```\n\n### Removing empty cells\n\nDrop empty cells i.e. cells where `source` is either empty or only contains\nwhitespace:\n\n    nbstripout --drop-empty-cells\n\n### Removing [init]{.title-ref} cells\n\nBy default `nbstripout` will keep cells with `init_cell: true` metadata. To\ndisable this behavior use:\n\n    nbstripout --strip-init-cells\n\n### Removing entire cells\n\nIn certain conditions it might be handy to remove not only the output, but the\nentire cell, e.g. when developing exercises.\n\nTo drop all cells tagged with \"solution\" run:\n\n    nbstripout --drop-tagged-cells=\"solution\"\n\nThe option accepts a list of tags separated by whitespace.\n\n### Keeping some output\n\nDo not strip the execution count/prompt number:\n\n    nbstripout --keep-count\n\nDo not strip outputs that are smaller that a given max size (useful for removing\nonly large outputs like images):\n\n    nbstripout --max-size 1k\n\nDo not strip the output, only metadata:\n\n    nbstripout --keep-output\n\nDo not reassign the cell ids to be sequential (which is the default behavior):\n\n    nbstripout --keep-id\n\nTo mark special cells so that the output is not stripped, you can either:\n\n1.  Set the `keep_output` tag on the cell. To do this, enable the tags toolbar\n    (View > Cell Toolbar > Tags) and then add the `keep_output` tag for each\n    cell you would like to keep the output for.\n\n2.  Set the `\"keep_output\": true` metadata on the cell. To do this, select the\n    \"Edit Metadata\" Cell Toolbar, and then use the \"Edit Metadata\" button on\n    the desired cell to enter something like:\n\n        {\n          \"keep_output\": true,\n        }\n\nYou can also keep output for an entire notebook. This is useful if you want to\nstrip output by default in an automated environment (e.g. CI pipeline), but want\nto be able to keep outputs for some notebooks. To do so, add the option above to\nthe *notebook* metadata instead. (You can also explicitly remove outputs from a\nparticular cell in these notebooks by adding a cell-level metadata entry.)\n\nAnother use-case is to preserve initialization cells that might load customized\nCSS etc. critical for the display of the notebook. To support this, we also keep\noutput for cells with:\n\n    {\n      \"init_cell\": true,\n    }\n\nThis is the same metadata used by the\n[init_cell nbextension](https://github.com/ipython-contrib/jupyter_contrib_nbextensions/tree/master/src/jupyter_contrib_nbextensions/nbextensions/init_cell).\n\n### Stripping metadata\n\nThe following metadata is stripped by default:\n\n-   Notebook metadata: `signature`, `widgets`\n-   Cell metadata: `ExecuteTime`, `collapsed`, `execution`, `heading_collapsed`,\n    `hidden`, `scrolled`\n\nAdditional metadata to be stripped can be configured via either\n\n-   `git config (--global/--system) filter.nbstripout.extrakeys`, e.g. :\n\n        git config --global filter.nbstripout.extrakeys '\n          metadata.celltoolbar\n          metadata.kernelspec\n          metadata.language_info.codemirror_mode.version\n          metadata.language_info.pygments_lexer\n          metadata.language_info.version\n          metadata.toc\n          metadata.notify_time\n          metadata.varInspector\n          cell.metadata.heading_collapsed\n          cell.metadata.hidden\n          cell.metadata.code_folding\n          cell.metadata.tags\n          cell.metadata.init_cell'\n\n-   the `--extra-keys` flag, which takes a space-delimited string as an\n    argument, e.g. :\n\n        --extra-keys=\"metadata.celltoolbar cell.metadata.heading_collapsed\"\n\nNote: Only notebook and cell metadata is currently supported and every key\nspecified via `filter.nbstripout.extrakeys` or `--extra-keys` must start with\n`metadata.` for notebook and `cell.metadata.` for cell metadata.\n\nYou can keep certain metadata that would be stripped by default with either\n\n-   `git config (--global/--system) filter.nbstripout.keepmetadatakeys`, e.g.:\n\n        git config --global filter.nbstripout.keepmetadatakeys '\n          cell.metadata.collapsed\n          cell.metadata.scrolled'\n\n-   the `--keep-metadata-keys` flag, which takes a space-delimited string as an\n    argument, e.g.:\n\n        --keep-metadata-keys=\"cell.metadata.collapsed cell.metadata.scrolled\"\n\nNote: Previous versions of Jupyter used `metadata.kernel_spec` for kernel\nmetadata. Prefer stripping `kernelspec` entirely: only stripping some attributes\ninside `kernelspec` may lead to errors when opening the notebook in Jupyter (see\n[#141](https://github.com/kynan/nbstripout/issues/141)).\n\n### Excluding files and folders\n\nTo exclude specific files or folders from being processed by the `nbstripout`\nfilters, add the path and exception to your filter specifications defined in\n`.git/info/attributes` or `.gitattributes`:\n\n    docs/** filter= diff=\n\nThis will disable `nbstripout` for any file in the `docs` directory.:\n\n    notebooks/Analysis.ipynb filter= diff=\n\nThis will disable `nbstripout` for the file `Analysis.ipynb` located in the\n`notebooks` directory.\n\nTo check which attributes a given file has with the current config, run:\n\n    git check-attr -a -- path/to/file\n\nFor a file to which the filter applies you will see the following:\n\n    $ git check-attr -a -- foo.ipynb\n    foo.ipynb: diff: ipynb\n    foo.ipynb: filter: nbstripout\n\nFor a file in your excluded folder you will see the following:\n\n    $ git check-attr -a -- docs/foo.ipynb\n    foo.ipynb: diff:\n    foo.ipynb: filter:\n\n## Manual filter installation\n\nSet up a git filter and diff driver using nbstripout as follows:\n\n    git config filter.nbstripout.clean '/path/to/nbstripout'\n    git config filter.nbstripout.smudge cat\n    git config filter.nbstripout.required true\n    git config diff.ipynb.textconv '/path/to/nbstripout -t'\n\nThis will add a section to the `.git/config` file of the current repository.\n\nIf you want the filter to be installed globally for your user, add the\n`--global` flag to the `git config` invocations above to have the configuration\nwritten to your `~/.gitconfig` and apply to all repositories.\n\nIf you want the filter to be installed system-wide, add the `--system` flag to\nthe `git config` invocations above to have the configuration written to\n`$(prefix)/etc/gitconfig` and apply to all repositories for all users.\n\nCreate a file `.gitattributes` (if you want it versioned with the repository) or\n`.git/info/attributes` (to apply it only to the current repository) with the\nfollowing content:\n\n    *.ipynb filter=nbstripout\n    *.ipynb diff=ipynb\n\nThis instructs git to use the filter named `nbstripout` and the diff driver\nnamed `ipynb` set up in the git config above for every `.ipynb` file in the\nrepository.\n\nIf you want the attributes be set for `.ipynb` files in any of your git\nrepositories, add those two lines to `~/.config/git/attributes`. Note that this\nfile and the `~/.config/git` directory may not exist.\n\nIf you want the attributes be set for `.ipynb` files in any git repository on\nyour system, add those two lines to `$(prefix)/etc/gitattributes`. Note that\nthis file may not exist.\n\n## Using `nbstripout` as a pre-commit hook\n\n[pre-commit](https://pre-commit.com) is a framework for managing git\n[pre-commit hooks](https://git-scm.com/docs/githooks).\n\nOnce you have [pre-commit](https://pre-commit.com) installed, add the following\nto the `.pre-commit-config.yaml` in your repository:\n\n    repos:\n    - repo: https://github.com/kynan/nbstripout\n      rev: 0.8.1\n      hooks:\n        - id: nbstripout\n\nThen run `pre-commit install` to activate the hook.\n\n> [!WARNING]\n>\n> In this mode, `nbstripout` is used as a git hook to strip any `.ipynb` files\n> before committing. This also modifies your working copy!\n>\n> In its regular mode, `nbstripout` acts as a filter and only modifies what git\n> gets to see for committing or diffing. The working copy stays intact.\n\n## Troubleshooting\n\n### Known issues\n\nCertain Git workflows are not well supported by `nbstripout`:\n\n-   Local changes to notebook files that are made invisible to Git due to the\n    `nbstripout` filter do still cause conflicts when attempting to sync\n    upstream changes (`git pull`, `git merge` etc.). This is because Git has no\n    way of resolving a conflict caused by a non-stripped local file being merged\n    with a stripped upstream file.  Addressing this issue is out of scope for\n    `nbstripout`. Read more and find workarounds in\n    [#108](https://github.com/kynan/nbstripout/issues/108).\n\n### Show files processed by nbstripout filter\n\nGit has [no builtin support](https://stackoverflow.com/a/52065333/396967) for\nlisting files a clean or smudge filter operates on. As a workaround, change the\nsetup of your filter in `.git/config`, `~/.gitconfig` or\n`$(prefix)/etc/gitconfig` as follows to see the filenames either filter operates\non:\n\n    [filter \"nbstripout\"]\n        clean  = \"f() { echo >&2 \\\"clean: nbstripout $1\\\"; nbstripout; }; f %f\"\n        smudge = \"f() { echo >&2 \\\"smudge: cat $1\\\"; cat; }; f %f\"\n        required = true\n",
    "bugtrack_url": null,
    "license": "License :: OSI Approved :: MIT License",
    "summary": "Strips outputs from Jupyter and IPython notebooks",
    "version": "0.8.1",
    "project_urls": {
        "Homepage": "https://github.com/kynan/nbstripout"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "cf9193b459c456b0e4389b2b3ddb3b82cd401d022691334a0f06e92c2046e780",
                "md5": "6899153d39827a2d129d890a681050a5",
                "sha256": "79a8c8da488d98c54c112fa87185045f0271a97d84f1d46918d6a3ee561b30e7"
            },
            "downloads": -1,
            "filename": "nbstripout-0.8.1-py2.py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "6899153d39827a2d129d890a681050a5",
            "packagetype": "bdist_wheel",
            "python_version": "py2.py3",
            "requires_python": ">=3.8",
            "size": 16329,
            "upload_time": "2024-11-17T10:38:31",
            "upload_time_iso_8601": "2024-11-17T10:38:31.803514Z",
            "url": "https://files.pythonhosted.org/packages/cf/91/93b459c456b0e4389b2b3ddb3b82cd401d022691334a0f06e92c2046e780/nbstripout-0.8.1-py2.py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "926e05d7e0e35598bd0d423167295f978005912a2dcd137c88ebf36e34047dc7",
                "md5": "b93a972af3633482bd6118fbd86d7438",
                "sha256": "eaac8b6b4e729e8dfe1e5df2c0f8ba44abc5a17a65448f0480141f80be230bb1"
            },
            "downloads": -1,
            "filename": "nbstripout-0.8.1.tar.gz",
            "has_sig": false,
            "md5_digest": "b93a972af3633482bd6118fbd86d7438",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 26399,
            "upload_time": "2024-11-17T10:38:33",
            "upload_time_iso_8601": "2024-11-17T10:38:33.275790Z",
            "url": "https://files.pythonhosted.org/packages/92/6e/05d7e0e35598bd0d423167295f978005912a2dcd137c88ebf36e34047dc7/nbstripout-0.8.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-11-17 10:38:33",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "kynan",
    "github_project": "nbstripout",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "nbstripout"
}
        
Elapsed time: 4.88318s