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
Dashboard Badge, Hero, KeyValue, Timeline, Rating
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

Primitives are pydantic models; declare the wire type as a Literal default and subclassing auto-registers it for from_dict — no manual map. Pick a type string that does not collide with a built-in:

from typing import Literal, Optional
from astralprims import Primitive, rebuild_primitive_union

class Ribbon(Primitive):
    type: Literal["ribbon"] = "ribbon"   # registered automatically
    label: str = ""
    count: Optional[int] = None

rebuild_primitive_union()  # refresh AnyPrimitive/primitive_adapter

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.3.0.tar.gz (16.1 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.3.0-py3-none-any.whl (14.8 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for astralprims-0.3.0.tar.gz
Algorithm Hash digest
SHA256 9c20fc086a284c15c170474a0f6a296b23123624e1acc724002dd731d7974246
MD5 e575d357b73827f66e2e94547a60ec26
BLAKE2b-256 c9eb945c0dd2f768a2e1764a9ae2920d9b07c74eb91934f29b2ec012054b0066

See more details on using hashes here.

Provenance

The following attestation bundles were made for astralprims-0.3.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.3.0-py3-none-any.whl.

File metadata

  • Download URL: astralprims-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 14.8 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.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9272b404205244b32391f32c3f7009d3c653fd97deed7ff2b01c8ebeaa4d8d7d
MD5 ae0cc077f5a75e9570ca819c260f8880
BLAKE2b-256 ba8683a16811b4750d1086a3db2822aee89446606c508c6b062b8127ab4e6021

See more details on using hashes here.

Provenance

The following attestation bundles were made for astralprims-0.3.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