Skip to main content

TealUI

Write a web UI in Python. The output is plain HTML, styled with Tailwind CSS and made interactive with Alpine.js. It works with any backend, because everything renders to a string.

Install

pip install tealui

To work on TealUI itself, install a checkout instead: pip install -e .

Python 3.9+. No dependencies.

Hello world

from tealui import Page, Card, Stat, Button, Div, H1

page = Page(
    "Hello, TealUI",
    Div(
        H1("Hello, TealUI", cls="text-2xl font-semibold tracking-tight"),
        Stat("Users", "1,204"),
        Card(Button("Get started")),
        cls="mx-auto max-w-xl space-y-6 p-8",
    ),
)

with open("index.html", "w", encoding="utf-8") as f:
    f.write(str(page))
print("Wrote index.html. Open it in a browser.")

Plain tags such as H1 have no default styling (Tailwind resets them). Style them with cls=, or use components such as PageHeader and Prose.

With no css= argument, TealUI uses the Tailwind CDN build, so this page needs the internet.

Production CSS

Tailwind only generates the classes it finds in scanned files, so give --src every folder where you write your own class strings (in Python or HTML); repeat --src for several folders. Without --src the current folder is scanned. Only the folders you name are scanned, nothing else.

tealui build static/tealui.css --src app/

The first run downloads the standalone Tailwind CLI (about 100 MB, checksum verified) into ~/.cache/tealui (needs internet once); later runs work offline. TEALUI_CACHE or --cache moves the cache. A fonts/ folder is copied next to the CSS file — keep it there. Installed look packages are included automatically; add a look that lives in your project with --look looks.py:OCEAN. --alpine static/alpine.js self-hosts Alpine (then Page(alpine="/static/alpine.js")); --htmx static/htmx.js does the same for HTMX (then Page(htmx="/static/htmx.js")). tealui css only writes the input file and fonts, for people who run their own Tailwind setup.

Then link the stylesheet:

from tealui import Page, Div

page = Page("Home", Div("hello"), css="/static/tealui.css")

Versions

TealUI follows the latest Alpine 3.x, HTMX 2.x and Tailwind 4.x. Dev mode loads them from jsdelivr by major version. A new major version is adopted only after testing. tealui update refreshes the cached tools, tealui versions shows installed vs latest.

Looks

A look is the theme: colors, radii, shadows and fonts, in light and dark, plus the class recipes components use. Fourteen are built in:

Look Kind Character
TEAL default Product look, teal accent
PAPER full Editorial: serif headings, warm paper, rust accent
SLATE full Cool grey, graphite accent
MIDNIGHT full, dark-first Blue-black with an indigo glow
LEDGER full Navy, dense and flat (32px controls, 40px rows)
CONTRAST full Black and white, WCAG AAA (7:1)
NORDIC full Arctic blue-grey, muted status colors
SOFT full Pastel lilac, large radii
TERMINAL full, dark-first Monospace, green on black, square
BRUTAL full Neo-brutalist: black borders, hard shadows
INDIGO, ENTERPRISE, FOREST, SUNSET accent swap TEAL with an indigo, corporate blue, deep green or rose accent
from tealui import use, using, with_accent, TEAL, PAPER, SLATE, Page, Div

use(SLATE)                          # change the default look
use(with_accent(TEAL, "#7c3aed"))   # or derive a custom accent from one hex

with using(PAPER):                  # or only for one render
    html = str(Page("Report", Div("hello")))

The dark-mode choice is saved in the browser (localStorage). Dark-first looks start dark until the visitor picks light. tealui looks lists every look; tealui look preview slate gallery.html writes a page with every component in a look.

Your own look

make_look copies TEAL (or base=) with your changes and checks the result: every variable and recipe present, no unsafe values, and contrast (4.5:1 by default) in light and dark.

from tealui import make_look, use

OCEAN = make_look(
    "ocean",
    vars={"accent": "#0e7490", "accent-hover": "#155e75", "accent-ink": "#ffffff",
          "accent-soft": "#e0f5f9", "accent-line": "#a5dbe6", "r-card": "16px"},
    dark={"accent": "#67e8f9", "accent-hover": "#a5f3fc", "accent-ink": "#062a30",
          "accent-soft": "#0c2c33", "accent-line": "#1c5a66"},
    recipes={"button": {"md": "h-10 px-4"}},
)
use(OCEAN)
print(OCEAN["name"])

tealui look new ocean writes a starter file, tealui look check ocean.py:OCEAN checks it, and --package adds a pyproject.toml to publish it as tealui-look-ocean. Installed look packages are found by tealui build automatically; a look kept in your project is added with tealui build ... --look looks.py:OCEAN. The full guide is docs/looks.md in the source distribution.

Fonts and icons

Override a look's fonts with with_fonts, then register the actual font file with a <style> tag in the head.

from tealui import use, with_fonts, TEAL, Page, Div, Style, Raw

use(with_fonts(TEAL, head='"MyFont", serif', body='"MyFont", sans-serif'))
page = Page("Font demo", Div("hello"),
            head=[Style(Raw('@font-face{font-family:"MyFont";src:url("/static/myfont.woff2") format("woff2")}'))])
print(len(str(page)))

Add your own icons with register_icon; the SVG markup is trusted and rendered as-is.

from tealui import register_icon, Icon

register_icon("spark", '<path d="M12 2l2.4 6.6L21 11l-6.6 2.4L12 20l-2.4-6.6L3 11l6.6-2.4z"/>')
print(Icon("spark"))

Interactivity

