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.2.0.tar.gz (15.5 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.2.0-py3-none-any.whl (14.4 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for astralprims-0.2.0.tar.gz
Algorithm Hash digest
SHA256 a5355fd7eccbae31c3490a5674c8f89cbc5c39509c3298049c576deeaa3c9b01
MD5 6bc6292a20e11dceb91f1ec1f6520316
BLAKE2b-256 ec00a2a5c716fe752da2be1ede74eb8c1c1655bf8e88f00d0584b4d1d37e5733

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: astralprims-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 14.4 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.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0a1d762bb6310b4a28eac244a240eff01640a902acf85db6748ec6af8b323a56
MD5 b24472496a425b115243979f552bb230
BLAKE2b-256 2d2f2c8a6c34a9b51e6bc40a0cb594b662e4b75735f4b150cc088970ed68d658

See more details on using hashes here.

Provenance

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