Add your description here
Project description
starlette-html
starlette-html is a Python-first HTML DSL for
Starlette that helps you build server-rendered UI with
plain Python functions.
It gives you a small, direct way to:
- build HTML in Python
- compose reusable components with normal functions
- pass data as regular function arguments
- render full documents or partial fragments
- return HTML responses directly from routes
- keep escaping safe by default
You might also like my other Starlette packages that I use and maintain:
starlette-hot-reload- a lightweight hot reload utility for Starlette that provides fast in-browser reloads for templates and static files.starlette-tailwindcss- a lightweight utility for Starlette that builds Tailwind CSS on startup with optional watch mode during development.
Installation
uv add starlette-html
# or
pip install starlette-html
Example
from starlette.applications import Starlette
from starlette.routing import Route
from starlette_html import Document, body, h1, head, html, p, title
async def load_user(request):
return {"name": "Ada", "email": "ada@example.com"}
def HomePage(*, user: dict):
return html(
head(title("Home")),
body(
h1(f"Hello {user['name']}"),
p(user["email"]),
),
)
async def homepage(request):
user = await load_user(request)
return Document(HomePage(user=user))
app = Starlette(
routes=[
Route("/", homepage),
]
)
Core idea
The library keeps the model simple.
- HTML tags are Python callables
- components are plain Python functions
- positional arguments become children
- keyword arguments become attributes
- lists and tuples are flattened
NoneandFalsechildren are ignored
Example:
from starlette_html import div, h1, p
def UserCard(*, name: str, email: str):
return div(
h1(name),
p(email),
cls="user-card",
)
Rendering
Use render when you want an HTML fragment.
from starlette_html import li, ul, render
html_fragment = render(
ul(
li("One"),
li("Two"),
)
)
Use render_document when you want a full HTML document.
from starlette_html import body, h1, head, html, render_document, title
page = render_document(
html(
head(title("Home")),
body(h1("Hello")),
)
)
Route helpers
starlette-html includes thin response helpers for direct route usage.
from starlette_html import Document, HTML, body, h1, head, html, title
async def homepage(request):
return Document(
html(
head(title("Home")),
body(h1("Hello")),
)
)
async def fragment(request):
return HTML(h1("Fragment"))
HTMX-friendly attributes
Normal HTML attributes work naturally, including HTMX attributes.
from starlette_html import button
button(
"Refresh",
hx_get="/items",
hx_target="#items",
hx_swap="outerHTML",
)
Safety
Dynamic text is escaped by default.
If you need to render trusted HTML, wrap it with Markup.
from starlette_html import Markup, div
div(Markup("<strong>Hello</strong>"))
License
MIT
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 starlette_html-0.1.0.tar.gz.
File metadata
- Download URL: starlette_html-0.1.0.tar.gz
- Upload date:
- Size: 4.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.11.3 {"installer":{"name":"uv","version":"0.11.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
af6b6a24b66a1c43be7f5b202b23612e163328d5a3a6325de07da196e5f619a7
|
|
| MD5 |
befccf262664cf1076586a24c9410f0d
|
|
| BLAKE2b-256 |
9364374d954b842c798d77aaf600992a899a7e1e2b53ace1abd2bedacf8fa756
|
File details
Details for the file starlette_html-0.1.0-py3-none-any.whl.
File metadata
- Download URL: starlette_html-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.11.3 {"installer":{"name":"uv","version":"0.11.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a835b9faac3753d2e8ccdf29fcce511eebbd5b7dcf6810a639d3453deb686951
|
|
| MD5 |
bac9566f4a1e568eef41125f77543365
|
|
| BLAKE2b-256 |
9fc1c2bc65fe2bbc8e1224cbf985686c7aa67088924a54b67f4a9eca62f87fc3
|