使用“重新启动&全部运行" [英] Jupyter notebook custom.js not applied when using "Restarting & Run All"
问题描述
为了获得正在运行的Jupyter笔记本的名称,我首先在〜/.jupyter/custom/custom.js中添加了以下行
In order to get the name of a running Jupyter notebook, I first added the following line in ~/.jupyter/custom/custom.js
// Create a nb_name variable with the name of the notebook
IPython.notebook.kernel.execute('nb_name = "' + IPython.notebook.notebook_name + '"');
然后,当我使用以下命令运行单元格时,在笔记本上
Then on my notebook when I run a cell with:
print(nb_name)
我得到:
NameError Traceback (most recent call last)
<ipython-input-1-7e37f787d8df> in <module>()
----> 1 print(nb_name)
NameError: name 'nb_name' is not defined
要解决此问题,我需要在第一行添加一个警报命令:
To solve this issue I need to add a first line with an alert command:
alert("hello world from custom.js")
// Create a nb_name variable with the name of the notebook
IPython.notebook.kernel.execute('nb_name = "' + IPython.notebook.notebook_name + '"');
然后,当我加载笔记本时,会显示一个警报窗口,一旦关闭它,我将按预期获得笔记本的名称.
Then an alert window shows up when I load my notebook, and once I closed it, I get the notebook name as expected.
我如何在没有用户任何操作的情况下使它工作(我正在使用笔记本电脑版本5.0.0,并且由于我不是服务器的管理员,所以无法更新它)?
How could I make it work without any action from the user (I am using notebook version 5.0.0 and as I am not the admin of the server cannot update it)?
通过Jupyter内核(Jupyter Notebook扩展)执行代码时,等待内核准备就绪部分解决了该问题. Custom.js文件,其中包含:
Question in Waiting for kernel to be ready when executing code via Jupyter kernel (Jupyter Notebook extension) solved partially the problem. Custom.js file containing:
Jupyter.notebook.events.one('kernel_ready.Kernel', () => {
// Create a nb_name variable with the name of the notebook
IPython.notebook.kernel.execute('nb_name = "' + IPython.notebook.notebook_name + '"');
});
按预期方式返回笔记本名称.现在的问题是,当我重新启动并运行所有程序"时,仍然收到上面提到的错误消息.任何意见或想法都将受到欢迎.
Return the notebook name as expected. Now the problem is I still get the error message mentioned above when I "Restart & Run All". Any comment or idea would be welcome.
推荐答案
custom.js kernel_ready.Kernel
事件仅在页面加载时触发一次,但在Restart & Run all
之后不会触发(或其任何变体).对于这个问题,我的解决方案有点骇客":
custom.js kernel_ready.Kernel
event is triggered only once when the page is loaded, but it is not triggered after Restart & Run all
(or any variants of it). My solution to this problem is a bit hackie:
/**
* `kernel_ready` logic
*/
function custom_kernel_ready_handler() {
IPython.notebook.kernel.execute('nb_name = "' + IPython.notebook.notebook_name + '"');
}
function handle_kernel_ready() {
// Create a nb_name variable with the name of the notebook
console.log('kernel_ready.Kernel: handle_kernel_ready() was triggered!');
custom_kernel_ready_handler();
Jupyter.notebook.events.one('kernel_ready.Kernel', () => {
//this recursive behavior is esential for `restart` kernel
handle_kernel_ready();
});
}
Jupyter.notebook.events.one('kernel_ready.Kernel', () => {
handle_kernel_ready();
});
希望有更好的解决方案...
Hoping for better solutions ...
这篇关于使用“重新启动&全部运行"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!