Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jupyter: How do I recall and edit the previous "In:" text?

In a command line Python session I can press Control-P to retrieve the previously entered line edit it.

How can I perform a similar operation in Jupyter, i.e. carry forward the contents of the previous "In:" block?

like image 931
Mark Harrison Avatar asked Mar 07 '23 05:03

Mark Harrison


1 Answers

Looks like Jupyter doesn't have such a feature out of the box, although, you could write your own custom keyboard shortcut using CodeMirror API: https://codemirror.net/doc/manual.html

First you need to create your own custom.js file: http://jupyter-notebook.readthedocs.io/en/stable/examples/Notebook/JavaScript%20Notebook%20Extensions.html#custom.js

You could try something like this (depending on what you're expecting to get):

CodeMirror.keyMap.pcDefault["Ctrl-P"] = function(cm) {

    var selected = Jupyter.notebook.get_selected_cell();
    if (!Jupyter.notebook.get_prev_cell(selected)) {
        // This is the first cell
        return;
    }

    Jupyter.notebook.select_prev();
    Jupyter.notebook.copy_cell();
    Jupyter.notebook.select_next();
    Jupyter.notebook.paste_cell_replace();
    Jupyter.notebook.handle_edit_mode(selected);

}

This will copy the contents of the cell above and insert it into the currently selected cell. You could replace paste_cell_replace() method with paste_cell_above() to create a new cell instead of replacing the contents of the currently selected one.

like image 138
d2718nis Avatar answered Mar 20 '23 11:03

d2718nis