Python Formatter (Ruff)
Format Python code with Ruff's Black-compatible formatter compiled to WebAssembly. Choose indentation and quote style; syntax errors show line and column.
Runs entirely in your browser. Your data is never uploaded.
Related Tools
View AllWhat is Python Formatter?
The Python Formatter runs the Ruff formatter – a fast, Black-compatible Python formatter – directly in your browser via WebAssembly. It normalises indentation, spacing, quotes, blank lines and line wrapping at 88 characters.
How to use Python Formatter
- Paste Python code into the input box or click Load sample.
- Choose the Indentation and Quote style.
- Click Format. The first run downloads the Ruff engine (WebAssembly, a few MB); later runs are instant.
- Copy or download the formatted code.
Example
def f(a,b):
return {'x':a}
def f(a, b):
return {"x": a} When to use Python Formatter
Use it to tidy scripts and snippets, format code for answers or teaching, or check that code parses. It formats only – it does not apply lint fixes such as removing unused imports or sorting imports. For projects, run ruff format or Black locally with your configuration.
How Python Formatter works
The official @astral-sh/ruff-wasm-web build of Ruff is loaded on demand. A Ruff workspace is created with line-length = 88, the chosen indent-width / indent-style and quote-style, and its format function is called. Ruff parses the code into a syntax tree and prints it in Black style (for example two blank lines between top-level definitions and magic trailing commas). If the code cannot be parsed, Ruff's error is shown with the line and column.
Common Use Cases
- Formatting Jupyter or script code before sharing
- Making code from tutorials PEP 8 friendly
- Checking Python syntax without installing anything
- Converting indentation from 2 to 4 spaces
Security Note
Everything runs locally in your browser; the code you paste is not uploaded.
FAQs
Is the output the same as Black?
Ruff's formatter is designed to be Black-compatible; differences are rare.
Why is the first run slow?
The Ruff WebAssembly module (a few MB) is downloaded once; the browser caches it.
Does it sort imports or remove unused ones?
No, it only formats. Those are lint fixes (Ruff I and F401 rules).
Is my code executed?
No, it is parsed and printed only.