13

I have recent build of Jupyter than has a menu action allowing you to Restart & Run All:

enter image description here

I would like to add a keyboard shortcut that binds to this action. I have seen the documentation for keyboard customization, but I'm still unsure how to add a keyboard shortcut.

I've built Juypter from source, so based on the help, it would appear that I need to add some code to notebook/static/custom/custom.js.

I've tried adding the following:

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

However, when I press [Meta-r], the kernel seems to restart but execute_run_all() does not get executed.

Chris Snow
  • 23,813
  • 35
  • 144
  • 309
  • **Note** To make it load automatically, you need to wrap it like show [here](http://stackoverflow.com/a/32159304/776515) – Luis Jun 30 '16 at 13:57

3 Answers3

10

As of Jupyter Notebook 5.0, you can now create and edit keyboard shortcuts directly in menu options. As of now, it's Help -> Edit Keyboard Shortcuts. There's a guide at the bottom of that dialogue box.

The docs on it are here: http://jupyter-notebook.readthedocs.io/en/latest/examples/Notebook/Custom%20Keyboard%20Shortcuts.html

ClimbsRocks
  • 994
  • 13
  • 15
6

Here's what I have in my custom.js. For it to work, the shortcut addition has to happen after the app is initialized:

$([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;
    }}
  );
});
maxymoo
  • 35,286
  • 11
  • 92
  • 119
Jim Hunziker
  • 14,111
  • 8
  • 58
  • 64
  • this doesn't work for me, it restarts but doesn't run the cells. selecting the menu item "restart and run all" does work however ... does the menu item use a different mechanism? – maxymoo Nov 03 '16 at 21:30
  • 2
    ah fixed it ... had to increase the timeout to 2000ms, i'm gonna edit the answer for the benifit of people with slow computers! – maxymoo Nov 03 '16 at 21:45
  • Thanks! I'm not using it these days, so I didn't have anything helpful to suggest. :) – Jim Hunziker Nov 03 '16 at 21:46
  • 1
    I go through phases of doing EE systems work and writing software. It's software for the time being! – Jim Hunziker Nov 03 '16 at 21:49
4

Just in case someone stumbles upon this post looking for the same answer: you need to wait for the kernel to restart with a timeout before executing. See this discussion on GitHub.

In your case, it would give:

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;
});
Silmathoron
  • 1,781
  • 1
  • 15
  • 32