如何将键盘快捷键永久添加到Jupyter(ipython)笔记本?

Apo*_*tus 11 startup ipython ipython-notebook jupyter jupyter-notebook

我有以下快捷方式配置,在Jupiter笔记本的单元格中运行后可以正常工作:

%%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;                   
    }
  });
Run Code Online (Sandbox Code Playgroud)

如何设置Jupiter笔记本以在启动时自动进行初始化?

我尝试添加相同的代码(没有%%javascript)C:\Users\<username>\.ipython\profile_default\static\custom\custom.js但它没有用.

我只有一个配置文件,使用ipython profile createPython 3.3,Windows 7创建.

提前致谢.

Dmi*_*tri 8

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

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

    return true;
});
Run Code Online (Sandbox Code Playgroud)

  • 您真的相信“&lt;您的代码&gt;”足以让某人使用您的方法创建键盘快捷键吗?! (7认同)

Sal*_*ali 7

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

要做到这一点Help- >Edit keyboard shortcuts

在此输入图像描述

  • 请注意,这不能解决编辑模式的键盘快捷键,只能解决命令模式的快捷键。因此,虽然表面上可以使用“输入命令模式”,但实际上不是由该界面设置的。 (2认同)
  • 这也不是持久的 (2认同)
  • @Celtor 它在 Jupyter 4.4.0 上对我来说是持久的,保存在 `~/.jupyter/nbconfig/notebook.json` 下。 (2认同)

Sac*_*pal 5

1. 更改命令模式快捷键:参考 Salvador 的回答

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

编辑文件~/.jupyter/nbconfig/notebook.json,https://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"
      }  
    } 
  }   
} 
Run Code Online (Sandbox Code Playgroud)