Skip to content

Instantly share code, notes, and snippets.

@philkuz
Created August 23, 2016 03:50
Show Gist options
  • Save philkuz/e5f08763e433b00dbbeb3504cf6252fa to your computer and use it in GitHub Desktop.
Save philkuz/e5f08763e433b00dbbeb3504cf6252fa to your computer and use it in GitHub Desktop.
Filter out python notebooks
*.ipynb filter=dropoutput_ipynb
[filter "dropoutput_ipynb"]
clean = $(which python) ./ipynb_output_filter.py
smudge = cat
#!/usr/bin/env python
import sys
try:
runarg_idx = sys.argv.index('--rundir')
rundir = sys.argv[runarg_idx+1]
import os
os.chdir(os.path.expanduser(rundir))
except ValueError:
pass
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 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 "metadata" in cell:
for field in ("collapsed", "scrolled"):
if field in cell.metadata:
del cell.metadata[field]
if hasattr(sheet.metadata, "widgets"):
del sheet.metadata["widgets"]
if hasattr(sheet.metadata, "language_info"):
if hasattr(sheet.metadata.language_info, "version"):
del sheet.metadata.language_info["version"]
if 'signature' in json_in.metadata:
json_in.metadata['signature'] = ""
write(json_in, sys.stdout, version)
@philkuz
Copy link
Author

philkuz commented Aug 23, 2016

BEFORE WORKING ON THIS REPO

To accomodate the ipython notebooks, this repo has included a script and config file to ensure
that output is not committed to the repo. Because of security issues you must set this up
manually when you first clone.

Simply run
git config --local include.path ../.gitconfig
and you'll be golden. Ignoring to do so will get you shunned.

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