Skip to content

Instantly share code, notes, and snippets.

@sizhky
Forked from damianavila/remove_output.py
Last active January 5, 2023 14:18
Show Gist options
  • Save sizhky/a79c5924d92e85fbaaded342ae489bfa to your computer and use it in GitHub Desktop.
Save sizhky/a79c5924d92e85fbaaded342ae489bfa to your computer and use it in GitHub Desktop.
Remove output from Jupyter notebook from the command line (dev version 1.0)
import sys
import io
import os
from IPython.nbformat.current import read, write
def remove_outputs(nb):
"""remove the outputs from a notebook"""
for ws in nb.worksheets:
for cell in ws.cells:
if cell.cell_type == 'code':
cell.outputs = []
def main(fname):
with io.open(fname, 'r', encoding='latin-1') as f:
nb = read(f, 'json')
remove_outputs(nb)
base, ext = os.path.splitext(fname)
new_ipynb = f"{base}_removed.ipynb"
with io.open(new_ipynb, 'w', encoding='latin-1') as f:
write(nb, f, 'json')
if __name__ == '__main__':
main(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment