Python 在 Jupyter 笔记本中清除单元格输出的键盘快捷键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39924826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 22:55:22  来源:igfitidea点击:

Keyboard shortcut to clear cell output in Jupyter notebook

pythonjupyter-notebook

提问by zesla

Does anyone know what is the keyboard shortcut to clear (not toggle) the cell output in Jupyter Notebook?

有谁知道在 Jupyter Notebook 中清除(而不是切换)单元格输出的键盘快捷键是什么?

采纳答案by Dmitrii Magas

You can setup your own shortcut in the UI (for the latest master version):

您可以在 UI 中设置自己的快捷方式(对于最新的主版本):

enter image description here

在此处输入图片说明

This menu can be found in Help > Keyboard Shortcutsin any open notebook.

这个菜单可以Help > Keyboard Shortcuts在任何打开的笔记本中找到。

回答by Ciprian Tomoiag?

For versions less than 5:

对于小于 5 的版本:

Option 1 -- quick hack:

选项 1 - 快速破解:

Change the cell type to raw then back to code: EscRYwill discard the output.

将单元格类型更改为原始然后返回代码:EscRY将丢弃输出。

Option 2 -- custom shortcut (without GUI):

选项 2——自定义快捷方式(无 GUI):

For this, you need to edit the custom.jsfile which is typically located at ~/.jupyter/custom/custom.js(if it doesn't exist, create it).

为此,您需要编辑custom.js通常位于的文件~/.jupyter/custom/custom.js(如果它不存在,请创建它)。

In there, you have to add

在那里,你必须添加

require(['base/js/namespace']) {
    // setup 'ctrl-l' as shortcut for clearing current output
    Jupyter.keyboard_manager.command_shortcuts
           .add_shortcut('ctrl-l', 'jupyter-notebook:clear-cell-output');
}

You can add shortcut there for all the fancy things you like, since the 2nd argument can be a function (docs)

你可以在那里为你喜欢的所有花哨的东西添加快捷方式,因为第二个参数可以是一个函数(文档

If you want mappings for other standard commands, you can dump a list of all available commands by running the following in your notebook:

如果您想要其他标准命令的映射,您可以通过在笔记本中运行以下命令来转储所有可用命令的列表:

from IPython.core.display import Javascript

js = """
  var jc_html = "";
  var jc_array = Object.keys(IPython.notebook.keyboard_manager.command_shortcuts.actions._actions);
  for (var i=0;i<jc_array.length;i++) {
    jc_html = jc_html + jc_array[i] + "<br >";
  }
  element.html(jc_html);
  """

Javascript(data=js, lib=None, css=None)

回答by devil in the detail

Add following at start of cell and run it:

在单元格的开头添加以下内容并运行它:

from IPython.display import clear_output
clear_output(wait=True)

回答by M.GEM

Just adding in for JupyterLabusers. Ctrl,(advanced settings) and pasting the below in User References under keyboard shortcuts does the trick for me.

只需为JupyterLab用户添加。Ctrl,(高级设置)并在键盘快捷键下的用户参考中粘贴以下内容对我来说很有效。

{
"shortcuts": [
        {
            "command": "notebook:hide-cell-outputs",
            "keys": [
                "H"
            ],
            "selector": ".jp-Notebook:focus"
        },
        {
            "command": "notebook:show-cell-outputs",
            "keys": [
                "Shift H"
            ],
            "selector": ".jp-Notebook:focus"
        }
    ]
}

回答by Donal Mee

Depends if you consider the command palettea short-cut. I do.

取决于您是否认为命令面板是捷径。我愿意。

  1. Press 'control-shift-p', that opens the command palette.
  2. Then type 'clear cell output'. That will let you select the command to clear the output.
  1. 按'control-shift-p',打开命令面板。
  2. 然后输入“清除单元格输出”。这将让您选择清除输出的命令。

enter image description here

在此处输入图片说明