Django templates. Flask + Jinja. FastAPI + React. What if you just... wrote Python?
What Is FastHTML?
FastHTML lets you build complete web applications in pure Python. No templates. No JavaScript. No frontend build step. HTML elements are Python functions.
from fasthtml.common import *
app, rt = fast_app()
@rt("/")
def get():
return Titled("My App",
H1("Hello World!"),
P("This is a complete web page."),
Button("Click me", hx_get="/clicked", hx_target="#result"),
Div(id="result")
)
@rt("/clicked")
def get():
return P("You clicked the button!")
serve()
python app.py
# Running at http://localhost:5001
That's a complete interactive web app. In Python. With htmx-powered interactivity.
HTML as Python
# Every HTML element is a Python function
page = Html(
Head(Title("My Page")),
Body(
Nav(
A("Home", href="/"),
A("About", href="/about"),
),
Main(
H1("Welcome"),
Ul(
Li("Item 1"),
Li("Item 2"),
Li("Item 3"),
),
Form(
Input(name="email", type="email", placeholder="your@email.com"),
Button("Subscribe", type="submit"),
hx_post="/subscribe",
hx_target="#message"
),
Div(id="message")
)
)
)
Database Integration
app, rt, todos, Todo = fast_app(
'todos.db',
todo=str,
done=bool,
pk='id'
)
@rt("/")
def get():
items = todos()
return Titled("Todos",
Ul(*[Li(t.todo, " ✓" if t.done else "") for t in items]),
Form(
Input(name="todo", placeholder="New todo..."),
Button("Add"),
hx_post="/add", hx_target="ul", hx_swap="beforeend"
)
)
@rt("/add")
def post(todo: str):
new = todos.insert(Todo(todo=todo, done=False))
return Li(new.todo)
SQLite database, CRUD operations, real-time updates — all in 20 lines of Python.
Why FastHTML
- Pure Python — if you know Python, you can build web apps
- No JavaScript — htmx handles interactivity
- No templates — HTML is just Python function calls
- Built-in auth, DB, WebSockets — batteries included
- Fast prototyping — idea to deployed app in minutes
Building Python web apps? Check out my developer tools or email spinov001@gmail.com.
Top comments (0)