Skip to content

xnano.components.schema

xnano.components.schema

xnano.components.schema


Declare table columns and chart series as component class attributes.

Classes:

ComponentDescriptor dataclass

ComponentDescriptor()

Named declarative component field.

Attributes:

  • name (str) –

    Attribute name assigned by the owning component class.

name class-attribute instance-attribute

name: str = dataclasses.field(default='', init=False)

Attribute name assigned by the component class.

Column dataclass

Column(
    header: str | None = None,
    accessor: Callable[[Any], Any] | None = None,
    format: FormatResolver = None,
    foreground: ColorResolver = None,
    background: ColorResolver = None,
    horizontal_align: "Alignment | None" = None,
    width: int | float | None = None,
)

Bases: ComponentDescriptor

Describe one table column.

Attributes:

  • header (str | None) –

    Header displayed for the column.

  • accessor (Callable[[Any], Any] | None) –

    Row attribute, mapping key, or value callback.

  • format (FormatResolver) –

    Optional format string or value callback.

  • foreground (ColorResolver) –

    Cell foreground color.

  • background (ColorResolver) –

    Cell background color.

  • horizontal_align ('Alignment | None') –

    Cell alignment.

  • width (int | float | None) –

    Fixed or proportional column width.

Methods:

header class-attribute instance-attribute

header: str | None = None

Displayed column header.

accessor class-attribute instance-attribute

accessor: Callable[[Any], Any] | None = None

Custom row value accessor.

format class-attribute instance-attribute

format: FormatResolver = None

Value formatter.

foreground class-attribute instance-attribute

foreground: ColorResolver = None

Foreground color or resolver.

background class-attribute instance-attribute

background: ColorResolver = None

Background color or resolver.

horizontal_align class-attribute instance-attribute

horizontal_align: 'Alignment | None' = None

Cell text alignment.

width class-attribute instance-attribute

width: int | float | None = None

Fixed width or fractional width.

name class-attribute instance-attribute

name: str = dataclasses.field(default='', init=False)

Attribute name assigned by the component class.

resolve_header

resolve_header() -> str

Return the displayed header.

Source code in xnano/components/schema.py
def resolve_header(self) -> str:
    """Return the displayed header."""
    return self.header or self.name.replace("_", " ").title()

resolve_value

resolve_value(row: Any) -> Any

Read this column's value from a row.

Source code in xnano/components/schema.py
def resolve_value(self, row: Any) -> Any:
    """Read this column's value from a row."""
    if self.accessor is not None:
        return self.accessor(row)
    if isinstance(row, dict):
        return row.get(self.name)
    return getattr(row, self.name, None)

resolve_text

resolve_text(value: Any) -> str

Format a value for display.

Source code in xnano/components/schema.py
def resolve_text(self, value: Any) -> str:
    """Format a value for display."""
    if value is None:
        return ""
    if self.format is None:
        return str(value)
    if isinstance(self.format, str):
        return self.format.format(value)
    return self.format(value)

resolve_color

resolve_color(value: Any) -> Any

Resolve the foreground color for a value.

Source code in xnano/components/schema.py
def resolve_color(self, value: Any) -> Any:
    """Resolve the foreground color for a value."""
    resolver = self.foreground
    return resolver(value) if callable(resolver) else resolver

resolve_background

resolve_background(value: Any) -> Any

Resolve the background color for a value.

Source code in xnano/components/schema.py
def resolve_background(self, value: Any) -> Any:
    """Resolve the background color for a value."""
    return (
        self.background(value)
        if callable(self.background)
        else self.background
    )

Series dataclass

Series(
    label: str | None = None,
    color: "ColorLike | None" = None,
    kind: "GraphTypeLike | None" = None,
    marker: "CanvasMarkerLike | None" = None,
)

Bases: ComponentDescriptor

Describe one chart series.

Attributes:

  • label (str | None) –

    Legend label; None derives it from the attribute name.

  • color ('ColorLike | None') –

    Series color.

  • kind ('GraphTypeLike | None') –

    Plot representation.

  • marker ('CanvasMarkerLike | None') –

    Glyph set used to paint the series.

Methods:

label class-attribute instance-attribute

label: str | None = None

Legend label.

color class-attribute instance-attribute

color: 'ColorLike | None' = None

Series color.

kind class-attribute instance-attribute

kind: 'GraphTypeLike | None' = None

Graph representation.

marker class-attribute instance-attribute

marker: 'CanvasMarkerLike | None' = None

Marker used to paint samples.

name class-attribute instance-attribute

name: str = dataclasses.field(default='', init=False)

Attribute name assigned by the component class.

resolve_label

resolve_label() -> str

Return the displayed legend label.

Source code in xnano/components/schema.py
def resolve_label(self) -> str:
    """Return the displayed legend label."""
    return self.label if self.label is not None else self.name