dict2css

A μ-library for constructing cascasing style sheets from Python dictionaries.

Data:

IMPORTANT

The string 'important'.

Style

Type annotation representing a style for dumps() and dump().

Functions:

dumps(styles, *[, indent, …])

Construct a cascading style sheet from a dictionary.

dump(styles, fp, *[, indent, …])

Construct a style sheet from a dictionary and write it to fp.

loads(styles)

Parse a style sheet and return its dictionary representation.

load(fp)

Parse a cascading style sheet from the given file and return its dictionary representation.

IMPORTANT = 'important'

Type:    str

The string 'important'.

Style

Type annotation representing a style for dumps() and dump().

The keys are CSS properties.

The values can be either:

  • A str, float or None, giving the value of the property.

  • A tuple of the property’s value (as above) and the priority such as IMPORTANT (which sets !important on the property).

Alias of Mapping[str, Union[Sequence, str, int, float, None]]

dumps(styles, *, indent='\t', trailing_semicolon=False, indent_closing_brace=False, minify=False, sort_keys=False, check_circular=True, none_style='none')[source]

Construct a cascading style sheet from a dictionary.

styles is a mapping of CSS selector strings to styles, which map property names to their values:

styles = {".wy-nav-content": {"max-width": (px(1200), IMPORTANT)}}
print(dumps(styles))
.wy-nav-content {
    max-width: 1200px !important
}

See the Style object for more information on the layout.

The keys can also be media at-rules, with the values mappings of property names to their values:

styles = {
    "@media screen and (min-width: 870px)": {
        ".wy-nav-content": {"max-width": (px(1200), IMPORTANT)},
        },
    }
print(dumps(styles))
@media screen and (min-width: 870px) {
    .wy-nav-content {
        max-width: 1200px !important
    }
}
Parameters
  • styles (Mapping[str, Union[Mapping[str, Union[Sequence, str, int, float, None]], Mapping]]) – A mapping of CSS selectors to styles.

  • indent (str) – The indent to use, such as a tab (\t), two spaces or four spaces. Default '\t'.

  • trailing_semicolon (Optional[bool]) – Whether to add a semicolon to the end of the final property. Default False.

  • indent_closing_brace (bool) – Default False.

  • minify (bool) – Minify the CSS. Overrides all other options. Default False.

  • sort_keys (bool) – Sort dictionary keys alphabetically. Default False.

  • check_circular (bool) – Check for circular references. Default True.

  • none_style (Union[Literal['none'], Literal['None']]) – Whether to represent None as None or none. Default 'none'.

Return type

str

Returns

The style sheet as a string.

Changed in version 0.2.0: Added support for media at-rules.

Changed in version 0.5.0: New implementation. Output may differ slightly from previous css-parser based one.

Changed in version 0.6.0: Added none_style option.

dump(styles, fp, *, indent='\t', trailing_semicolon=False, indent_closing_brace=False, minify=False, sort_keys=False, check_circular=True, none_style='none')[source]

Construct a style sheet from a dictionary and write it to fp.

styles = {".wy-nav-content": {"max-width": (px(1200), IMPORTANT)}}
dump(styles, ...)
.wy-nav-content {
    max-width: 1200px !important
}

See the Style object for more information on the layout.

The keys can also be media at-rules, with the values mappings of property names to their values:

styles = {
    "@media screen and (min-width: 870px)": {
        ".wy-nav-content": {"max-width": (px(1200), IMPORTANT)},
        },
    }
dump(styles, ...)
@media screen and (min-width: 870px) {
    .wy-nav-content {
        max-width: 1200px !important
    }
}
Parameters
  • styles (Mapping[str, Union[Mapping[str, Union[Sequence, str, int, float, None]], Mapping]]) – A mapping of CSS selectors to styles.

  • fp (Union[str, Path, PathLike, IO]) – An open file handle, or the filename of a file to write to.

  • indent (str) – The indent to use, such as a tab (\t), two spaces or four spaces. Default '\t'.

  • trailing_semicolon (Optional[bool]) – Whether to add a semicolon to the end of the final property. Default False.

  • indent_closing_brace (bool) – Default False.

  • minify (bool) – Minify the CSS. Overrides all other options. Default False.

  • sort_keys (bool) – Sort dictionary keys alphabetically. Default False.

  • check_circular (bool) – Check for circular references. Default True.

  • none_style (Union[Literal['none'], Literal['None']]) – Whether to represent None as None or none. Default 'none'.

Changed in version 0.2.0:

Changed in version 0.5.0: New implementation. Output may differ slightly from previous css-parser based one.

Changed in version 0.6.0: Added none_style option.

loads(styles)[source]

Parse a style sheet and return its dictionary representation.

New in version 0.2.0.

Changed in version 0.5.0: New implementation. Output may differ slightly from previous css-parser based one.

Parameters

styles (str)

Return type

MutableMapping[str, MutableMapping[str, Any]]

Returns

The style sheet as a dictionary.

load(fp)[source]

Parse a cascading style sheet from the given file and return its dictionary representation.

New in version 0.2.0.

Changed in version 0.5.0: New implementation. Output may differ slightly from previous css-parser based one.

Parameters

fp (Union[str, Path, PathLike, IO]) – An open file handle, or the filename of a file to write to.

Return type

MutableMapping[str, MutableMapping[str, Any]]

Returns

The style sheet as a dictionary.