【问题标题】:How to clear Jupyter Notebook's output in all cells from the Linux terminal?如何从 Linux 终端清除所有单元格中的 Jupyter Notebook 输出?
【发布时间】:2015-05-08 14:53:55
【问题描述】:

当笔记本的输出非常长并且保存到笔记本中时,我遇到了问题,每当我想再次打开这个特定的笔记本时,浏览器就会崩溃并且无法正确显示。

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

我想知道是否有一种方法可以清除笔记本中的所有输出,以便可以再次打开它而不会出现问题。我想删除所有输出,因为删除一个特定的似乎更麻烦。

【问题讨论】:

标签: jupyter-notebook


【解决方案1】:

nbconvert 6.0 应该修复 --clear-output

该选项之前已被破坏了很长时间,合并补丁的错误报告:https://github.com/jupyter/nbconvert/issues/822

应用于就地操作:

jupyter nbconvert --clear-output --inplace my_notebook.ipynb

或者保存到另一个名为my_notebook_no_out.ipynb的文件:

jupyter nbconvert --clear-output \
  --to notebook --output=my_notebook_no_out my_notebook.ipynb

这引起了我的注意by Harold in the comments

在 nbconvert 6.0 之前:--ClearOutputPreprocessor.enabled=True

--clear-output的用法相同:

jupyter nbconvert --ClearOutputPreprocessor.enabled=True --inplace my_notebook.ipynb
jupyter nbconvert --ClearOutputPreprocessor.enabled=True \
  --to notebook --output=my_notebook_no_out my_notebook.ipynb

在 Jupyter 4.4.0 中测试,笔记本==5.7.6。

【讨论】:

  • 这会将笔记本转换为 html,这似乎不是操作想要的..
  • @Jacquot 你在哪个版本的 Jupyter 中?我刚刚重新测试,它修改了 .ipynb 就地而不创建 HTML。
  • 我读得太快了你的评论,不知道--inplace选项;我学到了一些东西。但它出现在我的版本 5.3.1 中,选项--clear-output 可用,总结了--ClearOutputPreprocessor.enabled=True --inplace
  • 我必须添加一个--to notebook 才能使第二个版本(非就地)工作
  • 选项--clear-output 已损坏,请参阅问题#822。此问题已在上个月(2020 年 7 月)得到修复,因此它应该会在下一个版本中再次起作用。
【解决方案2】:

如果你创建了.gitattributesfile,你可以在某些文件被添加到 git 之前运行过滤器。这将使磁盘上的原始文件保持原样,但提交“清理”版本。

为此,请将其添加到您的本地 .git/config 或全局 ~/.gitconfig

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

然后在你的笔记本目录中创建一个.gitattributes 文件,用这个 内容:

*.ipynb filter=strip-notebook-output

这是如何工作的:

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

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

如果您想要更简洁但速度更快的版本,请尝试JQ

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

【讨论】:

  • 这是两全其美的。感谢分享这个
  • 不知道这个。这非常有用。
  • 一个稍微改进的替代方案如下。它会清理元数据,并且不会像提议的 JQ 解决方案那样将输出和 execution_count 添加到非代码单元(这会导致警告):clean = "jq '.cells |= map(if .\"cell_type\" == \"code\" then .outputs = [] | .execution_count = null else . end | .metadata = {}) | .metadata = {}'"
【解决方案3】:

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

按照这个命令:

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

【讨论】:

    【解决方案4】:

    使用clean_ipynb,不仅可以清除notebook输出,还可以清理代码。

    pip install clean_ipynb安装

    clean_ipynb hello.ipynb运行

    【讨论】:

    • nbclean 是一个工具,它可以通过一些方便的附加功能来做到这一点,例如仅删除某些代码/文本块,使其便于用于教学。
    【解决方案5】:

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

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

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

    请注意,我也将日志级别更改为 INFO,因为我确实希望看到清理正在运行的确认。

    repo/.gitconfig

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

    repo/.gitattributes

    *.ipynb filter=strip-notebook-output
    

    repo/git_configure.sh

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

    然后用户只需要运行:

    $ chmod u+x git_configure.sh
    $ ./git_configure.sh
    

    【讨论】:

      【解决方案6】:

      我必须说我发现 jupyer nbconvert 对于清除一些子数组和重置一些执行数的简单工作来说非常慢。这是可维护性方面的卓越解决方案,因为如果笔记本源代码格式发生变化,该工具预计会更新。但是,下面的替代解决方案更快,如果您没有 nbconvert 6.0 也可能有用(我目前有一个运行 5.6.1 的环境......)

      一个非常简单的jq (a sort of sed for json) 脚本可以非常快地完成任务:

      jq 'reduce path(.cells[]|select(.cell_type == "code")) as $cell (.; setpath($cell + ["outputs"]; []) | setpath($cell + ["execution_count"]; null))' notebook.ipynb > out-notebook.ipynb
      

      非常简单,它识别代码单元,并将它们的outputsexecution_count 属性分别替换为[]null


      或者,如果您只想删除输出并保留执行次数,您可以做得更简单:

      jq 'del(.cells[]|select(.cell_type == "code").outputs[])' notebook.ipynb > out-notebook.ipynb
      

      【讨论】:

        【解决方案7】:

        nbstripout 对我来说效果很好。

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

        nbstripout my_notebook.ipynb

        【讨论】:

        • 优秀——甚至nbstripout *.ipynb :)
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-09-08
        • 1970-01-01
        • 2016-09-06
        相关资源
        最近更新 更多