项目作者: ryantam626

项目描述 :
A JupyterLab extension to apply Black formatter to code within codecell.
高级语言: TypeScript
项目地址: git://github.com/ryantam626/jupyterlab_black.git
创建时间: 2018-07-18T21:45:29Z
项目社区:https://github.com/ryantam626/jupyterlab_black

开源协议:MIT License

下载


NOTE

If you are using Python3.6+, feel free to use my other less hacky plugin - it supports YAPF and autopep8 as well.

However if you are using Python2 or Python(<3.5), keep going.

jupyterlab_black

npm version
npm downloads

A JupyterLab extension to apply Black formatter to code within codecell.

Here is a little demo. Apologies for the horrid quality, I promise to find a better converter…

Prerequisites

  • JupyterLab
  • A Python 3.6+ anywhere on your system with black installed

Installation

From your existing environment with jupyterlab installed do the following:

  1. jupyter labextension install @ryantam626/jupyterlab_black
  2. pip install https://github.com/ryantam626/jupyterlab_black/archive/master.zip
  3. jupyter serverextension enable --py jupyterlab_black

Python3.6+ Kernel Notes

Install black with something like:

  1. conda install -c conda-forge black

OR

  1. pip install black

Find out path of your Python by which python and that would be what to put for blackPythonBin in the Usage section.

Non-Python3.6+ Kernel Notes

If you are using kernel that are not Python3.6+, create another environment with Python3.6+ and black installed, for example (with conda, my perferred environment manager):

  1. conda create --name py36_for_jupyterlab_black -c conda-forge python=3.6 black

Then find out where this lives by:

  1. source activate py36_for_jupyterlab_black
  2. which python

It should be something like /home/ryan/lib/anaconda2/envs/py36_for_jupyterlab_black/bin/python, depending on your distribution of conda, that would be what to put for blackPythonBin in the Usage section.

Usage

Head over to settings editor, and key in the python interpreter path of that Python3.6+ you have with black installed for blackPythonBin.

There is literally one option in the command palette right now:

  • Apply Black Formatter

How about a keyboard shortcut?

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

  1. {"jupyterlab_black:format":{
  2. "command": "jupyterlab_black:format",
  3. "keys": [
  4. "Ctrl K",
  5. "Ctrl L"
  6. ],
  7. "selector": ".jp-Notebook.jp-mod-editMode"
  8. }}

This basically says “Under edit mode (detected through the selector), using the chord Ctrl K + Ctrl L, invoke the jupyterlab_black:format command”. And there you have it :tada:

Development

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

  1. pip install -e .
  2. jupyter serverextension enable --py jupyterlab_black
  3. npm install
  4. npm run build
  5. jupyter labextension install . --no-build

Get npm and jupyter to watch for changes:

  1. npm run watch # in terminal 1
  2. jupyter lab --watch # in terminal 2

Making changes in the Python part of the plugin will require a rerun of jupyter lab --watch sadly.