首页 > 解决方案 > 通过 Jupyter 内核(Jupyter Notebook 扩展)执行代码时等待内核准备就绪

问题描述

我正在构建一个 Jupyter Notebook 扩展,一旦内核在初始化期间准备就绪,我就需要在其中执行一些代码。我正在执行我的代码如下,但我注意到有时内核在我加载我的扩展之前就准备好了,所以我的自定义事件中的代码永远不会被执行。

Jupyter.notebook.events.one('kernel_ready.Kernel', () => {
     // Couldn't execute this line, because kernel was already
     // ready when this event handler was attached
     Jupyter.notebook.kernel.execute(someCode)
});

我可以只信任笔记本执行代码而不必担心内核准备就绪吗?我猜有某种类型的队列,所以代码最终会被执行。

Jupyter.notebook.kernel.execute(someCode)

标签: jupyter-notebook

解决方案


我是 Javascript 的初学者,但也许我可以提供一些信息,因为您的问题已部分解决并且我几天前发布的问题(Jupyter notebook custom.js not applicable when using "Restarting & Run All")。

将我的代码放入

Jupyter.notebook.events.one('kernel_ready.Kernel', () => {

在 custom.js 文件上允许我保证它在我加载脚本时被执行。


推荐阅读