Last active
August 24, 2022 07:00
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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