Created
August 13, 2017 14:57
-
-
Save kangwonlee/11ed347d45c04ef99ceed6b4835f3e9f to your computer and use it in GitHub Desktop.
remove ipython outputs and execution numbers
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
""" | |
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