FastHTML

0.13.3 · active · verified Sat Apr 11

FastHTML is a lightweight and performant Python library designed for building dynamic HTML web applications with minimal code. It leverages ASGI for high performance and integrates well with technologies like HTMX for interactive user experiences. As of version 0.13.3, it is under active development with frequent minor releases.

Warnings

Install

Imports

Quickstart

This quickstart creates a simple FastHTML application that displays a clickable counter. It leverages HTMX to update the counter value on the page without a full page reload, demonstrating FastHTML's strength in building interactive UIs.

from fasthtml.common import *
from uvicorn import run

app = FastHTML()

# Global counter for demonstration
counter_val = 0

@app.get('/')
def home():
    return (
        Title('FastHTML Counter with HTMX'),
        H1('Click Counter'),
        P(
            'Count: ',
            A(
                counter_val, 
                id='count', 
                _hx_get='/count', 
                _hx_swap='outerHTML', 
                _hx_trigger='click'
            )
        )
    )

@app.get('/count')
def count_update():
    global counter_val
    counter_val += 1
    return A(
        counter_val, 
        id='count', 
        _hx_get='/count', 
        _hx_swap='outerHTML', 
        _hx_trigger='click'
    )

if __name__ == '__main__':
    # Run with uvicorn: uvicorn main:app --reload
    run(app, port=8000)

view raw JSON →