gizmobench
BrowseDaily

Python Formatter

Format supported Python source with Ruff 0.16.8 running locally as WebAssembly. Paste code or open a UTF-8 .py file, choose layout options, and copy or download the complete formatted code or unified diff. It formats valid syntax without running code, applying lint fixes or uploading your source. Input is limited to 64 KiB; the first Format downloads the pinned formatter from this site.

0 lines, 0 bytes of 64 KiB
40 to 120
Indent (spaces)
Quotes
the code is never run

formattedappears hereRuff 0.16.8

Paste Python into the box and press Format. The first Format downloads Ruff's 11.0 MB formatter from this site; your code stays in this browser and is never run.

Formatter
Ruff 0.16.8
Settings
88 cols, 4 spaces, double quotes
Lines
0 in
Diff
not yet

Ctrl+Enter or Cmd+Enter in the box formats too. Line length is the width Ruff fits lines to, from 40 to 120 characters; indent is the spaces per level; quotes is the quote Ruff prefers for strings, where preserve leaves each string as written and a string that would need more escapes keeps the other quote, so 'it\'s' becomes "it's" even under single. After a Format, changing a setting formats the same code again. Copy takes whichever view is showing; .py saves formatted.py and .diff saves formatted.diff, a unified diff from original.py that patch applies.

What comes out

  • x={"a":1,"b":2}default settings
    x = {"a": 1, "b": 2} and a final newline
  • result = some_function(argument_one, argument_two, argument_three)line length 40
    one argument per line, with a trailing comma after the last
  • def broken(:invalid syntax
    parse error at line 1, column 12; the input is kept exactly as typed
  • The formatted output, formatted againsame settings
    identical text, an empty diff
  • items=[1,True,None,{'name':'Ada'}]Python literals, default settings
    items = [1, True, None, {"name": "Ada"}] and a final newline
Accuracy. The output is what Ruff 0.16.8's formatter produces for the Python syntax it supports, and it can differ from Black in places. Your code is never run, lint fixes and import sorting are not applied, and a clean format says nothing about whether the code works. If the code does not parse, or formatting fails or runs past its limits, your original text stays exactly as you entered it. Formatting is done by Ruff 0.16.8 from Astral, compiled to WebAssembly and served from this site under the MIT license (copyright Charles Marsh); the full LICENSE, with the notices of the components Ruff bundles is served beside it. Your code never leaves this page: the only downloads are Ruff's two files (11.0 MB), from this site, on your first Format. Your line length, indent, quote and view settings are kept in this browser, and the Start over button at the top of the page forgets them; your code is not saved.

Common questions

Is this the same as running ruff format?
It runs the formatter from Ruff 0.16.8, the same version on every visit, compiled to WebAssembly by Astral and loaded from this site. You choose the line length (40 to 120), the indent (2, 4 or 8 spaces) and the quote style (single, double or preserve); every other formatter option stays at Ruff's default. A project whose pyproject.toml sets other options, such as tab indentation, can therefore get different output from ruff format there. It does not lint, fix lint errors or sort imports.
Is Ruff's output the same as Black's?
Mostly. Astral describes Ruff's formatter as a drop-in replacement for Black and documents the places where the two differ, so the output can differ from Black in those places. This page shows what Ruff 0.16.8 produces and makes no claim of byte-for-byte Black output.
What happens if my code has a syntax error?
Nothing is formatted, and the code in your box is left exactly as you typed it. The page shows Ruff's parser message with the line and column and marks the spot on that line: def broken(: stops at line 1, column 12 with Expected a parameter or the end of the parameter list. Fix that spot and press Format again.
Is my code run or uploaded?
No. The code is only text handed to Ruff's formatter in a background worker on this page; Ruff parses and prints it and has no way to execute it, so a raise statement or a line that writes a file is formatted, never run. Nothing you type is sent anywhere: the only downloads are Ruff's two files from this site on the first Format. Your line length, indent, quote and view settings are saved in this browser, never the code.
How big a file can it format?
Up to 64 KiB of UTF-8, which is 65,536 bytes. It counts bytes, so an accented letter such as é takes two and most emoji take four. The formatted result may be up to 256 KiB, and each format gets 2 seconds. A very long run of semicolon-separated statements on one line can take longer than that, and very deep nesting or a very long chain of calls can exhaust Ruff's stack; in each case the page says what happened and keeps your code.
Why does it not sort my imports or remove unused ones?
Those are lint fixes in Ruff (its isort rules and F401), not formatting. This page runs only Ruff's formatter, so import order, unused imports and comparisons such as x == None are left as written, and only the layout changes.
What does the diff show?
A unified diff from original.py to formatted.py with three lines of context, in the form the patch program reads, so you can review or apply exactly the lines Ruff changed. Copy takes whichever view is showing, and .diff saves the diff as formatted.diff.
Can I beautify a Python dictionary or list?
Yes. Paste a valid Python literal or assignment, such as items=[1,True,None,{'name':'Ada'}], then press Format. Ruff adjusts layout and quotes without evaluating the values or changing their order. Python uses True, False and None; JSON uses true, false and null. This formats Python source, not runtime pprint output or a Python-to-JSON conversion.
How do I format a local Python file?
Choose Open .py file and select a UTF-8 Python source file up to 64 KiB, then press Format. Files remain in this browser. Invalid UTF-8 or an oversized file leaves your current source unchanged. Download .py to save formatted.py, or download .diff to review or apply the exact text changes.
Can this fix Python indentation errors?
It can normalize an already valid block structure to 2, 4 or 8 spaces. If the indentation does not parse, Ruff reports the error and keeps your source. It cannot decide which statements belong inside a missing block, repair arbitrary syntax or check whether the program works.
Is a Python formatter also a linter or a PEP 8 checker?
Formatting adjusts the source layout. A linter checks additional code rules and may propose fixes. This page calls only Ruff’s formatter and does not run lint rules, sort imports, remove unused imports or certify complete PEP 8 compliance. Its default line length is 88, and you can choose another supported width.
Can I use this for JSON or Python logging formats?
Use JSON Formatter for strict JSON source, JSON viewing and JSON syntax validation. This page reads Python source and does not run json.dumps, logging.Formatter, date or numeric formatting APIs, or convert runtime Python objects to JSON. The separate Python Playground can run supported Python snippets within its own stated limits.
Will loading an example replace my saved settings?
No. Each example uses its own settings for this visit. Only the setting keys you explicitly choose are remembered, including a choice that matches the current value. Your source is never saved.

The output is what Ruff 0.16.8's formatter produces for the Python syntax it supports, and it can differ from Black in places. Your code is never run, lint fixes and import sorting are not applied, and a clean format says nothing about whether the code works. If the code does not parse, or formatting fails or runs past its limits, your original text stays exactly as you entered it.