DarkMatter in Cyberspace
  • Home
  • Categories
  • Tags
  • Archives

JupyterLab Notes


Install and Start

在 virtualenv 里执行:

pip install jupyterlab pandas numpy matplotlib seaborn plotly
jupyter lab

当前的 3.0.1 版本的文本编辑器对 vim 支持良好, Settings > Text Editor Kay Map: vim。

Customize Keyboard Shortcuts

Settings > Advanced Settings Editor, 在右侧 User Preferences 窗口中输入:

{
    "shortcuts": [
        {
            "command": "notebook:run-in-console",
            "keys": [
                "Alt E"
            ],
            "selector": ".jp-Notebook.jp-mod-editMode"
        },
    ]
}

注意观察窗口底部状态栏右侧有无错误提示,若没有, User Preferences 窗口右上角的保存图标变为有效状态,Ctrl-S 保存, 这个文件会被保存到 ~/.jupyter/lab/user-settings/@jupyterlab/shortcuts-extension/shortcuts.jupyterlab-settings 文件中。

Notebook-Console Loop

Notebook-Console 循环的工作模式: 将一次性的代码放在 console 里执行,将其中能够形成最终结果的部分保存在 notebook 里, 形成最终的报告。

Jupyter lab 服务启动后自动打开浏览器页面, 用左侧的 Files Browser 里选择并打开 demographics notebook, 开启一个与此 editor 对应的 console: File > New Console for notebook (注意关闭 Single Document Mode,否则无法出现在同一窗口中), 快捷键 Ctrl-Shift-' 在两个 tab 页间跳转(定义在 Tabs > Activate Previously Used Tab 中), Alt-E 将 editor 中当前行或选中文本发送到 console 里执行。

Frequently Used Keys

Alt-w: close tab;


2018.10.17 版本:

Install

Install JupyterLab with conda install -c conda-forge jupyterlab.

Configuration

配置文件在 ~/.jupyter/ 下,主要是 jupyter_notebook_config.py 和 jupyter_notebook_config.json 两个文件。

Disable Token and Start Server

如果不设置秘密则 Jupyter 每次生成不同的 token,会造成奇怪的安全错误, 所以下面为 Jupyter notebook 设置了密码,然后在指定端口启动:

$ jupyter notebook password
Enter password: 
Verify password: 
[NotebookPasswordApp] Wrote hashed password to /home/leo/.jupyter/jupyter_notebook_config.json
$ jupyter lab --port 8765 --no-browser

Customize Keyboard Shortcuts

在 Settings > Advanced Settings Editor > Keyboard Shortcuts 中设置快捷键, 例如下面两个快捷键分别是 重启 Kernel 并执行所有 cell 和 在 console 中执行当前行当前行代码:

{
    "notebook:restart-run-all": {
      "command": "notebook:restart-run-all",
      "keys": [
        "Alt Shift W"
      ],
      "selector": ".jp-Notebook:focus",
      "title": "Restart Kernel and Run All",
      "category": "Main Area"
    },
    "notebook:run-in-console": {
      "command": "notebook:run-in-console",
      "keys": ["Alt Shift M"],
      "selector": ".jp-Notebook:focus",
      "title": "Run Cell(s) and Insert Below",
      "category": "Notebook Cell Operations"
    }
}

其中最重要的是 command 和 keys,快捷键的名字与 command 一致即可, 没找到 selector 的说明文档,category 似乎可以随便写。 定义后保存,无需重启立即生效。

命令的完整列表见 namespace CommandIDs in index.ts

JupyterLab 目前还没用跳转快捷键,体验上不如 VS Code.

vim mode

jupyterlab-vim 是 JupyterLab 的 vim mode 实现。

它有3个状态:Jupyter command, vim command, vim insert,互相之间的切换方式:

  • Jupyter command > vim command: Enter

  • vim command > Jupyter command: Shift + ESC

  • vim command > vim insert: i

  • vim insert > vim command: ESC

完整快捷键列表:jupyterlab-vim。

在 Settings > Advanced Settings Editor 中设置的快捷键只在 Jupyter command 模式下生效,例如下面为 在 console 中执行当前行 分配快捷键 Alt+Shift+m, 只能先在 vim mode 下将光标移动到目标行,按 ESC 切换到 Jupyter command 状态, 再 Alt+Shift+m 执行当前行。

P.S.: 使用 Jupyterlab 时若提示升级 jupyterlab-vim,需要 node.js 版本 >=6.11.5, 则首先升级 node.js:

conda install -c conda-forge nodejs

Workspace Management

URL: http://localhost:8765/lab/workspaces/mywork

Integrated with jupyterhub

$ conda install -c conda-forge jupyterhub

Ref:

Jupyter lab shortcuts



Published

Oct 17, 2018

Last Updated

Jan 5, 2021

Category

Tech

Tags

  • jupyterlab 1
  • notebook 5
  • python 136

Contact

  • Powered by Pelican. Theme: Elegant by Talha Mansoor