Skip to content

Instantly share code, notes, and snippets.

@kangwonlee
Created August 13, 2017 14: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 kangwonlee/11ed347d45c04ef99ceed6b4835f3e9f to your computer and use it in GitHub Desktop.
Save kangwonlee/11ed347d45c04ef99ceed6b4835f3e9f to your computer and use it in GitHub Desktop.
remove ipython outputs and execution numbers
"""
Jupyter notebook could be good for educational purpose.
Before release in class, output generated during testing and execution number need to be removed.
"""
import os
import sys
import nbformat
# Use this module to read or write notebook files as particular nbformat versions.
def read_file(nb_filename):
assert os.path.exists(nb_filename)
txt = ''
with open(nb_filename, 'rb') as nb_file:
txt = nb_file.read()
nb_node = nbformat.reads(txt, nbformat.NO_CONVERT)
return nb_node
def process_nb_node(nb_node):
for cell in nb_node['cells']:
if 'code' == cell['cell_type']:
if 'outputs' in cell:
cell['outputs'] = []
if 'execution_count' in cell:
cell['execution_count'] = None
return nb_node
def write_file(nb_node, nb_filename):
nbformat.write(nb_node, nb_filename)
def process_nb_file(nb_filename):
nb_node = read_file(nb_filename)
processed_node = process_nb_node(nb_node)
write_file(processed_node, nb_filename)
if __name__ == '__main__':
def main(argv):
if 1 < len(argv):
filename = argv[1]
process_nb_file(filename)
else:
print("Usage : python %s <notebook file path>" % os.path.split(__file__)[-1])
help(nbformat)
main(sys.argv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment