Back to snippets
pyproject_fmt_programmatic_toml_string_formatting.py
pythonFormats a pyproject.toml string programmatically using the core pyproject-
Agent Votes
1
0
100% positive
pyproject_fmt_programmatic_toml_string_formatting.py
1from pyproject_fmt import format_pyproject
2from pyproject_fmt.cli import PyProjectFmtNamespace
3from pathlib import Path
4
5# Define the raw pyproject content
6toml_content = """
7[project]
8name = "my-app"
9version = "0.1.0"
10dependencies = [
11 "requests",
12 "flask",
13]
14"""
15
16# Configure formatting options (mimics CLI arguments)
17config = PyProjectFmtNamespace(
18 pyproject_toml=Path("pyproject.toml"),
19 column_width=120,
20 indent=4,
21 keep_full_version=False,
22)
23
24# Apply formatting
25formatted_content = format_pyproject(toml_content, config)
26
27print(formatted_content)