如何在Linux终端的所有单元格中清除IPython Notebook的输出?

san*_*206 30 linux ipython-notebook

我有一个问题,当笔记本电脑的输出很长,并保存到笔记本电脑,任何时候我想再次打开这个特定的笔记本电脑浏览器崩溃,无法正确显示.

要修复此问题,我必须使用文本编辑器打开它,并删除该单元格中的所有输出,从而导致问题.

我想知道是否有办法清理笔记本电脑的所有输出,这样可以再次打开它而没有问题.我想删除所有输出,因为删除一个特定的输出似乎更麻烦.

Cir*_*四事件 49

--ClearOutputPreprocessor.enabled=True

现在有一个内置的命令行选项来执行它:

jupyter nbconvert --ClearOutputPreprocessor.enabled=True --inplace Notebook.ipynb
Run Code Online (Sandbox Code Playgroud)

或者到另一个文件:

jupyter nbconvert --ClearOutputPreprocessor.enabled=True \
  --to notebook --output=NotebookNoOut Notebook.ipynb
Run Code Online (Sandbox Code Playgroud)

NotebookNoOut.ipynb也记录了一个jupyter nbconvert --help选项,但由于某种原因它没有用.

在Jupyter 4.4.0中测试过.

  • 我读得太快了你的评论并且不知道`--inplace`选项; 我学到了一些东西 但是对于我的5.3.1版本来说,选项`--clear-output`是可用的,它总结了`--ClearOutputPreprocessor.enabled = True --inplace` (2认同)
  • 选项 `--clear-output` 已损坏,请参阅问题 [#822](https://github.com/jupyter/nbconvert/issues/822)。该问题已于上个月(2020 年 7 月)得到修复,因此应该会在下一个版本中再次运行。 (2认同)

dir*_*jot 30

如果您创建了一个.gitattributes 文件,您可以在将某些文件添加到 git 之前对其运行过滤器。这会将原始文件保留在磁盘上,但提交“清理过的”版本。

为此,请将其添加到您的 local.git/config或 global ~/.gitconfig

[filter "strip-notebook-output"]
    clean = "jupyter nbconvert --ClearOutputPreprocessor.enabled=True --to=notebook --stdin --stdout --log-level=ERROR"
Run Code Online (Sandbox Code Playgroud)

然后.gitattributes使用笔记本在您的目录中创建一个文件,内容如下:

*.ipynb filter=strip-notebook-output
Run Code Online (Sandbox Code Playgroud)

这是如何工作的:

  • 该属性告诉 gitclean在将每个笔记本文件添加到索引(暂存)之前对它运行过滤器的操作。
  • 过滤器是我们的朋友nbconvert,设置为从标准输入读取、写入标准输出、剥离输出,并且仅在它有重要的事情要说时才说话。
  • 当从索引中提取文件时,smudge会运行过滤器的操作,但由于我们没有指定它,因此这是一个空操作。您可以在此处运行您的笔记本以重新创建输出 ( nbconvert --execute)。
  • 请注意,如果过滤器以某种方式失败,文件将被暂存而不转换。

我对这个过程唯一的小抱怨是我可以提交,.gitattributes但我必须告诉我的同事更新他们的.git/config.

如果你想要一个 hackier 但更快的版本,试试JQ

  clean = "jq '.cells[].outputs = [] | .cells[].execution_count = null | .'"
Run Code Online (Sandbox Code Playgroud)


小智 10

nbstripout对我来说效果很好。

打开 Jupyter 终端,导航到包含笔记本的文件夹,然后运行以下行:

nbstripout my_notebook.ipynb

  • 对于大多数人来说可能是显而易见的,但您需要首先安装 nbstripout,例如: pip install nbstripout (5认同)
  • 非常好 - 甚至是 `nbstripout *.ipynb` :) (2认同)

小智 8

使用--ClearOutputPreprocessor.enabled=True--clear-output

按照这个命令:

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


gb2*_*b2d 7

要扩展 @dirkjot 的答案来解决有关共享配置的问题:

创建本地 .gitconfig 文件,而不是修改 .git/config。这使得需要在其他机器上运行的命令稍微简单一些。您还可以创建一个脚本来运行该git config命令:

git config --local include.path ../.gitconfig

注意我还将日志级别更改为 INFO,因为我确实想查看清理正在运行的确认信息。

仓库/.gitconfig

[filter "strip-notebook-output"]
    clean = "jupyter nbconvert --ClearOutputPreprocessor.enabled=True --to=notebook --stdin --stdout --log-level=INFO"
Run Code Online (Sandbox Code Playgroud)

仓库/.gitattributes

*.ipynb filter=strip-notebook-output
Run Code Online (Sandbox Code Playgroud)

仓库/git_configure.sh

git config --local include.path ../.gitconfig
Run Code Online (Sandbox Code Playgroud)

然后用户只需运行:

$ chmod u+x git_configure.sh
$ ./git_configure.sh
Run Code Online (Sandbox Code Playgroud)