How to clear an IPython Notebook's output in all cells from the Linux terminal?

一笑奈何 提交于 2019-11-28 23:47:49

问题


I have a problem when the output from a notebook is really long and it's saved into the notebook, any time I want to open this particular notebook again the browser crashes and can't display correctly.

To fix this I have to open it with a text editor and delete all output from that cell causing the problem.

I wonder if there is a way to clean all output from the notebook so one can open it again without problem. I want to delete all output since deleting a specific one seems more troublesome.


回答1:


--ClearOutputPreprocessor.enabled=True

There is now a built-in command line option to do it:

jupyter nbconvert --ClearOutputPreprocessor.enabled=True --inplace Notebook.ipynb

Or to another file called NotebookNoOut.ipynb:

jupyter nbconvert --ClearOutputPreprocessor.enabled=True \
  --to notebook --output=NotebookNoOut Notebook.ipynb

jupyter nbconvert --help also documents a --clear-output option, but for some reason it did not work.

Tested in Jupyter 4.4.0, notebook==5.7.6.




回答2:


Use clean_ipynb, which not only clears notebook output but can also clean the code.

Install by pip install clean_ipynb

Run by clean_ipynb hello.ipynb




回答3:


If you create a .gitattributes file, you can run a filter over certain files before they are added to git. This will leave the original file on disk as-is, but commit the "cleaned" version.

For this to work, add this to your local .git/config or global ~/.gitconfig:

[filter "strip-notebook-output"]
    clean = "jupyter nbconvert --ClearOutputPreprocessor.enabled=True --to=notebook --stdin --stdout --log-level=ERROR"

Then create a .gitattributes file in your directory with notebooks, with this content:

*.ipynb filter=strip-notebook-output

How this works:

  • The attribute tells git to run the filter's clean action on each notebook file before adding it to the index (staging).
  • The filter is our friend nbconvert, set up to read from stdin, write to stdout, strip the output, and only speak when it has something important to say.
  • When a file is extracted from the index, the filter's smudge action is run, but this is a no-op as we did not specify it. You could run your notebook here to re-create the output (nbconvert --execute).
  • Note that if the filter somehow fails, the file will be staged unconverted.

My only minor gripe with this process is that I can commit .gitattributes but I have to tell my co-workers to update their .git/config.

If you want a hackier but much faster version, try JQ:

  clean = "jq '.cells[].outputs = [] | .cells[].execution_count = null | .'"



回答4:


Use --ClearOutputPreprocessor.enabled=True and --clear-output

Following this command:

jupyter nbconvert --ClearOutputPreprocessor.enabled=True --clear-output *.ipynb



来源:https://stackoverflow.com/questions/28908319/how-to-clear-an-ipython-notebooks-output-in-all-cells-from-the-linux-terminal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!