当笔记本的输出非常长并且保存到笔记本中时,我遇到了一个问题,每当我想再次打开这个特定的笔记本时,浏览器就会崩溃并且无法正确显示。
要解决此问题,我必须使用文本编辑器打开它并删除该单元格中导致问题的所有输出。
我想知道是否有一种方法可以清除笔记本的所有输出,以便可以毫无问题地再次打开它。我想删除所有输出,因为删除特定的输出似乎更麻烦。
当笔记本的输出非常长并且保存到笔记本中时,我遇到了一个问题,每当我想再次打开这个特定的笔记本时,浏览器就会崩溃并且无法正确显示。
要解决此问题,我必须使用文本编辑器打开它并删除该单元格中导致问题的所有输出。
我想知道是否有一种方法可以清除笔记本的所有输出,以便可以毫无问题地再次打开它。我想删除所有输出,因为删除特定的输出似乎更麻烦。
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
Harold 在评论中引起了我的注意。
在 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。
如果您创建一个.gitattributes
文件,您可以在将某些文件添加到 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
这是如何工作的:
clean
每个笔记本文件添加到索引(暂存)之前对每个笔记本文件运行过滤器的操作。nbconvert
,设置为从标准输入读取,写入标准输出,剥离输出,并且仅在有重要内容时才说话。smudge
运行过滤器的操作,但这是一个无操作,因为我们没有指定它。您可以在此处运行您的笔记本以重新创建输出 ( nbconvert --execute
)。我对这个过程唯一的不满是我可以提交.gitattributes
,但我必须告诉我的同事更新他们的.git/config
.
如果您想要一个更hacker但更快的版本,请尝试JQ:
clean = "jq '.cells[].outputs = [] | .cells[].execution_count = null | .'"
使用--ClearOutputPreprocessor.enabled=True和--clear-output
遵循此命令:
jupyter nbconvert --ClearOutputPreprocessor.enabled=True --clear-output *.ipynb
要扩展@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"
回购/.gitattributes
*.ipynb filter=strip-notebook-output
回购/git_configure.sh
git config --local include.path ../.gitconfig
然后用户只需要运行:
$ chmod u+x git_configure.sh
$ ./git_configure.sh
我必须说我发现jupyer nbconvert
清除一些子数组和重置一些执行数的简单工作非常缓慢。这是可维护性方面的卓越解决方案,因为如果笔记本源代码格式发生变化,该工具预计会更新。但是,下面的替代解决方案更快,如果您没有 nbconvert 6.0 也可能有用(我目前有一个运行 5.6.1 的环境......)
一个非常简单jq
的(一种 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
非常简单,它识别代码单元,并分别用和替换它们的outputs
和execution_count
属性。[]
null
或者,如果您只想删除输出并保留执行次数,您可以做的更简单:
jq 'del(.cells[]|select(.cell_type == "code").outputs[])' notebook.ipynb > out-notebook.ipynb
nbstripout
对我来说效果很好。
打开 Jupyter 终端,导航到包含笔记本的文件夹,然后运行以下行:
nbstripout my_notebook.ipynb