Pages for right-to-left languages mirror by themselves: Page(..., lang="ar") sets dir="rtl", and every look uses logical classes (ms-, ps-, start-). Server-rendered fragments work with HTMX: any hx_* keyword becomes an hx-* attribute (Button("Load", hx_get="/rows", hx_target="#rows")) and Page(..., htmx=True) loads the library.

Components are Alpine-powered, and you can always write your own JS:

from tealui import Page, Div, Span, Button

page = Page(
    "Counter",
    Div(
        Span(x_text="n"),
        Button("+1", on_click="n++"),
        x_data="counter",
    ),
    js="Alpine.data('counter', () => ({ n: 0 }))",
)

Add extra tags to the <head>:

from tealui import Page, Div, Link, Script

page = Page(
    "Head example",
    Div("hi"),
    head=[Link(rel="icon", href="/favicon.ico"),
          Script(src="/static/extra.js", defer=True)],
)

Escape hatches

Any tag, any attribute:

from tealui import Div, El, Raw

print(Div(El("custom-thing", "hello", any_attr="x")))
print(Div(Raw("<svg viewBox='0 0 10 10'><circle cx='5' cy='5' r='4'/></svg>")))

Attribute naming rule: cls becomes class; _ becomes -; on_x becomes @x; bind_x becomes :x; __ becomes .. For example on_click -> @click, bind_class -> :class, x_model__number -> x-model.number. Text is escaped by default; only Raw is trusted.

Components

  • Layout: Page, Shell, Sidebar, Topbar, SiteHeader, PageHeader, Card, Prose, DocsNav, Toc, ReadingProgress
  • Data: Stat, Delta, Table, BarList, Sparkline, AreaChart, Progress, Badge, Avatar, Kbd
  • Forms: Button, TextInput, TextBox, SelectBox, Checkbox, Switch, RadioGroup, Field, Segmented, SearchBox, TagFilter, tag_show
  • Overlays & feedback: Modal, Dropdown, Toaster, toast_js, Callout, Tabs
  • Blog & nav: PostCard, Pager, ArticleHeader, ShareBar, Breadcrumb, NavItem, DarkToggle, Icon

A tag filter needs one wrapper that holds the x-data for both the filter and the posts:

from tealui import Div, TagFilter, PostCard, tag_show

Div(
    TagFilter(["python", "css"]),
    PostCard("A", href="/a", tag="python", x_show=tag_show("python")),
    PostCard("B", href="/b", tag="css", x_show=tag_show("css")),
    x_data="{ tag: '' }",
)

Rules of the road

  • cls= is added last, so your classes win.
  • Extra keyword arguments land on the root element (on the control itself for form controls).
  • The tag helpers Input, Select, Textarea, Label, Option and Form already exist, so the components are named TextInput, SelectBox, and so on.

Serve it

from http.server import BaseHTTPRequestHandler, HTTPServer
from tealui import Page, Div, H1

class Handler(BaseHTTPRequestHandler):
    def do_GET(self):
        body = str(Page("Home", Div(H1("Served by stdlib"), cls="p-8")))
        data = body.encode("utf-8")
        self.send_response(200)
        self.send_header("Content-Type", "text/html; charset=utf-8")
        self.send_header("Content-Length", str(len(data)))
        self.end_headers()
        self.wfile.write(data)

HTTPServer(("127.0.0.1", 8000), Handler).serve_forever()

Any framework works the same way: return str(page) as the response body.

Developing TealUI

tealui test runs the unit tests (--browser also runs the browser compatibility check and the accessibility and device suite), tealui export tests, builds the wheel and sdist into dist/ and writes the minimal source zip (needs pip install build).

License

MIT. The bundled fonts and icons keep their own licenses (see tealui/fonts/LICENSES.txt and tealui/icons-LICENSE.txt).

Trust boundaries

Text and attribute values are escaped by default. Raw is for trusted markup only. The values of x_*, on_*, and bind_* attributes are JavaScript expressions that run in the visitor's browser; escaping them as HTML does not make them safe, so never build them from untrusted text. bind_href and bind_src bypass the server-side URL safety check, so validate those destinations yourself before passing them in. Plain HTML event attributes (onclick=, onload=...) and srcdoc= are the same: they run what they contain. Address attributes (href, src, action, formaction, poster, data, background, and the SVG animation values to/from/by/values) are checked, and a javascript:, vbscript: or non-image data: address becomes #.

Metadata

Release files for tealui 0.3.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tealui 0.3.1
File Size Uploaded Provenance Actions
tealui-0.3.1.tar.gz 300.7 kB Without provenance Details for tealui-0.3.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for tealui 0.3.1
File Interpreter ABI Platform Actions
tealui-0.3.1-py3-none-any.whl Python 3 none any Details for tealui-0.3.1-py3-none-any.whl

Total release size: 563.3 kB

Release files / tealui-0.3.1.tar.gz

Download URL tealui-0.3.1.tar.gz
Size 300.7 kB
Tags Source
SHA-256 checksum
How to use checksums
0ea140fc78e5d88f4a107bfe34b77160427a0f5399fd74ec00bc2546ac7321c5
BLAKE2b-256 checksum
How to use checksums
465ad6a49ce9eb5016a5fd84aa8e3a431a3c13e0006c102732424b2e48e2cfa4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.10

Release files / tealui-0.3.1-py3-none-any.whl

Download URL tealui-0.3.1-py3-none-any.whl
Size 262.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
18210d2b44e417ca0c7699908a3d6b90c97c8d16b5c526e5dfc9ebd9ef1453f7
BLAKE2b-256 checksum
How to use checksums
bd73011268fa9b0cd1ebb33f17255a4696d7da593908336bad0adeeca001b7c6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.10

Security for tealui 0.3.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page