dict2css
A μ-library for constructing cascading style sheets from Python dictionaries.
What it is and what it does
dict2css is a lightweight library that bridges Python dictionaries and CSS stylesheets. It provides dump and load functions—similar to the json and toml standard library modules—allowing you to construct CSS from nested Python dicts where keys are CSS selectors and values are property mappings. Property values can optionally include an "important" flag as a tuple. The library depends on domdf-python-tools and tinycss2 for parsing and serialization.
The package targets developers who want to programmatically generate or manipulate CSS within Python, such as in static site generators, template systems, or configuration-driven styling. It is typed, actively maintained, and supports modern Python versions from 3.8 onward.
Use it for:
- Generate CSS stylesheets programmatically from configuration dictionaries in static site generators or template engines.
- Parse existing CSS files into Python dicts for inspection, modification, or transformation.
- Build theme or style configuration systems where CSS is defined as nested Python data structures.
- Serialize dynamic styling rules computed at runtime into valid CSS output.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
dict2css converts Python dictionaries to CSS stylesheets and parses CSS back into dictionaries, with an API modeled on json and toml modules.
Yes, if you need to work with CSS as Python data structures. The library is small, actively maintained, has no known vulnerabilities, and installs with low friction. MIT licensing is unrestrictive. The main unknown is whether it handles advanced CSS features (at-rules, media queries) or only basic selectors and properties—verify against your use case before committing.
Install
dict2css on PyPI
pip
pip install dict2cssuv
uv add dict2csspoetry
poetry add dict2cssInstalling dict2css
Before you install
Low friction installation with a pure-Python wheel. Active maintenance as of 2026-07-06, with support across Python 3.8–3.13 and PyPy.
License in practice
MIT license is permissive; you can use, modify, and distribute dict2css with minimal restrictions.
Quickstart
from dict2css import dump
styles = {
"body": {"margin": "0", "padding": "0"},
".header": {"background-color": ("red", "important")}
}
css_string = dump(styles)
Verify before relying
- Whether the package handles CSS at-rules (@media, @keyframes, etc.) or only simple selectors and properties.
- Performance characteristics when working with large stylesheets.
- Whether round-trip fidelity (dump then load) preserves all CSS semantics.
Package facts
| License | MIT (permissive) |
| Python support | supports the current Python release (>=3.8) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 2 — domdf-python-tools, tinycss2 |
| Maintenance | actively maintained — 85 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 669,534/month — #5,413 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: dict2css-0.6.0-py3-none-any.whl
Keywords: css, serialize
Tags
More Python Modules packages
Converts domain names between Unicode and…
permissive · top 100 on PyPI
setuptoolsSetuptools is a Python build backend and…
permissive · top 100 on PyPI
PyYAMLPyYAML parses and emits YAML 1.1 data format,…
permissive · top 100 on PyPI
pydanticPydantic validates Python data structures…
permissive · top 100 on PyPI
annotated-typesProvides reusable metadata objects for use with…
permissive · top 100 on PyPI
typing-inspectionProvides runtime tools to inspect and…
permissive · top 100 on PyPI
cssutilscssutils parses and builds CSS stylesheets as a…
copyleft · top 5,000 on PyPI
pynlinerConverts CSS stylesheets to inline styles in…
permissive · top 15,000 on PyPI
cssselect2cssselect2 parses and matches CSS4 selectors…
permissive · top 1,000 on PyPI
css-inlineInlines CSS from style and link tags directly…
permissive · top 5,000 on PyPI
cssselectcssselect parses CSS3 selectors and translates…
permissive · top 1,000 on PyPI
dom-tomlProvides utilities and helpers for working with…
permissive · top 5,000 on PyPI
tree-sitter-cssProvides a CSS grammar parser for tree-sitter,…
permissive · top 5,000 on PyPI
csscompressorCompresses CSS by removing unnecessary…
permissive · top 5,000 on PyPI
tinycss2tinycss2 parses CSS strings into token and…
permissive · top 1,000 on PyPI
jsonsConverts Python objects (dataclasses, attrs…
permissive · top 5,000 on PyPI