Skip to content

Instantly share code, notes, and snippets.

@npyoung
Last active April 12, 2023 08:57
Show Gist options
  • Save npyoung/799108bda3c6e7ecb0364dca924914b3 to your computer and use it in GitHub Desktop.
Save npyoung/799108bda3c6e7ecb0364dca924914b3 to your computer and use it in GitHub Desktop.
Files for making git repos handle IPython/Jupyter notebooks well
*.ipynb filter=stripoutput
git config --local filter.stripoutput.clean "python nbstripout"
#!/usr/bin/env python
"""strip outputs from an IPython Notebook
Opens a notebook, strips its output, and writes the outputless version to the original file.
Useful mainly as a git pre-commit hook for users who don't want to track output in VCS.
This does mostly the same thing as the `Clear All Output` command in the notebook UI.
Adapted from rom https://gist.github.com/minrk/6176788 to work with
git filter driver
"""
import sys, codecs
#You may need to do this for your script to work with GitX or Tower:
#sys.path.append("/Users/chris/anaconda/envs/conda/lib/python2.7/site-packages")
import nbformat as fmt
def strip_output(nb):
"""strip the outputs from a notebook object"""
for cell in nb.cells:
if 'outputs' in cell:
cell['outputs'] = []
if 'prompt_number' in cell:
cell['prompt_number'] = None
elif 'execution_count' in cell:
cell['execution_count'] = None
return nb
if __name__ == '__main__':
nb = fmt.read(sys.stdin, fmt.NO_CONVERT)
nb = strip_output(nb)
if sys.version_info < (3, 0):
sys.stdout = codecs.getwriter('utf8')(sys.stdout)
fmt.write(nb, sys.stdout, fmt.NO_CONVERT)
@npyoung
Copy link
Author

npyoung commented Jan 19, 2021

Place all files in your repo's root. Then run configure-git, or copy and paste the commands on Windows.

Configuration must be done on all instances of the repo, i.e. each time you clone. This process cannot be automated for security reasons.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment