Skip to content

Instantly share code, notes, and snippets.

@DavidMertz
Last active August 29, 2015 14:23
Show Gist options
  • Save DavidMertz/65f0aeed79bce905fd37 to your computer and use it in GitHub Desktop.
Save DavidMertz/65f0aeed79bce905fd37 to your computer and use it in GitHub Desktop.
Customizing git to checkin Jupyter Notebooks w/o output

Configure your system to look like following to make sure that Notebooks will always be pushed to GitHub without their output. This will make for smaller saved files, but more relevantly, also ones that are easier to diff (and that git will merge more cleanly).

% cat ~/.gitattributes
*.ipynb    filter=dropoutput_ipynb

% cat ~/bin/ipynb_output_filter.py
#!/usr/bin/env python
import sys
version = None

if sys.version[0] == '2':
    reload(sys)
    sys.setdefaultencoding('utf8')

try:
    # Jupyter
    from jupyter_nbformat import reads, write
except ImportError:
    try:
        # New IPython
        from IPython.nbformat import reads, write
    except ImportError:
        # Old IPython
        from IPython.nbformat.current import reads, write
        version = 'json'

to_parse = sys.stdin.read()

if not version:
    import json
    json_in = json.loads(to_parse)
    version = json_in['nbformat']

json_in = reads(to_parse, version)

if hasattr(json_in, 'worksheets'):
    # IPython
    sheets = json_in.worksheets
else:
    # Jupyter
    sheets = [json_in]

for sheet in sheets:
    for cell in sheet.cells:
        if "outputs" in cell:
            cell.outputs = []
        for field in ("prompt_number", "execution_number"):
            if field in cell:
                del cell[field]
        for field in ("execution_count",):
            if field in cell:
                cell[field] = None

if 'signature' in json_in.metadata:
    json_in.metadata['signature'] = ""

write(json_in, sys.stdout, version)

% 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
% chmod +x ~/bin/ipynb_output_filter.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment