Jupyter:我该如何回忆和编辑以前的"In:"文本? [英] Jupyter: How do I recall and edit the previous "In:" text?
问题描述
在命令行Python会话中,我可以按Control-P来检索先前输入的行,对其进行编辑.
In a command line Python session I can press Control-P to retrieve the previously entered line edit it.
如何在Jupyter中执行类似的操作,即结转前一个"In:"块中的内容?
How can I perform a similar operation in Jupyter, i.e. carry forward the contents of the previous "In:" block?
推荐答案
看起来Jupyter没有开箱即用的功能,尽管您可以使用CodeMirror API编写自己的自定义键盘快捷键: https://codemirror.net/doc/manual.html
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
首先,您需要创建自己的custom.js
文件:
http://jupyter-notebook .readthedocs.io/en/stable/examples/Notebook/JavaScript%20Notebook%20Extensions.html#custom.js
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);
}
这将复制上面单元格的内容,并将其插入到当前选定的单元格中.您可以将paste_cell_replace()
方法替换为paste_cell_above()
来创建一个新的单元格,而不是替换当前所选单元格的内容.
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.
这篇关于Jupyter:我该如何回忆和编辑以前的"In:"文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!