
Documentation: https://getludic.dev/docs/
Ludic lets you write HTML as typed Python components. Your type checker validates the markup, so wrong nesting, unknown attributes and missing arguments are caught before a page ever renders. At its core Ludic only renders HTML, so it fits into the web framework you already use, and it pairs naturally with htmx.
from typing import override
from ludic import Attrs, Component
from ludic.html import a, b, p
class LinkAttrs(Attrs):
to: str
class Link(Component[str, LinkAttrs]):
@override
def render(self) -> a:
return a(*self.children, href=self.attrs["to"])
p(t"Hello {b('Stranger')}! Click {Link('here', to='/home')}.").to_html()
# <p>Hello <b>Stranger</b>! Click <a href="/home">here</a>.</p>
Invalid markup is a type error. This is what mypy reports:
br("Hello, World!")
# error: Argument 1 to "br" has incompatible type "str"; expected "Never"
html(body())
# error: Argument 1 to "html" has incompatible type "body"; expected "head"
div("Test", href="test")
# error: Unexpected keyword argument "href" for "div"
Link("here", too="/home")
# error: Unexpected keyword argument "too" for "Link"; did you mean "to"?
Link(div("here"), to="/home")
# error: Argument 1 to "Link" has incompatible type "div"; expected "str"
Link("here")
# error: Missing named argument "to" for "Link"
"I've just composed my first PageLayout component and I have no words!"
– Igor Davydenko
Features
- Type-guided HTML: every element knows which children and attributes it allows
- Components in pure Python, composed like in React
- Mix markup and text with Python 3.14 t-strings (template strings)
- Text is escaped by default, trusted markup is marked explicitly with
Safe
- Typed </> htmx attributes, so dynamic pages need almost no JavaScript
- Works with Starlette, FastAPI and Django, or with no framework at all
- Typed CSS for your components with Themes
- Simple, responsive layouts adopted from the Every Layout Book
Installation
Ludic 1.x requires Python 3.14+ for t-string support. If you are on Python 3.12 or 3.13, use Ludic 0.5.x, which uses f-strings instead.
The core package renders HTML and has a single dependency:
pip install ludic
To also get the Starlette based web framework, form parsing and code highlighting:
pip install "ludic[full]"
You can also use a basic cookiecutter template to get started quickly. With UV, you need to run only one command:
uvx cookiecutter gh:getludic/template
Type Checking
Elements from ludic.html are checked by plain type hints, so mypy and pyright (and Pylance in VS Code) report the errors above, except those about component attributes. To also check the attributes of your own components, enable the mypy plugin in pyproject.toml:
[tool.mypy]
plugins = ["ludic.mypy_plugin"]
mypy supports t-strings since version 2.0. Older versions report every t-string as an error.
Use It With Your Framework
Ludic does not enforce any frontend or backend framework. Pick the integration that matches your stack:
Starlette – LudicApp is a thin wrapper around Starlette, handlers return elements directly:
from ludic.html import b, p
from ludic.web import LudicApp
app = LudicApp()
@app.get("/")
async def homepage() -> p:
return p(t"Hello {b('Stranger')}!")
FastAPI – set the route class and return elements from your endpoints:
from fastapi import FastAPI
from ludic.contrib.fastapi import LudicRoute
from ludic.html import p
app = FastAPI()
app.router.route_class = LudicRoute
@app.get("/")
async def homepage() -> p:
return p("Hello, World!")
Django – wrap the element in a response:
from django.http import HttpRequest
from ludic.contrib.django import LudicResponse
from ludic.html import p
def index(request: HttpRequest) -> LudicResponse:
return LudicResponse(p("Hello, World!"))