【问题标题】:how to bind a command to 'Restart and Run All'?如何将命令绑定到“重新启动并全部运行”?
【发布时间】:2015-09-14 21:06:32
【问题描述】:

我最近构建的 Jupyter 有一个菜单操作允许您重新启动并运行所有

我想添加一个绑定到此操作的键盘快捷键。我已经看到documentation 用于键盘自定义,但我仍然不确定如何添加键盘快捷键。

我已经从源代码构建了 Juypter,因此根据帮助,看来我需要向 notebook/static/custom/custom.js 添加一些代码。

我尝试添加以下内容:

IPython.keyboard_manager.command_shortcuts.add_shortcut('meta-r', function (event) {
        IPython.notebook.restart_kernel();
        IPython.notebook.execute_run_all();
        return false;
});

但是,当我按下 [Meta-r] 时,内核似乎重新启动,但 execute_run_all() 没有被执行。

【问题讨论】:

  • 注意要使其自动加载,您需要将其包装为 show here

标签: ipython-notebook jupyter


【解决方案1】:

从 Jupyter Notebook 5.0 开始,您现在可以直接在菜单选项中创建和编辑键盘快捷键。截至目前,它是帮助 -> 编辑键盘快捷键。该对话框底部有一个指南。

上面的文档在这里: http://jupyter-notebook.readthedocs.io/en/latest/examples/Notebook/Custom%20Keyboard%20Shortcuts.html

【讨论】:

    【解决方案2】:

    这是我的custom.js 中的内容。要使其正常工作,必须在应用初始化后添加快捷方式:

    $([Jupyter.events]).on("app_initialized.NotebookApp", function () {
        Jupyter.keyboard_manager.command_shortcuts.add_shortcut('0,1', {
        help: 'restart and run all',
        help_index: '0,1',
        handler: function (event) {
          Jupyter.notebook.kernel.restart();
          restartTime = 2000 // decrease this if you have a fast computer
          setTimeout(function(){ Jupyter.notebook.execute_all_cells(); }, restartTime);
          return false;
        }}
      );
    });
    

    【讨论】:

    • 这对我不起作用,它会重新启动但不会运行单元格。选择菜单项“重新启动并运行所有”确实有效但是......菜单项是否使用不同的机制?
    • 啊修好了...不得不将超时时间增加到 2000 毫秒,我要编辑答案,以使计算机速度慢的人受益!
    • 谢谢!这些天我没有使用它,所以我没有任何有用的建议。 :)
    • 我经历了做 EE 系统工作和编写软件的阶段。暂时是软件!
    【解决方案3】:

    以防万一有人偶然发现这篇文章正在寻找相同的答案:您需要等待内核在超时后重新启动,然后再执行。 看到这个discussion on GitHub

    在你的情况下,它会给出:

    IPython.keyboard_manager.command_shortcuts.add_shortcut('meta-r',
      function (event) {
          IPython.notebook.kernel.restart();
          setTimeout(function(){ IPython.notebook.execute_all_cells(); }, 1000);
          return false;
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-12
      • 2018-04-26
      • 1970-01-01
      • 1970-01-01
      • 2018-06-11
      • 2019-11-11
      • 1970-01-01
      • 2016-08-26
      相关资源
      最近更新 更多