Last active
January 8, 2017 10:29
-
-
Save mratkovic/3e72de2d0fe464f11f9cb8292674e406 to your computer and use it in GitHub Desktop.
Python script that clears all output cells from given notebook
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
import argparse | |
from nbformat import read, write, NO_CONVERT | |
def main(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument("notebook", help="path to jupyter notebook") | |
parser.add_argument("-o", "--output", default="processed.ipynb", help="output path") | |
args = parser.parse_args() | |
src = args.notebook | |
output = args.output | |
print("Loading notebook...") | |
with open(src, 'r') as f: | |
nb = read(f, NO_CONVERT) | |
print("Removing all output...") | |
out_cells = filter(lambda c: c.cell_type == 'code', nb.cells) | |
for cell in out_cells: | |
cell.outputs = [] | |
print("Saving to %s..." % output) | |
with open(output, 'w', encoding='utf8') as f: | |
write(nb, f, NO_CONVERT) | |
print("Completed...") | |
if __name__ == '__main__': | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment