Page MenuHomec4science

nbstripout
No OneTemporary

File Metadata

Created
Tue, Nov 26, 06:40

nbstripout

#!/usr/bin/env python3
"""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
# jk: found this here: https://github.com/cfriedline/ipynb_template/blob/master/nbstripout
# jk config:
In .gitattributes add the line:
*.ipynb filter=stripoutput
IN .git/config add the lines (!!adapt path to nbconvert!!):
[filter "stripoutput"]
clean = /home/krausej/bin/nbstripout
smudge = cat
required = true
"""
import sys
from nbformat import v4
def strip_output(nb):
"""strip the outputs from a notebook object"""
for cell in nb.cells:
if 'outputs' in cell:
cell['outputs'] = []
if 'execution_count' in cell:
cell['execution_count'] = 0
return nb
if __name__ == '__main__':
nb = v4.reads(sys.stdin.read())
nb = strip_output(nb)
sys.stdout.write(v4.writes(nb))

Event Timeline