How can I open an IPython notebook without the output?

前端 未结 4 1023
感情败类
感情败类 2021-02-01 07:25

I have an IPython notebook where I\'ve accidentally dumped a huge output (15 MB) that crashed the notebook. Now when I open the notebook and attempt to delete the troubleso

4条回答
  •  我在风中等你
    2021-02-01 07:58

    Here is a further modification from @Edward Fung's answer that will output the cleaned notebook to a new file rather than rely on stin and stout

    from nbformat import read, write
    
    def strip_output(nb):
        for cell in nb.cells:
            if hasattr(cell, "outputs"):
                cell.outputs = []
            if hasattr(cell, "prompt_number"):
                del cell["prompt_number"]
    
    nb = read(open("my_notebook.ipynb"), 4)
    strip_output(nb)
    write(nb, open("my_notebook_cleaned.ipynb", "w"), 4)
    

提交回复
热议问题