#code-formatter #python #black #http-client #blackd

app blackd-client

Blazing fast Python code formatting using Black

5 releases

0.1.0 May 22, 2023
0.0.6 Nov 2, 2021
0.0.5 Nov 1, 2021
0.0.4 May 23, 2021
0.0.3 May 14, 2021

#2064 in Command line utilities

GPL-3.0 license

30KB
227 lines

blackd-client

Tiny HTTP client for the Black (blackd) Python code formatter

Black is a brilliant, opinionated formatter for Python. However it can be quite slow when using an editor integration with format on save, since the process is cold-started every time you call it.

Luckily there's blackd, which is a small HTTP server that keeps the Black process running in the background so that it can be called directly without the lenghty startup time.

blackd-client is a simple helper that provides a single executable to communicate with Black, mainly for me to learn Rust.

If you're using Black (or writing Python code in general) I recommend you to check it out!

Install

  1. Install Black
pip install black

or using Homebrew on macOS (preferred)

brew install black
  1. Install blackd-client

Alternatively if you have Rust toolchain installed:

cargo install blackd-client
  1. Start blackd daemon
blackd

or as a launchd service using Homebrew on macOS (preferred)

sudo brew services start black

Usage

pipe file contents to stdin, e.g.

cat main.py | blackd-client

output is formatted using Black ✨

Benchmark comparison

Normal black --fast

 hyperfine 'cat subclean/core/parser.py | black --fast -'
  Time (mean ± σ):     296.8 ms ±  41.3 ms    [User: 228.7 ms, System: 51.1 ms]
  Range (min … max):   260.7 ms … 403.6 ms    10 runs

Using blackd-client

 hyperfine 'cat subclean/core/parser.py | blackd-client'
  Time (mean ± σ):      23.7 ms ±   3.7 ms    [User: 2.7 ms, System: 4.8 ms]
  Range (min … max):    19.2 ms …  35.7 ms    84 runs

Result: blackd is more than 10x faster! 🚀

Neovim integration

Editor integration for Neovim can be done using a general purpose language server. There are two options to choose from:

  1. null-ls (what I use)

  2. EFM

Dependencies

~285KB