forked from clawpack/riemann
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request clawpack#41 from rjleveque/nbstripout
added apps/notebooks/nbstripout
- Loading branch information
Showing
1 changed file
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#!/usr/bin/env python | ||
"""strip outputs from an IPython Notebook | ||
Opens a notebook, strips its output, and writes the outputless version to the original file. | ||
Useful mainly as a git pre-commit hook for users who don't want to track output in VCS. | ||
This does mostly the same thing as the `Clear All Output` command in the notebook UI. | ||
From: https://gist.github.com/minrk/6176788 | ||
""" | ||
|
||
import io | ||
import sys | ||
|
||
from IPython.nbformat import current | ||
|
||
def strip_output(nb): | ||
"""strip the outputs from a notebook object""" | ||
nb.metadata.pop('signature', None) | ||
for cell in nb.worksheets[0].cells: | ||
if 'outputs' in cell: | ||
cell['outputs'] = [] | ||
if 'prompt_number' in cell: | ||
cell['prompt_number'] = None | ||
return nb | ||
|
||
if __name__ == '__main__': | ||
filename = sys.argv[1] | ||
with io.open(filename, 'r', encoding='utf8') as f: | ||
nb = current.read(f, 'json') | ||
nb = strip_output(nb) | ||
with io.open(filename, 'w', encoding='utf8') as f: | ||
current.write(nb, f, 'json') | ||
|