Skip to content

Style

PythonNative styles are plain Python dicts; an element's style prop may be a single dict or a list of dicts (later entries win on key collision). StyleSheet is a small helper for declaring named styles and composing them.

StyleSheet, style resolution, and theming support.

Provides:

  • A StyleSheet helper for creating and composing reusable style dictionaries.
  • A resolve_style utility for flattening the style prop accepted by every component factory.
  • A pair of light and dark default themes plus a ThemeContext for distributing a theme dict across a subtree.

Style values are plain Python dicts so they are trivial to compose, diff, and store. Properties unrecognized by the platform handler are ignored.

Example
import pythonnative as pn

styles = pn.StyleSheet.create(
    title={"font_size": 24, "bold": True, "color": "#333"},
    container={"padding": 16, "spacing": 12},
)

pn.Column(
    pn.Text("Hello", style=styles["title"]),
    style=styles["container"],
)

Classes:

Name Description
StyleSheet

Utility for creating, composing, and flattening style dictionaries.

Functions:

Name Description
resolve_style

Flatten a style prop into a single dict.

Attributes:

Name Type Description
ThemeContext Context

Default theme context populated with DEFAULT_LIGHT_THEME.

ThemeContext module-attribute

ThemeContext: Context = create_context(DEFAULT_LIGHT_THEME)

Default theme context populated with DEFAULT_LIGHT_THEME.

Wrap a subtree in Provider(ThemeContext, my_theme, ...) to override the theme for that subtree, then read it inside descendants via use_context(ThemeContext).

StyleSheet

Utility for creating, composing, and flattening style dictionaries.

All methods are stateless and return fresh dicts, so the values can be reused safely across components.

Methods:

Name Description
create

Create a set of named styles from keyword arguments.

compose

Merge multiple style dicts.

flatten

Flatten a style value or list of styles into a single dict.

create staticmethod

create(**named_styles: _StyleDict) -> Dict[str, _StyleDict]

Create a set of named styles from keyword arguments.

Parameters:

Name Type Description Default
**named_styles _StyleDict

Each keyword argument is a style name mapping to a dict of property values.

{}

Returns:

Type Description
Dict[str, _StyleDict]

A dict mapping each name to a copy of the supplied dict, so

Dict[str, _StyleDict]

the caller can mutate the result without affecting the

Dict[str, _StyleDict]

originals.

Example
from pythonnative import StyleSheet

styles = StyleSheet.create(
    heading={"font_size": 28, "bold": True},
    body={"font_size": 16},
)

compose staticmethod

compose(*styles: _StyleDict) -> _StyleDict

Merge multiple style dicts.

Parameters:

Name Type Description Default
*styles _StyleDict

Style dicts to merge. Later dicts override keys from earlier ones.

()

Returns:

Type Description
_StyleDict

A new dict containing the merged result. Falsy entries

_StyleDict

(e.g., None) are skipped.

flatten staticmethod

flatten(styles: Any) -> _StyleDict

Flatten a style value or list of styles into a single dict.

Equivalent to resolve_style but exposed on StyleSheet for parity with React Native's API.

Parameters:

Name Type Description Default
styles Any

A single dict, a list of dicts, or None.

required

Returns:

Type Description
_StyleDict

A flat dict combining the inputs.

resolve_style

resolve_style(style: StyleValue) -> _StyleDict

Flatten a style prop into a single dict.

Accepts None, a single dict, or a list of dicts (later entries override earlier ones, mirroring React Native's array-style pattern). Used by every built-in element factory in pythonnative.components.

Parameters:

Name Type Description Default
style StyleValue

The raw value of the component's style argument.

required

Returns:

Type Description
_StyleDict

A flat dict suitable for the native handler. Always a fresh

_StyleDict

dict, never the input.

Next steps