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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0242754ff315bbce309d1107dc7a2a2ebf08b63d19f70cb8af86c7f25ed04639
|
|
| MD5 |
31a0e5fc9217be10ac8f226a02ca9ff2
|
|
| BLAKE2b-256 |
c15bbf2fb11c26aaebb1feb2f36d350d35582e5c569faa8b4015045cdfc3380c
|
Provenance
The following attestation bundles were made for astralprims-0.1.0.tar.gz:
Publisher:
python-publish.yml on AstralDeep/Astral-Primitives
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
astralprims-0.1.0.tar.gz -
Subject digest:
0242754ff315bbce309d1107dc7a2a2ebf08b63d19f70cb8af86c7f25ed04639 - Sigstore transparency entry: 1670817896
- Sigstore integration time:
-
Permalink:
AstralDeep/Astral-Primitives@b1f7aa1662bd86f89222867f33c834816461deb9 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/AstralDeep
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@b1f7aa1662bd86f89222867f33c834816461deb9 -
Trigger Event:
push
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bb9ed82b8768a05a87ae051a43f47e45f768fa34ae4368f9ad82f094ea84dbe6
|
|
| MD5 |
68ee4a414fc3336e4830055b2ff47c92
|
|
| BLAKE2b-256 |
0b2c920878ce35929396f31201d4095fae05d4d35728a32d9c6113e06701713d
|
Provenance
The following attestation bundles were made for astralprims-0.1.0-py3-none-any.whl:
Publisher:
python-publish.yml on AstralDeep/Astral-Primitives
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
astralprims-0.1.0-py3-none-any.whl -
Subject digest:
bb9ed82b8768a05a87ae051a43f47e45f768fa34ae4368f9ad82f094ea84dbe6 - Sigstore transparency entry: 1670818022
- Sigstore integration time:
-
Permalink:
AstralDeep/Astral-Primitives@b1f7aa1662bd86f89222867f33c834816461deb9 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/AstralDeep
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@b1f7aa1662bd86f89222867f33c834816461deb9 -
Trigger Event:
push
-
Statement type: