How to have black format strings IN python code? [duplicate]
Say I want to use black as an API, and do something like:
import black
black.format("some python code")
Formatting code by calling the black
binary with Popen
is an alternative, but that's not what I'm asking.
You could try using format_str
:
from black import format_str, FileMode
res = format_str("some python code", mode=FileMode())
print(res)
Use black.format_file_contents
.
e.g.
import black
mode = black.FileMode()
fast = False
out = black.format_file_contents("some python code", fast, mode)
https://github.com/psf/black/blob/19.3b0/black.py#L642