Add permanent key bindings for Jupyter Notebook

This post shows how to add customized permanent key bindings for jupyter notebook.

1. check the location of your jupyter config folder using the command:

sudo ~/.local/bin/jupyter --config-dir

I am running Ubuntu. The config folder, by default is, `/home/your_user_name/.jupyter`

2. Create a folder `custom` in the config folder. 

3. Create a file `custom.js` in the `custom` folder. (So the path for `custom.js` is `/home/your_user_name/.jupyter/custom/custom.js`) Add your key binding function in `custom.js`. Here is an example adding a key bind combo for restarting and re-running all cells.

$([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();
      setTimeout(function(){ Jupyter.notebook.execute_all_cells(); }, 1000);
      return false;
    }}
  );
});

4. Now restart the Jupyter and try press 0 followed by 1 in the command mode. All cells should be re-run after an one second delay.

 

Reference:

https://www.webucator.com/blog/2015/11/show-line-numbers-by-default-in-ipython-notebook/

http://stackoverflow.com/questions/32573958/how-to-bind-a-command-to-restart-and-run-all

Leave a comment

Your email address will not be published. Required fields are marked *