pythonpython-black

Is it possible to call Black as an API?


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.


Solution

  • You could try using format_str:

    from black import format_str, FileMode
    res = format_str("some python code", mode=FileMode())
    print(res)