Skip to main content

Composable, serializable UI primitives for Python

Project description

Astral Primitives

Composable, serializable UI primitives for Python. Describe UI as plain Python objects, then serialize them to a dict/JSON for storage or for a server-driven UI to render.

Install

pip install -e ".[dev]"   # editable install with test deps

Quick start

from astralprims import Button

button = Button(
    label="the button text",
    action="open",
    css={"background-color": "white", "color": "#000000"},
)

button.to_dict()
# {
#   "type": "button",
#   "css": {"background-color": "white", "color": "#000000"},
#   "label": "the button text",
#   "action": "open",
#   "payload": {},
#   "variant": "primary",
# }

button.to_json()  # -> JSON string

None and empty css are dropped from the output, so payloads stay clean.

Composing layouts

from astralprims import Card, Container, Text, Button

page = Container(css={"display": "flex"}, direction="column").add(
    Text(content="Welcome", variant="h1"),
    Card(title="Sign up").add(
        Text(content="Enter your details below."),
        Button(label="Get started", action="signup"),
    ),
)
page.to_dict()  # children/content serialize recursively

Round-tripping from data

Already have a primitive as a dict (from storage or an API)? Rebuild it — including the full nested tree:

from astralprims import Primitive

spec = {"type": "button", "label": "Buy", "action": "checkout"}
button = Primitive.from_dict(spec)   # -> Button(...)

Shipping a response

from astralprims import create_ui_response, Text, Button

create_ui_response([Text(content="hi"), Button(label="ok", action="go")])
# {"_ui_components": [{...}, {...}], "_data": None}

A FastAPI endpoint can return primitive.to_dict() or create_ui_response(...) directly.

Built-in primitives

Group Primitives
Layout Container, Card, Grid/Grids, Tabs (+ TabItem), Collapsible, Divider
Content Text, Button, Input, ParamPicker, Image, CodeBlock, Alert, ProgressBar, MetricCard, List_, Table
Charts BarChart, LineChart, PieChart, PlotlyChart (+ ChartDataset)
Media/IO Audio, FileUpload, FileDownload
Theming ColorPicker, ThemeApply

Every primitive also accepts css, id, class_name (serialized as class), tooltip, and an attributes dict for arbitrary extra keys.

Defining your own primitive

Subclassing auto-registers the new type for from_dict — no manual map:

from dataclasses import dataclass
from typing import ClassVar, Optional
from astralprims import Primitive

@dataclass
class Badge(Primitive):
    type: ClassVar[str] = "badge"   # registered automatically
    label: str = ""
    count: Optional[int] = None

Tests

pytest

License

Apache-2.0

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

astralprims-0.1.0.tar.gz (14.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

astralprims-0.1.0-py3-none-any.whl (13.7 kB view details)

Uploaded Python 3

File details

Details for the file astralprims-0.1.0.tar.gz.

File metadata

  • Download URL: astralprims-0.1.0.tar.gz
  • Upload date:
  • Size: 14.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for astralprims-0.1.0.tar.gz
Algorithm Hash digest
SHA256 0242754ff315bbce309d1107dc7a2a2ebf08b63d19f70cb8af86c7f25ed04639
MD5 31a0e5fc9217be10ac8f226a02ca9ff2
BLAKE2b-256 c15bbf2fb11c26aaebb1feb2f36d350d35582e5c569faa8b4015045cdfc3380c

See more details on using hashes here.

Provenance

The following attestation bundles were made for astralprims-0.1.0.tar.gz:

Publisher: python-publish.yml on AstralDeep/Astral-Primitives

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file astralprims-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: astralprims-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 13.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for astralprims-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bb9ed82b8768a05a87ae051a43f47e45f768fa34ae4368f9ad82f094ea84dbe6
MD5 68ee4a414fc3336e4830055b2ff47c92
BLAKE2b-256 0b2c920878ce35929396f31201d4095fae05d4d35728a32d9c6113e06701713d

See more details on using hashes here.

Provenance

The following attestation bundles were made for astralprims-0.1.0-py3-none-any.whl:

Publisher: python-publish.yml on AstralDeep/Astral-Primitives

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page