Skip to content

Instantly share code, notes, and snippets.

@bsmith89
Last active August 24, 2022 07:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bsmith89/4a32efdeda6495d2ba4e to your computer and use it in GitHub Desktop.
Save bsmith89/4a32efdeda6495d2ba4e to your computer and use it in GitHub Desktop.
Git clean/smudge filter for removing output and line numbers from IPython Notebooks.
#! /usr/bin/env python
"""From http://stackoverflow.com/a/20844506/1951857
To use::
# Add the script to your path
chmod +x path/to/this/ipynb_output_filter.py
echo "*.ipynb filter=dropoutput_ipynb" >> ~/.gitattributes
git config --global core.attributesfile ~/.gitattributes
git config --global filter.dropoutput_ipynb.clean ipynb_output_filter.py
git config --global filter.dropoutput_ipynb.smudge cat
When you run ``git status`` you'll see changes not yet staged, but
diff-ing, committing, etc. should all ignore the output/prompt number
portions of the notebook.
You may find that ``git add *.ipynb`` cleans up your status output without
changing the content of the staging area.
"""
import sys
from IPython.nbformat.current import read, write
json_in = read(sys.stdin, 'json')
for sheet in json_in.worksheets:
for cell in sheet.cells:
if "outputs" in cell:
cell.outputs = []
if "prompt_number" in cell:
cell.pop("prompt_number")
write(json_in, sys.stdout, 'json')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment