]> git.madduck.net Git - etc/vim.git/blob - docs/blackd.md

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Refactor docs / Maintenance of docs (#1456)
[etc/vim.git] / docs / blackd.md
1 ## 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 paying the cost of starting up a new
5 _Black_ process every time you want to blacken a file.
6
7 ### Usage
8
9 `blackd` is not packaged alongside _Black_ by default because it has additional
10 dependencies. You will need to do `pip install black[d]` to install it.
11
12 You can start the server on the default port, binding only to the local interface by
13 running `blackd`. You will see a single line mentioning the server's version, and the
14 host and port it's listening on. `blackd` will then print an access log similar to most
15 web servers on standard output, merged with any exception traces caused by invalid
16 formatting requests.
17
18 `blackd` provides even less options than _Black_. You can see them by running
19 `blackd --help`:
20
21 ```text
22 Usage: blackd [OPTIONS]
23
24 Options:
25   --bind-host TEXT                Address to bind the server to.
26   --bind-port INTEGER             Port to listen on
27   --version                       Show the version and exit.
28   -h, --help                      Show this message and exit.
29 ```
30
31 There is no official blackd client tool (yet!). You can test that blackd is working
32 using `curl`:
33
34 ```sh
35 blackd --bind-port 9090 &  # or let blackd choose a port
36 curl -s -XPOST "localhost:9090" -d "print('valid')"
37 ```
38
39 ### Protocol
40
41 `blackd` only accepts `POST` requests at the `/` path. The body of the request should
42 contain the python source code to be formatted, encoded according to the `charset` field
43 in the `Content-Type` request header. If no `charset` is specified, `blackd` assumes
44 `UTF-8`.
45
46 There are a few HTTP headers that control how the source is formatted. These correspond
47 to command line flags for _Black_. There is one exception to this: `X-Protocol-Version`
48 which if present, should have the value `1`, otherwise the request is rejected with
49 `HTTP 501` (Not Implemented).
50
51 The headers controlling how code is formatted are:
52
53 - `X-Line-Length`: corresponds to the `--line-length` command line flag.
54 - `X-Skip-String-Normalization`: corresponds to the `--skip-string-normalization`
55   command line flag. If present and its value is not the empty string, no string
56   normalization will be performed.
57 - `X-Fast-Or-Safe`: if set to `fast`, `blackd` will act as _Black_ does when passed the
58   `--fast` command line flag.
59 - `X-Python-Variant`: if set to `pyi`, `blackd` will act as _Black_ does when passed the
60   `--pyi` command line flag. Otherwise, its value must correspond to a Python version or
61   a set of comma-separated Python versions, optionally prefixed with `py`. For example,
62   to request code that is compatible with Python 3.5 and 3.6, set the header to
63   `py3.5,py3.6`.
64 - `X-Diff`: corresponds to the `--diff` command line flag. If present, a diff of the
65   formats will be output.
66
67 If any of these headers are set to invalid values, `blackd` returns a `HTTP 400` error
68 response, mentioning the name of the problematic header in the message body.
69
70 Apart from the above, `blackd` can produce the following response codes:
71
72 - `HTTP 204`: If the input is already well-formatted. The response body is empty.
73 - `HTTP 200`: If formatting was needed on the input. The response body contains the
74   blackened Python code, and the `Content-Type` header is set accordingly.
75 - `HTTP 400`: If the input contains a syntax error. Details of the error are returned in
76   the response body.
77 - `HTTP 500`: If there was any kind of error while trying to format the input. The
78   response body contains a textual representation of the error.
79
80 The response headers include a `X-Black-Version` header containing the version of
81 _Black_.