Registry / serialization / dict2css

dict2css

JSON →
library0.6.0pypypi✓ verified 24d ago

dict2css is a μ-library for constructing cascading style sheets from Python dictionaries. It simplifies the process of programmatically generating CSS by mapping Python dictionary structures to CSS rules. The current version is 0.3.0.post1, with an irregular release cadence, having recent updates in late 2023.

pip install dict2css
INSTALL
IMPORT
SIG · DICT2CSS
D
dict2css
serializationpythonv0.6.0
Install
2.1s avg
Import
110ms
Disk
18MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.6.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.108s · 19.4MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 2.1s · import 0.112s · 20MB
18MB installed
● package 18MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

CSSSerializer
from dict2css import CSSSerializer
from dict2css import make_css
dump
from dict2css import dump
from dict2css import make_css
Style
from dict2css import Style
from dict2css import make_css

This quickstart demonstrates how to define a CSS structure using a Python dictionary and then convert it into a valid CSS string using the `make_css` function. It covers basic element styling, ID selectors, and nested properties.

from dict2css import make_css # Define your CSS structure as a Python dictionary style_dict = { "body": { "font-family": "'Arial', sans-serif", "margin": "0", "padding": "0", "background-color": "#f0f0f0" }, "h1": { "color": "#333", "text-align": "center" }, "p": { "font-size": "16px", "line-height": "1.5", "color": "#666" }, "#main-content": { "width": "80%", "margin": "20px auto", "padding": "20px", "background": "#fff", "border-radius": "8px" } } # Convert the dictionary to a CSS string css_output = make_css(style_dict) print(css_output)
Debug
Known issues
gotchaThe `make_css` convenience function was introduced in `v0.3.0` as the primary, simpler interface for converting dictionaries. While `DictionaryToCss` class still works, new code should prefer `make_css`.
fix
Migrate existing calls from `DictionaryToCss().convert(data)` to `make_css(data)` for a more streamlined API.
affects: Before v0.3.0
gotchaThe library primarily supports basic element, class, and ID selectors with nested properties. It does not natively provide direct dictionary-based support for complex CSS features like pseudo-classes (`:hover`), pseudo-elements (`::before`), or media queries (`@media`) within the standard nested dictionary structure.
fix
For advanced CSS features, consider generating them as separate strings, concatenating them, or using a more comprehensive CSS preprocessor or templating engine if dynamic generation is required.
affects: All versions
gotchadict2css performs minimal validation on the *values* provided in the input dictionary. Supplying syntactically incorrect or semantically invalid CSS property values (e.g., specifying a non-existent color or unit) will result in invalid CSS being generated without explicit errors from the library itself.
fix
Ensure that all dictionary values correspond to valid CSS property values according to CSS specifications to avoid generating malformed stylesheets.
affects: All versions
Upgrade
Version history
0.6.0latest on PyPI · released May 21, 2026
Audit
Dependencies

No dependency data recorded yet.

Agent activity
11 hits · last 30 days
node
10
Resources
dict2css — pip install dict2css · libregistry