dict2css
¶
A μ-library for constructing cascasing style sheets from Python dictionaries.
See also
css-parser, which this library builds upon.
Data:
The string |
|
Type annotation representing a style for |
Functions:
|
Construct a cascading style sheet from a dictionary. |
|
Construct a style sheet from a dictionary and write it to |
|
Parse a style sheet and return its dictionary representation. |
|
Parse a cascading style sheet from the given file and return its dictionary representation. |
|
Create a CSS Style Rule from a dictionary. |
Classes:
Represents a CSS style sheet. |
-
Style
¶ Type annotation representing a style for
make_style()
anddumps()
.The keys are CSS properties.
The values can be either:
-
dumps
(styles, *, indent='\t', trailing_semicolon=False, indent_closing_brace=False, minify=False)[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
,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 (
bool
) – Whether to add a semicolon to the end of the final property. DefaultFalse
.minify (
bool
) – Minify the CSS. Overrides all other options. DefaultFalse
.
- Return type
- Returns
The style sheet as a string.
Changed in version 0.2.0: Added support for media at-rules.
-
dump
(styles, fp, *, indent='\t', trailing_semicolon=False, indent_closing_brace=False, minify=False)[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
,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 (
bool
) – Whether to add a semicolon to the end of the final property. DefaultFalse
.minify (
bool
) – Minify the CSS. Overrides all other options. DefaultFalse
.
Changed in version 0.2.0:fp
now acceptsdomdf_python_tools.typing.PathLike
objects, representing the path of a file to write to.Added support for media at-rules.
-
loads
(styles)[source]¶ Parse a style sheet and return its dictionary representation.
New in version 0.2.0.
- Parameters
styles (
str
)- Return type
- 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.
-
class
StyleSheet
[source]¶ Represents a CSS style sheet.
Methods:
add
(rule)Add the
rule
to the style sheet.add_style
(selector, styles)Add a style to the style sheet.
add_media_styles
(media_query, styles)Add a set of styles for a media query to the style sheet.
tostring
()Returns the style sheet as a string.
-
add
(rule)[source]¶ Add the
rule
to the style sheet.- Parameters
rule (
css_parser.css.CSSRule
)- Return type
-