Last active
April 12, 2023 08:57
-
-
Save npyoung/799108bda3c6e7ecb0364dca924914b3 to your computer and use it in GitHub Desktop.
Files for making git repos handle IPython/Jupyter notebooks well
This file contains 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
*.ipynb filter=stripoutput |
This file contains 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
git config --local filter.stripoutput.clean "python nbstripout" |
This file contains 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 | |
"""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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.