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

时间:2017-05-31 23:38:05

标签: python jupyter-notebook

在Jupyter笔记本中选择run allrun all aboverun all below之后,如何关注当前在Jupyter笔记本中运行的单元格?即,在整个笔记本的执行过程中,我希望显示给我的单元格是正在运行的单元格。

1 个答案:

答案 0 :(得分: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,但是大多数时候Tab重新加载都有效。

如果您只想跳到当前执行单元一次,请在将以下内容添加到Alt-I并重新加载正在运行的笔记本后,使用~/.jupyter/custom/custom.js

// 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个快捷方式都只能在命令模式下使用(请参阅上文以了解具体情况)。

经过测试,可与带有python 3.6.6的jupyter notebook 5.6.0一起使用。

相关问题