-
-
Save dlithio/a3f001634f67d00d7ae0 to your computer and use it in GitHub Desktop.
Make ipython notebook files place nice with git. Remove output from notebooks when doing diffs and commits.
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 | |
# this script filters output from ipython notebooks, for use in git repos | |
# http://stackoverflow.com/questions/18734739/using-ipython-notebooks-under-version-control | |
# | |
# put this file in a `bin` directory in your home directory, then run: | |
# | |
# echo -e "*.ipynb \t filter=dropoutput_ipynb" >> ~/.gitattributes | |
# git config --global core.attributesfile ~/.gitattributes | |
# git config --global filter.dropoutput_ipynb.clean ~/bin/ipynb_output_filter.py | |
# git config --global filter.dropoutput_ipynb.smudge cat | |
import sys | |
from IPython.nbformat import read, write, NO_CONVERT | |
json_in = read(sys.stdin, NO_CONVERT) | |
# detect earlier versions | |
if('worksheets' in json_in): | |
# versions prior to 4 had a 'worksheets' field with a single element | |
sheet = json_in.worksheets[0] | |
else: | |
sheet = json_in | |
for cell in sheet.cells: | |
if "outputs" in cell: | |
cell.outputs = [] | |
if "prompt_number" in cell: | |
cell.prompt_number = None | |
if "execution_count" in cell: | |
cell.execution_count = 0 | |
write(json_in, sys.stdout, NO_CONVERT) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment