如何配置黑色以使用不同的格式规则对不同的文件扩展名进行使用?

发布于 2025-01-21 13:12:05 字数 157 浏览 2 评论 0原文

我将黑色用于格式化normal .py文件以及jupyter笔记本文件文件(.ipynb)。对于笔记本电脑,我想要一个较短的行长

是否可以针对黑色的不同文件扩展名为不同的格式规则指定不同的格式规则?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

魂归处 2025-01-28 13:12:05

您可以为.py.ipynb文件创建两个单独的文件,并分别

docs

- 配置文件从文件路径读取配置。

- 包含文本的正则表达式,与应包含在递归搜索中的文件和目录匹配的正则表达式。

因此,要格式化多种类型的文件,运行类似的内容:

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

您也可以指定inclage 字段<代码> toml文件。也在文档中:

[tool.black]
line-length = 88
target-version = ['py37']
include = '\.pyi?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文