nbstripout

0.9.1 · active · verified Sun Apr 12

nbstripout is a Python utility that strips outputs, metadata, and other extraneous content from Jupyter and IPython notebooks. It's most commonly used as a Git hook to prevent large, noisy diffs and ensure clean notebooks in version control. The current version is 0.9.1, and it maintains an active release cadence with several updates per year.

Warnings

Install

Imports

Quickstart

The primary use case for nbstripout is as a Git hook, which automatically strips notebook outputs and metadata before committing. The `--install` command sets this up for a repository. A programmatic example demonstrates how to use the `strip_output` function directly in Python.

# 1. Install nbstripout
pip install nbstripout

# 2. Install the git hook in your repository
# This ensures outputs are stripped automatically before committing.
# Navigate to your git repository first.
# os.system('nbstripout --install') # Uncomment to run, but be aware it modifies your .git/config

# 3. Example of programmatic usage (optional)
from nbstripout import strip_output
import json

# Simulate reading a notebook file
example_notebook_content = {
    "cells": [
        {
            "cell_type": "code",
            "execution_count": 1,
            "metadata": {},
            "outputs": [
                {
                    "name": "stdout",
                    "output_type": "stream",
                    "text": ["Hello, nbstripout!\n"]
                }
            ],
            "source": "print('Hello, nbstripout!')"
        }
    ],
    "metadata": {
        "kernelspec": {
            "display_name": "Python 3",
            "language": "python",
            "name": "python3"
        }
    },
    "nbformat": 4,
    "nbformat_minor": 5
}

# Strip outputs from the notebook content
stripped_notebook_content = strip_output(json.dumps(example_notebook_content))

# Print the stripped content (outputs should be empty)
print("\n--- Original Notebook ---")
print(json.dumps(example_notebook_content, indent=2))
print("\n--- Stripped Notebook ---")
print(json.dumps(json.loads(stripped_notebook_content), indent=2))

view raw JSON →