Skip to content

Instantly share code, notes, and snippets.

@mdboom
Created January 7, 2013 21:23
Show Gist options
  • Save mdboom/4478561 to your computer and use it in GitHub Desktop.
Save mdboom/4478561 to your computer and use it in GitHub Desktop.
Filter an IPython notebook so that lines of console output that were written over by carriage return characters (\\r) are removed.
"""
Filter an IPython notebook so that lines of console output that were
written over by carriage return characters (\\r) are removed.
This is a UNIX filter, so use as::
cat my_notebook.ipynb | nb_filter.py > my_notebook.filtered.ipynb
"""
import json
import re
import sys
def filter(s):
while True:
match = re.search(ur'(?:^|\r|\n)(.*?\r(?!\n))', s)
if match is not None:
s = s[:match.start()] + s[match.end():]
else:
break
return s
content = json.load(sys.stdin)
for worksheet in content['worksheets']:
for cell in worksheet['cells']:
texts = {}
for output in cell['outputs']:
if output['output_type'] == 'stream':
for line in output['text']:
texts.setdefault(output['stream'], []).append(line)
cell['outputs'] = []
for stream, text in texts.items():
text = ''.join(text)
text = filter(text)
cell['outputs'].append({
'output_type': 'stream',
'stream': stream,
'text': [text]})
json.dump(content, sys.stdout, indent=1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment