如何将命令绑定到"重新启动并运行全部"?

Chr*_*now 13 ipython-notebook jupyter

我最近构建了Jupyter而不是菜单操作,允许您重新启动并运行所有:

在此输入图像描述

我想添加一个绑定到此操作的键盘快捷键.我已经看过键盘自定义的文档,但我仍然不确定如何添加键盘快捷键.

我已经从源代码构建了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;
});
Run Code Online (Sandbox Code Playgroud)

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

Cli*_*cks 9

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

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


Jim*_*ker 6

这就是我的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;
    }}
  );
});
Run Code Online (Sandbox Code Playgroud)

  • 啊修复它...不得不将超时时间增加到2000ms,我要编辑慢速计算机用户的好处的答案! (2认同)

Sil*_*ron 4

以防万一有人偶然发现这篇文章寻找相同的答案:您需要等待内核在超时后重新启动,然后再执行。请参阅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;
});
Run Code Online (Sandbox Code Playgroud)