使用来自单元格的命令重新启动 ipython 内核

Restart ipython Kernel with a command from a cell

是否可以通过从笔记本 GUI 中选择 Kernel > Restart,而是通过在笔记本单元中执行命令来重新启动 ipython 内核?

正如 Thomas K. 所建议的,这是从键盘重新启动 ipython 内核的方法:

import os
os._exit(00)

定义一个重启Jupyter内核的函数,我已经成功使用:

from IPython.display import display_html
def restartkernel() :
    display_html("<script>Jupyter.notebook.kernel.restart()</script>",raw=True)

然后调用

restartkernel()

什么时候重启。

import IPython

IPython.Application.instance().kernel.do_shutdown(True) #automatically restarts kernel

尝试print(chr(12))

我不确定这个函数在幕后做了什么,但如果你正在寻找一种方法来隐藏所有以前的输出(例如内存中的 'card' 游戏),它是有效的。