Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I configure black to use different formatting rules for different file extensions?

I use black for format normal .py files as well as Jupyter Notebook files (.ipynb). For notebooks, I want a shorter line-length.

Is it possible to specify different formatting rules for different file extensions with black?

like image 261
grisaitis Avatar asked Jan 29 '26 04:01

grisaitis


1 Answers

You could create two separate files for .py and .ipynb files and run them separately

Some usefull flags from docs:

--config FILE Read configuration from FILE path.

--include TEXT A regular expression that matches files and directories that should be included on recursive searches.

So, to format multiple types of files, run something like:

python -m black --config pyproject.py.toml --include '*.py' src
python -m black --config pyproject.ipynb.toml --include '*.ipynb' src

Also you could specify include field inside toml files. It's in docs too:

[tool.black]
line-length = 88
target-version = ['py37']
include = '\.pyi?$'
like image 100
Alex Kosh Avatar answered Jan 30 '26 20:01

Alex Kosh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!