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, typed Style, style resolution, and theming.

PythonNative ships a single, fully-typed Style TypedDict that enumerates every supported style property and constrains enum-shaped values via typing.Literal. The TypedDict gives editors and type-checkers (mypy, pyright) full autocomplete and validation: a typo such as flex_direction="collumn" is now a static error, not a silent runtime no-op.

Style values remain plain dicts at runtime so they are trivial to compose, diff, and store. Properties unrecognized by a platform handler are still ignored, so third-party handlers may extend the palette without modifying core types.

The runtime helpers (resolve_style, StyleSheet) accept either a Style TypedDict, a regular Dict[str, Any] (for forward-compat or unrestricted use), or a list of either kind of dict, and always return a fresh, flat dict.

Example
import pythonnative as pn

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

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

Classes:

Name Description
EdgeInsets

Per-edge spacing values used for padding and margin.

ShadowOffset

Shadow displacement in points.

TransformRotate

Rotation transform in degrees (numeric) or with explicit unit suffix.

TransformScale

Uniform scale transform.

TransformScaleX

Horizontal-only scale transform.

TransformScaleY

Vertical-only scale transform.

TransformTranslate

Translation transform along one or both axes.

Style

Statically-typed style dictionary.

StyleSheet

Utility for creating, composing, and flattening style dictionaries.

Functions:

Name Description
style

Construct a Style from keyword arguments.

resolve_style

Flatten a style prop into a single dict.

Attributes:

Name Type Description
Color

Color value: "#RRGGBB", "#AARRGGBB", or any string a platform

Dimension

A length value. Numbers are points/dp; strings ending in "%" are

EdgeValue

Padding/margin value: a uniform number, a "%" string, or an

TransformEntry

A single transform operation.

TransformSpec

transform style value: a single operation or an ordered list.

StyleProp

Public type for the style parameter on every component factory.

ThemeContext Context

Default theme context populated with DEFAULT_LIGHT_THEME.

Color module-attribute

Color = str

Color value: "#RRGGBB", "#AARRGGBB", or any string a platform handler recognizes (e.g., "red"). Stored verbatim and parsed by the handler at apply-time, so palettes from third-party libraries pass through unchanged.

Dimension module-attribute

Dimension = Union[int, float, str]

A length value. Numbers are points/dp; strings ending in "%" are parent-relative percentages.

EdgeValue module-attribute

EdgeValue = Union[Dimension, EdgeInsets]

Padding/margin value: a uniform number, a "%" string, or an EdgeInsets dict.

TransformEntry module-attribute

A single transform operation.

TransformSpec module-attribute

transform style value: a single operation or an ordered list.

StyleProp module-attribute

StyleProp = Union[Style, Dict[str, Any], List[Optional[Union[Style, Dict[str, Any]]]], None]

Public type for the style parameter on every component factory.

Accepts a Style TypedDict (recommended), a plain Dict[str, Any] (forward-compat / unrestricted), a list of either with None entries skipped, or None.

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).

EdgeInsets

Bases: TypedDict

Per-edge spacing values used for padding and margin.

Mirrors React Native's EdgeInsets shape with PythonNative's convenience aliases. Any subset of keys may be supplied.

ShadowOffset

Bases: TypedDict

Shadow displacement in points.

TransformRotate

Bases: TypedDict

Rotation transform in degrees (numeric) or with explicit unit suffix.

TransformScale

Bases: TypedDict

Uniform scale transform.

TransformScaleX

Bases: TypedDict

Horizontal-only scale transform.

TransformScaleY

Bases: TypedDict

Vertical-only scale transform.

TransformTranslate

Bases: TypedDict

Translation transform along one or both axes.

Style

Bases: TypedDict

Statically-typed style dictionary.

Lists every style property recognized by the built-in components plus their layout engine, with enum-shaped values constrained via Literal. Style is a total=False TypedDict so any subset of keys is valid at construction time.

Custom native components may accept additional, unlisted keys — they are ignored by the built-in handlers but flow through the reconciler unmodified, so third-party handlers can read them.

Example
import pythonnative as pn

title: pn.Style = {
    "font_size": 24,
    "color": "#0A84FF",
    "text_align": "center",
}

pn.Text("Hello", style=title)

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.

absolute_fill

Return a style that absolutely fills the parent.

create staticmethod

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

Create a set of named styles from keyword arguments.

Parameters:

Name Type Description Default
**named_styles Style

Each keyword argument is a style name mapping to a Style dict.

{}

Returns:

Type Description
Dict[str, Style]

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

Dict[str, Style]

so the caller can mutate the result without affecting the

Dict[str, Style]

originals.

Example
from pythonnative import StyleSheet, style

styles = StyleSheet.create(
    heading=style(font_size=28, bold=True),
    body=style(font_size=16),
)

compose staticmethod

compose(*styles: StyleProp) -> Style

Merge multiple style dicts.

Parameters:

Name Type Description Default
*styles StyleProp

Style dicts to merge. Later dicts override keys from earlier ones. Falsy entries (None) are skipped. List entries are flattened in turn.

()

Returns:

Type Description
Style

A new Style dict containing the merged result.

flatten staticmethod

flatten(styles: StyleProp) -> Style

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 and typed as returning a Style.

Parameters:

Name Type Description Default
styles StyleProp

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

required

Returns:

Type Description
Style

A flat Style dict combining the inputs.

absolute_fill staticmethod

absolute_fill() -> Style

Return a style that absolutely fills the parent.

Convenience preset matching React Native's StyleSheet.absoluteFill: position: "absolute" with every inset pinned to 0.

style

style(**properties: Any) -> Style

Construct a Style from keyword arguments.

Equivalent to Style(...) but works with any Python version (TypedDict.__init__ is fragile prior to 3.11) and reads more naturally inside expressions:

pn.View(child, style=pn.style(padding=16, background_color="#fff"))

Unknown keys are accepted at runtime to keep the door open for third-party styling extensions; static type checkers will still reject them when this helper's return type is annotated as Style.

Parameters:

Name Type Description Default
**properties Any

Style key/value pairs.

{}

Returns:

Type Description
Style

A fresh Style dict containing the supplied entries.

resolve_style

resolve_style(value: StyleProp) -> Dict[str, Any]

Flatten a style prop into a single dict.

Accepts None, a single dict (Style or untyped), 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
value StyleProp

The raw value of the component's style argument.

required

Returns:

Type Description
Dict[str, Any]

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

Dict[str, Any]

dict, never the input.

Next steps