Metadata-Version: 2.4
Name: tealui
Version: 0.3.1
Summary: A tiny, clean, pythonic UI library that renders plain HTML with Tailwind and Alpine.
Author: octalynx
License-Expression: MIT
Keywords: html,ui,tailwind,alpine,components,themes,flask,fastapi,django
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Topic :: Internet :: WWW/HTTP :: Dynamic Content
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Typing :: Typed
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Dynamic: license-file

# 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

```bash
pip install tealui
```

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

Python 3.9+. No dependencies.

## Hello world

```python
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.

```bash
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:

```python
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 |

```python
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.

```python
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.

```python
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.

```python
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:

```python
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>`:

```python
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:

```python
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:

```python
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

```python
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 `#`.
