Skip to content

Instantly share code, notes, and snippets.

@andreipit
Forked from damianavila/remove_output.py
Last active May 29, 2019 10:40
Show Gist options
  • Select an option

  • Save andreipit/e89887582b67ec428c1aba175c7979a2 to your computer and use it in GitHub Desktop.

Select an option

Save andreipit/e89887582b67ec428c1aba175c7979a2 to your computer and use it in GitHub Desktop.

Revisions

  1. andreipit revised this gist May 29, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion remove_output.py
    Original file line number Diff line number Diff line change
    @@ -25,4 +25,4 @@ def remove_outputs(nb):
    new_ipynb = "%s_removed%s" % (base, ext)
    with io.open(new_ipynb, 'w', encoding='utf8') as f:
    write(nb, f, 'json')
    print "wrote %s" % new_ipynb
    print ("wrote %s" % new_ipynb)
  2. @damianavila damianavila created this gist Apr 3, 2013.
    28 changes: 28 additions & 0 deletions remove_output.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    """
    Usage: python remove_output.py notebook.ipynb [ > without_output.ipynb ]
    Modified from remove_output by Minrk
    """
    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 = []

    if __name__ == '__main__':
    fname = sys.argv[1]
    with io.open(fname, 'r') as f:
    nb = read(f, 'json')
    remove_outputs(nb)
    base, ext = os.path.splitext(fname)
    new_ipynb = "%s_removed%s" % (base, ext)
    with io.open(new_ipynb, 'w', encoding='utf8') as f:
    write(nb, f, 'json')
    print "wrote %s" % new_ipynb