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

Deadly 提交于 2019-12-13 16:17:41

问题


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?


回答1:


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.




回答2:


Maybe a lame solution, but you can just copy the cell, right? Press Esc, select the cell (mostly likely the previous one, thus press Up) and press c and v.



来源:https://stackoverflow.com/questions/50311218/jupyter-how-do-i-recall-and-edit-the-previous-in-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!