如何在没有输出的情况下打开IPython笔记本? [英] How can I open an IPython notebook without the output?
问题描述
我有一台IPython笔记本,我不小心丢弃了一个巨大的输出(15英寸MB),导致笔记本电脑崩溃。现在,当我打开笔记本电脑并试图删除麻烦的电池时,笔记本电脑再次崩溃 - 从而阻止我解决问题并恢复笔记本电脑的稳定性。
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 troublesome cell, the notebook crashes again—thus preventing me from fixing the problem and restoring the notebook to stability.
最好修复我能想到的是手动将输入单元格粘贴到新笔记本上,但有没有办法只打开笔记本没有任何输出?
The best fix I can think of is manually pasting the input cells to a new notebook, but is there a way to just open the notebook without any outputs?
推荐答案
有一个很好的片段(我用作git提交钩子)去除ipython笔记本的输出:
There is this nice snippet (that I use as a git commit hook) to strip the output of an ipython notebook:
#!/usr/bin/env python
def strip_output(nb):
for ws in nb.worksheets:
for cell in ws.cells:
if hasattr(cell, "outputs"):
cell.outputs = []
if hasattr(cell, "prompt_number"):
del cell["prompt_number"]
if __name__ == "__main__":
from sys import stdin, stdout
from IPython.nbformat.current import read, write
nb = read(stdin, "ipynb")
strip_output(nb)
write(nb, stdout, "ipynb")
stdout.write("\n")
你可以轻松地使它变得更好用,目前您必须将其称为
You can easily make it a bit nicer to use, currently you'd have to call it as
strip_output.py < my_notebook.ipynb > my_notebook_stripped.ipynb
这篇关于如何在没有输出的情况下打开IPython笔记本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!