Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to bind a command to 'Restart and Run All'?

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.

like image 453
Chris Snow Avatar asked Sep 14 '15 21:09

Chris Snow


People also ask

How do I run an entire notebook?

You can run the notebook document step-by-step (one cell a time) by pressing shift + enter. You can run the whole notebook in a single step by clicking on the menu Cell -> Run All.

Which menu tab in the jupyter notebook contains the run all command?

For the latest jupyter notebook, (version 5) you can go to the 'help' tab in the top of the notebook and then select the option 'edit keyboard shortcuts' and add in your own customized shortcut for the 'run all' function.

What is the shortcut key for run in jupyter notebook?

Shift + Enter run the current cell, select below. Ctrl + Enter run selected cells. Alt + Enter run the current cell, insert below. Ctrl + S save and checkpoint.


1 Answers

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;
});
like image 117
Silmathoron Avatar answered Oct 03 '22 19:10

Silmathoron