【问题标题】:How to add keyboard shortcuts permanently to Jupyter (ipython) notebook?如何将键盘快捷键永久添加到 Jupyter(ipython)笔记本?
【发布时间】:2015-08-17 09:14:33
【问题描述】:

我有以下快捷方式配置,在 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;                   
    }
  });

如何设置 Jupiter notebook 在启动时自动进行初始化?

我尝试将相同的代码(不带%%javascript)添加到C:\Users\<username>\.ipython\profile_default\static\custom\custom.js,但没有成功。

我只有一个配置文件,使用ipython profile create、Python 3.3、Windows 7 创建。

提前致谢。

【问题讨论】:

  • 请注意,对于 jupyter,custom.js 位于 ~/.jupyter/custom/custom.js

标签: ipython ipython-notebook startup jupyter-notebook jupyter


【解决方案1】:

Jupyter notebook 的新版本中(使用pip install --upgrade notebook 或者如果您使用conda conda upgrade notebook 进行更新),您可以从笔记本本身自定义它们。

为此帮助 -> 编辑键盘快捷键

【讨论】:

  • 请注意,这并不能解决编辑模式的键盘快捷键,只能解决命令模式。因此,虽然enter command mode 表面上可用,但实际上并没有由该接口设置。
  • 这也不是持久的
  • @Celtor 它在 Jupyter 4.4.0 上对我来说是持久的,保存在 ~/.jupyter/nbconfig/notebook.json 下。
  • 对我不起作用。我正在尝试为restart kernel and run all cells 分配一个快捷方式,但是一旦我从对话框中保存然后重新打开对话框,快捷方式就消失了。它根本不起作用
  • @JayChakra 我可能迟到了,但我遇到了同样的问题,这里有一些必须注意的事情:1-根据底部的解释,您选择的组合键必须是有效的分配键的位置 2- 它不得已用于另一个命令(键入时不得变为红色) 3- 您必须按 Enter 键并看到它被添加到它的左侧。
【解决方案2】:

custom.js 是此代码的正确位置。尝试将其包装如下(不要忘记块末尾之前的return true):

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

    return true;
});

【讨论】:

  • 你真的相信“”足以让某人使用你的方法创建键盘快捷键吗?!
【解决方案3】:

1.如需更改命令模式快捷方式:请参阅 Salvador 的回答

2。更改编辑模式快捷键:

编辑文件,~/.jupyter/nbconfig/notebook.jsonhttps://jupyter-notebook.readthedocs.io/en/stable/extending/keymaps.html中所述

例如,将执行代码的 control-enter 快捷键替换为 macOS 上的 command-enter 后,文件如下所示:

{
  "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"
      }  
    } 
  }   
} 

【讨论】:

  • 1.这对命令和编辑模式都不起作用吗? 2.上面萨尔瓦多·达利的回答不是只是通过GUI编辑这个文件吗?
  • 萨尔瓦多的回答只改变了命令模式的快捷方式,而不是编辑模式
  • @Sachit Nagpal,感谢您的回复。是否有文件支持你所说的?我在上面的代码 sn-p 中看到了"edit": ,我尝试使用版本6.1.4,它似乎也配置了edit 模式键盘映射。
  • 我的意思是编辑~/.jupyter/nbconfig/notebook.json 应该适用于命令和编辑模式。
  • 您可以相应地编辑文件。然后不要在编辑键下绑定/取消绑定任何东西,如果您只想编辑命令模式快捷方式,请仅在命令键下添加绑定。
【解决方案4】:

使用 nbextensions 轻松添加热键

  1. 安装nbextensions
    pip install jupyter_contrib_nbextensions
  2. 然后启动 jupyter notebook。
  3. 介绍页面将有一个名为 nbextensions 的新选项卡,单击它并启用键盘快捷键编辑器。
  4. 现在打开任何笔记本点击帮助>键盘快捷键
  5. 如果您单击每个快捷方式,其旁边都会有一个铅笔图标,然后您可以将快捷方式设置为您想要的任何内容。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-04
    • 2016-12-02
    • 2017-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多