Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add keyboard shortcuts permanently to Jupyter (ipython) notebook?

I have the following configuration for shortcuts, that works after running it in the cell of Jupiter notebook:

%%javascript


IPython.keyboard_manager.command_shortcuts.add_shortcut('ctrl-q', {
    help: 'Clear all output',               // This text will show up on the help page (CTRL-M h or ESC h)
    handler: function (event) {             // Function that gets invoked
        if (IPython.notebook.mode == 'command') {
            IPython.notebook.clear_all_output();
            return false;
        }
        return true;                   
    }
  });

How can I setup Jupiter notebook to make this initialization automatically on startup?

I tried adding the same code (without %%javascript) to C:\Users\<username>\.ipython\profile_default\static\custom\custom.js but it didn't work.

I have only one profile, created with ipython profile create, Python 3.3, Windows 7.

Thanks in advance.

like image 668
Apogentus Avatar asked Aug 17 '15 09:08

Apogentus


People also ask

Which keyboard shortcut in Jupyter can you use to run the current cell and then select the cell below?

Run code cells Use the following smart shortcuts to quickly run the code cells: Ctrl+Enter : Runs the current cell. Shift+Enter : Runs the current cell and select the cell below it.

What can we do via using shortcut keys in the command mode in Jupyter Notebook?

While in command mode: A to insert a new cell above the current cell, B to insert a new cell below. M to change the current cell to Markdown, Y to change it back to code. D + D (press the key twice) to delete the current cell.

Which shortcut key can be used to see the description of a function in Jupyter Notebook?

shift + tab + tab + tab + tab @Shakeel This does work for many functions but it doesn't for others. For example, using only pandas, in oo.


4 Answers

In the new version of Jupyter notebook (update it either with pip install --upgrade notebook or if you use conda conda upgrade notebook), you can customize them from the notebook itself.

To do this Help -> Edit keyboard shortcuts

enter image description here

like image 140
Salvador Dali Avatar answered Oct 21 '22 22:10

Salvador Dali


custom.js is the correct place for this code. Try wrapping it as follows (don't forget the return true before the end of the block):

$([IPython.events]).on("app_initialized.NotebookApp", function () {
    <your code>

    return true;
});
like image 22
Dmitri Avatar answered Oct 21 '22 22:10

Dmitri


1. For changing command mode shortcuts: refer Salvador's answer

2. For changing edit mode shortcuts:

Edit the file, ~/.jupyter/nbconfig/notebook.json as explained on https://jupyter-notebook.readthedocs.io/en/stable/extending/keymaps.html

For example, after replacing the control-enter shortcut to execute code, with command-enter on macOS, the file looks like this:

{
  "Notebook": {
    "Toolbar": true,
    "Header": true
  },
  "Cell": {
    "cm_config": {
      "lineNumbers": true
    }
  },
  "keys": {
    "command": {
      "unbind": [
        "ctrl-enter"
      ],
      "bind": {
        "cmdtrl-enter": "jupyter-notebook:run-cell"   
      }
    }, 
    "edit": {
      "unbind": [
        "ctrl-enter"
      ],
      "bind": {
        "cmdtrl-enter": "jupyter-notebook:run-cell"
      }  
    } 
  }   
} 
like image 6
Sachit Nagpal Avatar answered Oct 21 '22 21:10

Sachit Nagpal


Adding hotkeys the easy way with nbextensions

  1. Install nbextensions.
    pip install jupyter_contrib_nbextensions
  2. Then launch jupyter notebook.
  3. The the intro page will have a new tab called nbextensions click it and enable Keyboard Shortcut Editor.
  4. Now open any notebook click help>keyboard shortcuts
  5. Each shortcut will have a pencil icon next to it if you click on it then you can set the shortcut to whatever you want.
like image 4
James Draper Avatar answered Oct 21 '22 20:10

James Draper