繁体   English   中英

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

[英]How can I follow the cell currently being run in a Jupyter notebook?

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

~/.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个快捷方式都只能在命令模式下使用(有关更多信息,请参见上文)。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM