A Quarto filter to format the python code using black
quarto add shafayetShafee/black-formatter
This will install the extension under the _extensions
subdirectory.
If you're using version control, you will want to check in this directory.
Before using this filter, make sure black is installed already. See here for installation command.
Then add the filter in your quarto document
---
filters:
- black-formatter
---
And it is possible to specify the black command line options via a pyproject.toml
file.
Suppose you want to use 80 characters per line i.e. --line-length=80
(Black defaults to 88 character per line), you can specify the follwoing in pyproject.toml
file under [tool.black]
,
[tool.black]
line-length = 80
And then place this toml file in the same directory along with the qmd file of which python code-chunks you want to format.
Here is the source code for a minimal example: example.qmd and view the rendered output with black-formatted code here