我该如何在Jupyter笔记本中跟踪当前正在运行的单元格?

5
当我在Jupyter笔记本中选择运行全部运行以上全部运行以下全部后,如何在Jupyter笔记本中跟随当前正在运行的单元格?也就是说,在整个笔记本的执行过程中,我希望显示给我的单元格是正在运行的单元格。
1个回答

1

将以下内容添加到~/.jupyter/custom/custom.js中,并重新加载正在运行的笔记本:

/*
 In Command mode Meta-[ toggles Follow Exec Cell mode, Meta-] turns it off.

 To adjust the behavior you can adjust the arguments:
 * behavior: One of "auto", "instant", or "smooth". Defaults to "auto". Defines the transition animation.
 * block:    One of "start", "center", "end", or "nearest". Defaults to "center".
 * inline:   One of "start", "center", "end", or "nearest". Defaults to "nearest".
 https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView
*/
function scrollIntoRunningCell(evt, data) {
    $('.running')[0].scrollIntoView({behavior: 'smooth', inline: 'center'});
}

Jupyter.keyboard_manager.command_shortcuts.add_shortcut('Meta-[', {
    help: 'Follow Executing Cell On',
    help_index: 'zz',
    handler: function (event) {
        Jupyter.notebook.events.on('finished_execute.CodeCell', scrollIntoRunningCell);
        //console.log("Follow Executing Cell On")
        return false;
    }
});

Jupyter.keyboard_manager.command_shortcuts.add_shortcut('Meta-]', {
    help: 'Follow Executing Cell Off',
    help_index: 'zz',
    handler: function (event) {
        Jupyter.notebook.events.off('finished_execute.CodeCell', scrollIntoRunningCell);
        //console.log("Follow Executing Cell Off")
        return false;
    }
});

现在处于命令模式(当焦点单元格周围有蓝色框而不是绿色,或按Esc切换模式),按下Meta-[将当前运行的单元格保持在屏幕中央,按下Meta-]返回正常行为。
如果这无法正常工作,请通过取消注释console.log()调用并观察浏览器开发人员工具的控制台来调试此设置,以检查custom.js是否已加载且未出现错误,并且快捷键已注册并激活处理程序。有时需要重新启动jupyter notebook,但大多数情况下,选项卡重新加载即可。
如果您只想跳转到当前正在执行的单元格,请在将以下内容添加到~/.jupyter/custom/custom.js并重新加载运行的笔记本后,使用Alt-I跳转一次:
// Alt-I: Go to Running cell shortcut [Command mode]
Jupyter.keyboard_manager.command_shortcuts.add_shortcut('Alt-I', {
    help : 'Go to Running cell',
    help_index : 'zz',
    handler : function (event) {
        setTimeout(function() {
            // Find running cell and click the first one
            if ($('.running').length > 0) {
                //alert("found running cell");
                $('.running')[0].scrollIntoView();
            }}, 250);
        return false;
    }
});

注意:为了使其正常工作,所有部分都应该未折叠-否则它将不知道进入折叠的部分。

您可以根据自己的喜好调整激活快捷键。

请记住,这3个快捷方式只在命令模式下起作用(有关如何确定,请参见上文)。

已经测试过,在jupyter notebook 5.6.0和python 3.6.6中可以使用。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接