Skip to content

Instantly share code, notes, and snippets.

@udibr
Last active February 22, 2016 00:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save udibr/1d4c71b79dfd360b087d to your computer and use it in GitHub Desktop.
Save udibr/1d4c71b79dfd360b087d to your computer and use it in GitHub Desktop.
A command line for cleaning ipython notebook from the content of the output cells
import sys
if len(sys.argv) < 2:
print "Usage: %s <file-name>"%sys.argv[0]
exit(0)
fname = sys.argv[1]
import json
lookup = {'outputs':[], 'execution_count':None}
def filter(d):
if isinstance(d,dict):
d = dict((k,lookup.get(k,filter(v))) for k,v in d.iteritems())
elif isinstance(d,list):
d = map(filter,d)
return d
for fname in sys.argv[1:]:
with open(fname,'r') as fp:
d = json.load(fp)
d = filter(d)
with open(fname, 'w') as fp:
json.dump(d, fp, sort_keys=True, indent=4, separators=(',', ': '))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment