Skip to main content

Type-safe, composable UI components for Python — server-side rendered, HTMX-first.

Project description

PyPI version Python 3.11+ License: MIT mypy strict

htmforge

Type-safe, composable UI components for Python — server-side rendered, HTMX-first, framework-agnostic.

Why htmforge?

  • No templates — build HTML with plain Python classes and type-checked props
  • HTMX-native — typed enums for every hx-* attribute, zero string guessing
  • Pydantic validation — props are validated on construction and assignment
  • Framework-agnostic — works with FastAPI, Flask, Django, or any WSGI/ASGI app

Installation

pip install htmforge

Quickstart

A FastAPI route that renders a full page with a user table and an add-user form:

from fastapi import FastAPI
from fastapi.responses import HTMLResponse

from htmforge.components.page import Page
from htmforge.components import DataTable, FormField, InputType
from htmforge.core.element import Element
from htmforge.elements import div, h1

app = FastAPI()

USERS = [["Ada Lovelace", "ada@example.com"], ["Grace Hopper", "grace@example.com"]]


class UsersPage(Page):
    users: list[list[str]]
    added: bool = False

    def _body_content(self) -> list[Element | str | None]:
        return [
            div(
                h1("Users"),
                DataTable(
                    headers=["Name", "Email"],
                    rows=self.users,
                ),
                FormField(
                    name="name",
                    label_text="Full Name",
                    input_type=InputType.TEXT,
                    required=True,
                ),
            )
        ]


@app.get("/users", response_class=HTMLResponse)
def list_users() -> str:
    return UsersPage(
        title="Users",
        css_urls=["/static/style.css"],
        users=USERS,
    ).to_html()

Components

Component Description Import
DataTable Table with optional HTMX reload from htmforge.components import DataTable
Alert Info/success/warning/error box, dismissible from htmforge.components import Alert
Pagination Previous/Next + page links with HTMX targeting from htmforge.components import Pagination
Page Full HTML document with DOCTYPE, abstract base from htmforge.components.page import Page
FormField Label + input + error div, 8 input types from htmforge.components import FormField

HTMX Integration

All HTMX attributes are available as typed enums — no string typos:

from htmforge.elements import button
from htmforge.htmx import HxSwap, HxTarget, HxTrigger

btn = button(
    "Delete",
    hx_delete="/users/1",
    hx_swap=HxSwap.OUTER_HTML,
    hx_target=HxTarget.CLOSEST_TR,
    hx_confirm="Really delete?",
)
# → <button hx-delete="/users/1" hx-swap="outerHTML"
#           hx-target="closest tr" hx-confirm="Really delete?">Delete</button>

Framework Support

FastAPI

from fastapi.responses import HTMLResponse

@app.get("/", response_class=HTMLResponse)
def index() -> str:
    return MyPage(title="Home").to_html()

Flask

from flask import Response

@app.route("/")
def index() -> Response:
    return MyPage(title="Home").to_flask()

Django

from django.http import HttpRequest, HttpResponse

def index(request: HttpRequest) -> HttpResponse:
    return MyPage(title="Home").to_django()

Contributing

Contributions are welcome — please read CONTRIBUTING.md first.

License

MIT — see LICENSE.

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

htmforge-0.1.2.tar.gz (2.7 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

htmforge-0.1.2-py3-none-any.whl (19.3 kB view details)

Uploaded Python 3

File details

Details for the file htmforge-0.1.2.tar.gz.

File metadata

  • Download URL: htmforge-0.1.2.tar.gz
  • Upload date:
  • Size: 2.7 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.0

File hashes

Hashes for htmforge-0.1.2.tar.gz
Algorithm Hash digest
SHA256 889999b4beeae985dd8026a20b79541b3303d3b826fee488f7b18c41da6cdfa4
MD5 e1ba5a6769781e64aeb1a66121249757
BLAKE2b-256 81d42b9bb700da0d76dbb95d54988c06e670f8f4d9301b48d96a352a157b0464

See more details on using hashes here.

File details

Details for the file htmforge-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: htmforge-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 19.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.0

File hashes

Hashes for htmforge-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 965696fb4c92d4ca5c3a859b13077e8af1d924aad26955c17978e358704f75ef
MD5 c99ea00aac06535e02ed7c092bf7b8b6
BLAKE2b-256 8717b1f0ee3bcc3cea6bbb01015b8d14773f4e0c77128ac0a2b15cc6f7c3035f

See more details on using hashes here.

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