且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在没有输出的情况下打开IPython笔记本?

更新时间:2022-11-03 13:46:17

有一个很好的片段(我用作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