| 1 |
# Black as a server (blackd) |
| 2 |
|
| 3 |
`blackd` is a small HTTP server that exposes _Black_'s functionality over a simple |
| 4 |
protocol. The main benefit of using it is to avoid the cost of starting up a new _Black_ |
| 5 |
process every time you want to blacken a file. |
| 6 |
|
| 7 |
```{warning} |
| 8 |
`blackd` should not be run as a publicly accessible server as there are no security |
| 9 |
precautions in place to prevent abuse. **It is intended for local use only**. |
| 10 |
``` |
| 11 |
|
| 12 |
## Usage |
| 13 |
|
| 14 |
`blackd` is not packaged alongside _Black_ by default because it has additional |
| 15 |
dependencies. You will need to execute `pip install 'black[d]'` to install it. |
| 16 |
|
| 17 |
You can start the server on the default port, binding only to the local interface by |
| 18 |
running `blackd`. You will see a single line mentioning the server's version, and the |
| 19 |
host and port it's listening on. `blackd` will then print an access log similar to most |
| 20 |
web servers on standard output, merged with any exception traces caused by invalid |
| 21 |
formatting requests. |
| 22 |
|
| 23 |
`blackd` provides even less options than _Black_. You can see them by running |
| 24 |
`blackd --help`: |
| 25 |
|
| 26 |
```{program-output} blackd --help |
| 27 |
|
| 28 |
``` |
| 29 |
|
| 30 |
You can test that blackd is working using `curl`: |
| 31 |
|
| 32 |
```sh |
| 33 |
blackd --bind-port 9090 & # or let blackd choose a port |
| 34 |
curl -s -XPOST "localhost:9090" -d "print('valid')" |
| 35 |
``` |
| 36 |
|
| 37 |
Or using the python client: |
| 38 |
|
| 39 |
```python |
| 40 |
import asyncio |
| 41 |
|
| 42 |
from blackd.client import BlackDClient |
| 43 |
|
| 44 |
async def main(): |
| 45 |
client = BlackDClient(url="http://127.0.0.1:9090") |
| 46 |
unformatted_code = "def hello(): print('Hello, World!')" |
| 47 |
formatted_code = await client.format_code(unformatted_code) |
| 48 |
print(formatted_code) |
| 49 |
|
| 50 |
if __name__ == "__main__": |
| 51 |
asyncio.run(main()) |
| 52 |
``` |
| 53 |
|
| 54 |
Cross-origin browser requests are rejected by default. If you need to access `blackd` |
| 55 |
from a browser-based client, pass one or more `--cors-allow-origin` options to allow |
| 56 |
specific origins. |
| 57 |
|
| 58 |
## Protocol |
| 59 |
|
| 60 |
`blackd` only accepts `POST` requests at the `/` path. The body of the request should |
| 61 |
contain the python source code to be formatted, encoded according to the `charset` field |
| 62 |
in the `Content-Type` request header. If no `charset` is specified, `blackd` assumes |
| 63 |
`UTF-8`. Request bodies are limited to 5 MiB by default; use `--max-body-size` to change |
| 64 |
that limit. |
| 65 |
|
| 66 |
There are a few HTTP headers that control how the source code is formatted. These |
| 67 |
correspond to command line flags for _Black_. There is one exception to this: |
| 68 |
`X-Protocol-Version` which if present, should have the value `1`, otherwise the request |
| 69 |
is rejected with `HTTP 501` (Not Implemented). |
| 70 |
|
| 71 |
The headers controlling how source code is formatted are: |
| 72 |
|
| 73 |
- `X-Line-Length`: corresponds to the `--line-length` command line flag. |
| 74 |
- `X-Skip-Source-First-Line`: corresponds to the `--skip-source-first-line` command line |
| 75 |
flag. If present and its value is not an empty string, the first line of the source |
| 76 |
code will be ignored. |
| 77 |
- `X-Skip-String-Normalization`: corresponds to the `--skip-string-normalization` |
| 78 |
command line flag. If present and its value is not the empty string, no string |
| 79 |
normalization will be performed. |
| 80 |
- `X-Skip-Magic-Trailing-Comma`: corresponds to the `--skip-magic-trailing-comma` |
| 81 |
command line flag. If present and its value is not an empty string, trailing commas |
| 82 |
will not be used as a reason to split lines. |
| 83 |
- `X-Preview`: corresponds to the `--preview` command line flag. If present and its |
| 84 |
value is not an empty string, experimental and potentially disruptive style changes |
| 85 |
will be used. |
| 86 |
- `X-Unstable`: corresponds to the `--unstable` command line flag. If present and its |
| 87 |
value is not an empty string, experimental style changes that are known to be buggy |
| 88 |
will be used. |
| 89 |
- `X-Enable-Unstable-Feature`: corresponds to the `--enable-unstable-feature` flag. The |
| 90 |
contents of the flag must be a comma-separated list of unstable features to be |
| 91 |
enabled. Example: `X-Enable-Unstable-Feature: feature1, feature2`. |
| 92 |
- `X-Fast-Or-Safe`: if set to `fast`, `blackd` will act as _Black_ does when passed the |
| 93 |
`--fast` command line flag. |
| 94 |
- `X-Python-Variant`: if set to `pyi`, `blackd` will act as _Black_ does when passed the |
| 95 |
`--pyi` command line flag. Otherwise, its value must correspond to a Python version or |
| 96 |
a set of comma-separated Python versions, optionally prefixed with `py`. For example, |
| 97 |
to request code that is compatible with Python 3.5 and 3.6, set the header to |
| 98 |
`py3.5,py3.6`. |
| 99 |
- `X-Diff`: corresponds to the `--diff` command line flag. If present, a diff of the |
| 100 |
formats will be output. |
| 101 |
|
| 102 |
If any of these headers are set to invalid values, `blackd` returns a `HTTP 400` error |
| 103 |
response, mentioning the name of the problematic header in the message body. |
| 104 |
|
| 105 |
Apart from the above, `blackd` can produce the following response codes: |
| 106 |
|
| 107 |
- `HTTP 204`: If the input is already well-formatted. The response body is empty. |
| 108 |
- `HTTP 200`: If formatting was needed on the input. The response body contains the |
| 109 |
blackened Python code, and the `Content-Type` header is set accordingly. |
| 110 |
- `HTTP 400`: If the input contains a syntax error. Details of the error are returned in |
| 111 |
the response body. |
| 112 |
- `HTTP 500`: If there was any other kind of error while trying to format the input. The |
| 113 |
response body contains a textual representation of the error. |
| 114 |
|
| 115 |
The response headers include a `X-Black-Version` header containing the version of |
| 116 |
_Black_. |