reza1615 / jupyterlab_code_formatter

A universal code formatter for JupyterLab.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

jupyterlab_code_formatter

Binder npm version npm downloads code style

Code formatter for JupyterLab.

Prerequisites

  • JupyterLab installed under Python3.6+ environment

Why Python3.6+

  • Mainly because Black requires 3.6+, and I like using Black the most;
  • Typing support requires 3.5+, and I like typing;

Installation

Using pip

jupyter labextension install @ryantam626/jupyterlab_code_formatter
pip install jupyterlab_code_formatter
jupyter serverextension enable --py jupyterlab_code_formatter
# Remember to install one of the supported formatters (it's in the next section)

Using conda

conda install -c conda-forge black
jupyter labextension install @ryantam626/jupyterlab_code_formatter
conda install -c conda-forge jupyterlab_code_formatter
jupyter serverextension enable --py jupyterlab_code_formatter

Make sure to restart jupyter server after installation.

Installation of formatters

For jupyterlab_code_formatter to work, you would also need some formatters, three are currently supported:

  • Black;
  • YAPF;
  • Autopep8;
  • Isort;

Install at least one of them through pip install black for example (or its equivalent in other package management systems).

Usage

Assuming you do have one of the formatters installed in the Python environment that runs JupyterLab, some extra options would appear under JUPYTERLAB CODE FORMATTER, use those to apply formatting to current codecell (no multiple code cell application atm).

Configs

There are some formatter config exposed through the Jupyter Lab Advanced Settings Editor, have a browse and change it if you wish.

How about a keyboard shortcut?

Add an extra entry to your keyboard shortcuts settings with something like

{"jupyterlab_code_formatter:black":{
    "command": "jupyterlab_code_formatter:black",
    "keys": [
        "Ctrl K",
        "Ctrl M"
    ],
    "selector": ".jp-Notebook.jp-mod-editMode"
}}

This basically says "Under edit mode (detected through the selector), using the chord Ctrl K + Ctrl M, invoke the jupyterlab_code_formatter:black command". And there you have it 🎉

For Autopep8, YAPF or Isort, simply do autopep8/yapf/isort instead of black.

Using the Black formatter on Windows

You might see some errors about writing grammar tables when using the black formatter on Windows, simply manually create the directory as suggested in here and you should be good to go.

Development

For a development install (requires npm version 4 or later), do the following in the repository directory:

npm install
npm run build
jupyter labextension link .
pip install -e .

To rebuild the package and the JupyterLab app:

npm run build
jupyter lab build

About

A universal code formatter for JupyterLab.

License:MIT License


Languages

Language:Python 43.0%Language:TypeScript 40.1%Language:Jupyter Notebook 16.9%