菜鸟笔记
提升您的技术认知

Jupyter Lab通过安装插件利用autopep8等实现快速格式化代码

概述

Jupyter Lab本身并不支持代码格式化。目前,比较成熟的解决方案就是使用jupyterlab_code_formatter插件。

jupyterlab_code_formatter支持Python常见的代码格式化包,比如autopep8blackisort等,还可以自定义格式化工具。可以通过编辑器菜单、右键菜单、工具栏按钮等方式对单元格内的代码进行格式化。

jupyterlab_code_formatter项目地址为:https://github.com/ryantam626/jupyterlab_code_formatter
jupyterlab_code_formatter文档地址为:https://jupyterlab-code-formatter.readthedocs.io/en/latest/

安装

安装环境要求

Jupyter Lab最好版本大于3.0.0,Python版本要求3.6+。

第一步:安装jupyterlab_code_formatter

pip install jupyterlab_code_formatter

第二步:安装Python代码格式化包(如果已安装过,此步骤可省略)

jupyterlab_code_formatter支持Python常见的代码格式化包,比如autopep8blackisort等。
插件安装完成后,需要安装代码格式化包。

  1. 注意插件默认支持的包是isortblack,安装这两个包后续会避免很多问题! 安装命令为:pip install black isort 。执行该命令可省略第2个操作,不安装autopep8包。

  2. 由于不太熟悉isortblack,这里我们选择安装autopep8包。安装命令为:pip install autopep8

第三步:重启Jupyter Lab服务

注意!不是重启Jupyter Lab内核!

点击Jupyter Lab左侧栏插件图标,在INSTALLED列表下,可以观察到jupyterlab_code_formatter已安装。(务必启用插件功能)

应用插件

插件安装完成后,打开记事本即可对代码进行格式化,格式化方式有以下几种:

通过菜单进行格式化

正确安装插件和格式化包之后,在Edit菜单中会添加对应的菜单项。

注意!只有安装对应的格式化包才会显示相应的菜单项。

选中菜单项,将对当前单元格的代码进行格式化。

通过鼠标右键菜单格式化

插件安装成功后,右键点击单元格或笔记本空白处,在弹出的右键菜单中会出现Format cell菜单项。单击菜单项会对单元格或文件进行代码格式化。但是,前提是正确配置了默认格式化包(默认为isortblack。插件默认会依次使用isortblack进行格式化。由于这里没有安装isortblack,因此会提示错误。
没有正确安装配置isort则会提示如下错误Jupyterlab Code Formatter Error Formatter isort not found!


如果安装了isort但未配置,未安装black,则会提示如下错误Jupyterlab Code Formatter Error Formatter balck not found!

通过工具栏按钮

插件安装成功后,笔记本上方工具栏中会添加一个格式化按钮。单击按钮会对单元格或文件进行代码格式化。但是与右键菜单类似,前提是正确配置了默认格式化包

配置插件

配置插件方法如下:依次点击Jupyter Lab Settings菜单→ Advanced Settings Editor菜单项→Jupyterlab Code Formatter进入插件配置界面。

在右侧的User Preference中输入自定义配置即可覆盖默认配置。
系统配置为如下,默认使用isortblack作为格式化工具。

{
  
    // Jupyterlab Code Formatter
    // @ryantam626/jupyterlab_code_formatter:settings
    // Jupyterlab Code Formatter settings.
    // **********************************************

    // Autopep8 Config
    // Config to be passed into autopep8's fix_code function call as the options dictionary.
    "autopep8": {
  },

    // Black Config
    // Config to be passed into black's format_str function call.
    "black": {
  
        "line_length": 88,
        "string_normalization": true
    },

    // Auto format config
    // Auto format code when save the notebook.
    "formatOnSave": false,

    // FormatR Config
    // Config to be passed into formatR's tidy_source function call.
    "formatR": {
  
        "indent": 2,
        "arrow": true,
        "wrap": true,
        "width_cutoff": 150
    },

    // Isort Config
    // Config to be passed into isort's SortImports function call.
    "isort": {
  
        "multi_line_output": 3,
        "include_trailing_comma": true,
        "force_grid_wrap": 0,
        "use_parentheses": true,
        "line_length": 88
    },

    // Code Formatter Preferences
    // Preferences for this plugin
    "preferences": {
  
        "default_formatter": {
  
            "python": [
                "isort",
                "black"
            ],
            "r": "formatR"
        }
    },

    // Styler Config
    // Config to be passed into styler's style_text function call.
    "styler": {
  },

    // YAPF Config
    // Config to be passed into yapf's FormatCode function call.
    "yapf": {
  
        "style_config": "google"
    }
}

配置实例: 设置autopep8为默认格式工具

{
  
    "preferences": {
  
        "default_formatter": {
  
            "python": "autopep8",
            "R": "styler"
        }
    }
}