【问题标题】:How can I follow the cell currently being run in a Jupyter notebook?如何跟踪当前在 Jupyter 笔记本中运行的单元?
【发布时间】:2017-05-31 23:38:04
【问题描述】:

在 Jupyter 笔记本中选择 run allrun all aboverun all below 后,如何跟踪当前在 Jupyter 笔记本中运行的单元格?即,我希望显示给我的单元格是在笔记本执行期间正在运行的单元格。

【问题讨论】:

    标签: python jupyter-notebook


    【解决方案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-[ 使当前运行的单元格停留在屏幕中间,按 @ 987654324@ 恢复正常行为。

    如果这不起作用,请通过取消注释 console.log() 调用来调试此设置,并观察您的浏览器开发人员工具的控制台以检查 custom.js 是否已正确加载,并且快捷方式已注册并且处理程序已激活。有时您需要重新启动jupyter notebook,但大多数时候tab-reload 工作正常。

    如果您只想跳转到当前正在执行的单元格,请在将以下内容添加到 ~/.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 一起使用。

    【讨论】:

      猜你喜欢
      • 2020-05-17
      • 2015-11-11
      • 2017-11-04
      • 2020-04-05
      • 1970-01-01
      • 1970-01-01
      • 2018-04-13
      • 2018-09-12
      • 1970-01-01
      相关资源
      最近更新 更多