Last active
October 19, 2015 19:32
-
-
Save alexrutherford/131d7dd1d06a02b596b5 to your computer and use it in GitHub Desktop.
Clears output from Jupyter notebooks
This file contains hidden or 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/python | |
| import json,os,sys | |
| ############ | |
| def main(): | |
| ############ | |
| ''' | |
| Usage: python clear_output.py name.ipynb | |
| Output name_clean.ipynb [--inplace] | |
| Script to clean output from IPython notebooks | |
| Useful if large output makes notebook very large to load | |
| Shamelessly based on gist at https://gist.github.com/damianavila/5305869 | |
| Updated for Jupyter notebook schema. | |
| ''' | |
| try: | |
| inFileName=sys.argv[1] | |
| base,ext=os.path.splitext(sys.argv[1]) | |
| outFileName='%s_clean%s' % (base,ext) | |
| except: | |
| print 'Enter file name' | |
| sys.exit(1) | |
| with open(inFileName) as inFileHandle: | |
| data=json.load(inFileHandle) | |
| if sys.argv[2]=='--inplace': | |
| answer=raw_input('Overwrite? [y/N]') | |
| if answer.lower() in ['y','yes']: | |
| overwrite=True | |
| print 'Overwriting %s....' % inFileName | |
| # print data.keys() | |
| # print data['cells'][0] | |
| nCleaned=0 | |
| for n,cell in enumerate(data['cells']): | |
| #print '++++',cell.keys(),cell['metadata'],cell['cell_type'] | |
| # print '+++' | |
| if cell['cell_type']=='code': | |
| for nn,o in enumerate(cell['outputs']): | |
| # print o.keys(),o.get('output_type') | |
| data['cells'][n]['outputs']=[] | |
| nCleaned+=1 | |
| print 'Cleaned %d cells' % (nCleaned) | |
| with open(outFileName,'w') as outFileHandle: | |
| json.dump(data,outFileHandle) | |
| if overwrite: | |
| print 'Written to %s' % inFileName | |
| os.rename(outFileName,inFileName) | |
| else: | |
| print 'Written to %s' % outFileName | |
| if __name__=='__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment