The fastest way to create HTML apps - a next-generation Python web framework for building fast, scalable web applications with minimal code
npx @tessl/cli install tessl/pypi-python-fasthtml@0.12.0A next-generation Python web framework for building fast, scalable web applications with minimal code. FastHTML provides a powerful and expressive API that maps directly to HTML and HTTP, allowing developers to create advanced interactive web applications using pure Python.
pip install python-fasthtmlfrom fasthtml.common import *This single import provides access to all core FastHTML functionality including:
For advanced usage, import specific modules:
from fasthtml.core import FastHTML, APIRouter, HtmxHeaders
from fasthtml.components import Div, P, H1, Form, Input
from fasthtml.xtend import AX, CheckboxX, Titled, Socials
from fasthtml.oauth import OAuth, GoogleAppClient, GitHubAppClient
from fasthtml.pico import Card, Grid, Container, picolink
from fasthtml.js import MarkdownJS, HighlightJS, SortableJS
from fasthtml.svg import Svg, Circle, Rect, Path, PathFT
from fasthtml.toaster import Toast, add_toast, setup_toasts
from fasthtml.jupyter import show, nb_serve, HTMXfrom fasthtml.common import *
# Create FastHTML app and router
app, rt = fast_app()
# Define routes using decorators
@rt('/')
def get():
return Div(
H1('Welcome to FastHTML'),
P('Hello World!', hx_get="/change")
)
@rt('/change')
def get():
return P('Content changed!', style="color: green;")
# Start development server
serve()FastHTML is built on modern web development principles:
The framework emphasizes simplicity and speed, offering a lightweight solution that leverages the full Python ecosystem while maintaining intuitive syntax for rapid development.
Core functionality for creating FastHTML applications, defining routes, and handling HTTP requests. Includes the main FastHTML class, routing decorators, and development server.
def fast_app(
db=None,
render=None,
hdrs=None,
ftrs=None,
tbls=None,
before=None,
middleware=None,
live=False,
debug=False,
routes=None,
exception_handlers=None,
on_startup=None,
on_shutdown=None,
lifespan=None,
default_hdrs=True,
pico=True,
surreal=True,
htmx=True,
ws_hdr=False,
secret_key=None,
key_fname='.sesskey',
session_cookie='session',
max_age=365*24*3600,
sess_path='/',
same_site='lax',
sess_https_only=False,
sess_domain=None,
htmlkw=None,
**bodykw
) -> tuple[FastHTML, RouteFuncs]:
"""
Create FastHTML app with sensible defaults and optional database integration.
Returns:
tuple: (FastHTML app instance, RouteFuncs for routing)
"""
def serve(
appname=None,
app='app',
host='0.0.0.0',
port=None,
reload=True,
reload_includes=None,
reload_excludes=None
):
"""Start development server with hot reload."""
class FastHTML:
"""Main FastHTML application class (extends Starlette)."""
class RouteFuncs:
"""Dynamic route function container for HTTP methods."""Complete HTML element generation system with Python functions for every HTML tag. Includes enhanced components with HTMX integration and form handling capabilities.
def Div(*c, **kw):
"""Create HTML div element."""
def P(*c, **kw):
"""Create HTML paragraph element."""
def H1(*c, **kw):
"""Create HTML h1 heading element."""
def Form(*c, **kw):
"""Create HTML form element with enhanced functionality."""
def Input(*c, **kw):
"""Create HTML input element."""
def Button(*c, **kw):
"""Create HTML button element."""Built-in HTMX support for creating dynamic, interactive web applications without writing JavaScript. Includes HTMX attributes, response handling, and event management.
class HtmxHeaders:
"""HTMX request headers dataclass."""
hx_request: str
hx_target: str
hx_trigger: str
hx_trigger_name: str
hx_current_url: str
def ft_hx(tag, *c, **kw):
"""Create HTML element with HTMX support."""Comprehensive form handling including data conversion, validation, file uploads, and dataclass integration.
def form2dict(form) -> dict:
"""Convert FormData to dictionary."""
def fill_form(form, obj):
"""Fill form with data object."""
def File(*c, **kw):
"""Handle file uploads."""OAuth integration with multiple providers, basic authentication middleware, and session management.
class OAuth:
"""Main OAuth handler class."""
class BasicAuthMiddleware:
"""HTTP Basic authentication middleware."""
class GoogleAppClient:
"""Google OAuth integration."""Built-in PicoCSS integration with enhanced components and styling utilities for rapid UI development.
def Card(*c, **kw):
"""PicoCSS card component."""
def Grid(*c, **kw):
"""PicoCSS grid container."""
def Container(*c, **kw):
"""PicoCSS container."""Integration with popular JavaScript libraries including Markdown rendering, syntax highlighting, and interactive components.
def MarkdownJS(*c, **kw):
"""Markdown parsing and rendering."""
def HighlightJS(*c, **kw):
"""Syntax highlighting."""
def SortableJS(*c, **kw):
"""Drag-and-drop sorting."""Comprehensive SVG element generation system for creating scalable vector graphics, charts, diagrams, and interactive visualizations.
def Svg(*args, viewBox=None, xmlns="http://www.w3.org/2000/svg", **kwargs):
"""SVG root element with automatic xmlns namespace."""
def Rect(width, height, x=0, y=0, fill=None, stroke=None, **kwargs):
"""SVG rectangle element."""
def Circle(r, cx=0, cy=0, fill=None, stroke=None, **kwargs):
"""SVG circle element."""
def Path(d='', fill=None, stroke=None, **kwargs):
"""SVG path element for complex shapes."""User notification system with toast messages, flash messages, and session-based notification management for enhanced user experience.
def Toast(message: str, typ: str = "info", dismiss: bool = False, duration: int = 5000):
"""Create toast notification message."""
def add_toast(sess, message: str, typ: str = "info", dismiss: bool = False):
"""Add toast notification to user session."""
def setup_toasts(app, duration: int = 5000):
"""Set up toast notification system in FastHTML app."""Development server with live reload, Jupyter notebook integration, testing utilities, and deployment tools.
def show(ft):
"""Display FastHTML elements in Jupyter."""
class Client:
"""HTTP client for testing FastHTML apps."""
def nb_serve():
"""Start Jupyter-compatible server."""