Ludic 1.0: t-strings for Python 3.14 →

Typed HTML components in pure Python

Write HTML as Python components. Your type checker validates the markup, so wrong nesting, unknown attributes and missing arguments fail before a page ever renders.

Get startedStar on GitHub
pip install ludic
components.py
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='/docs/')}.")

Rendered by this page

Hello Stranger! Click here.

HTML
<p>Hello <b>Stranger</b>! Click <a href="/docs/">here</a>.</p>

Type checking

Invalid markup is a type error

Elements and components declare the children and attributes they accept. The mypy plugin checks every call, so mistakes show up in your editor and in CI instead of in the browser.

app.py
br("Hello, World!")
html(body())
div("Test", href="test")
Link("here", too="/home")
Link(div("here"), to="/home")
Link("here")
$ mypy app.py
app.py:1: error: Argument 1 to "br" has incompatible type "str"; expected "Never"
app.py:2: error: Argument 1 to "html" has incompatible type "body"; expected "head"
app.py:3: error: Unexpected keyword argument "href" for "div"
app.py:4: error: Unexpected keyword argument "too" for "Link"; did you mean "to"?
app.py:5: error: Argument 1 to "Link" has incompatible type "div"; expected "str"
app.py:6: error: Missing named argument "to" for "Link"
Found 6 errors in 1 file (checked 1 source file)

Features

Everything you need to render HTML, nothing more

At its core Ludic only renders HTML, so it fits into the web framework you already use.

Type-guided HTML

Every element knows which children and attributes it allows, so mypy catches invalid markup.

Components

Compose pages from small Python classes, like in React, with typed children and attributes.

t-strings

Mix text and elements with Python 3.14 template strings, no template language needed.

Escaped by default

Text is always escaped. Trusted markup is marked explicitly with Safe.

htmx attributes

Typed hx-* attributes make dynamic pages with almost no JavaScript.

Any framework

Use the Starlette based web framework, FastAPI, Django, or just call .to_html().

Themes

Components declare their CSS and read colors and sizes from the theme. This site's dark mode is a second theme.

Layouts

Responsive layouts adopted from the Every Layout book, ready in the catalog.

Integrations

Use it with your framework

Return elements from Starlette, FastAPI or Django views, or render a string with .to_html() anywhere else.

app.py
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')}!")
main.py
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!")
views.py
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!"))
render.py
from ludic.html import b, p

html = p(t"Hello {b('World')}!").to_html()
# '<p>Hello <b>World</b>!</p>'

htmx

Interactive pages without writing JavaScript

Endpoints render themselves as components, and typed hx-* attributes wire them together.

counter.py
class Counter(Endpoint[CounterAttrs]):
    @classmethod
    def get(cls, number: int) -> Self:
        return cls(number=number)

    @override
    def render(self) -> Cluster:
        number = self.attrs["number"]
        return Cluster(
            ButtonDanger(
                "Decrement",
                disabled=number <= 0,
                hx_get=self.url_for(Counter, number=number - 1).path,
                hx_target="#counter",
            ),
            b(number),
            ButtonSuccess(
                "Increment",
                hx_get=self.url_for(Counter, number=number + 1).path,
                hx_target="#counter",
            ),
            id="counter",
        )
0

This counter is the endpoint on the left. Each click asks the server for the new HTML and htmx swaps it in.

AI tooling

Built for coding agents

Most code is now written with an AI assistant. Ludic ships instructions for agents, so they write type-safe Ludic code and check it with mypy.

Agent skills

Teach any coding agent to write idiomatic Ludic code.

npx skills add https://github.com/getludic/ludic --skill '*'

Claude Code plugin

Install both skills from the Ludic plugin marketplace.

/plugin marketplace add getludic/ludic
/plugin install ludic@ludic

Context7

Agents that look up docs with Context7 get the Ludic docs and rules from /getludic/ludic.

Open on Context7 →

llms.txt

Every page of this site has a Markdown version, listed in llms.txt, or all in one file.

llms.txtllms-full.txt

From the community

Loved by Python developers

“I've just composed my first PageLayout component and I have no words!”

– Igor Davydenko

Get started

Build your next page with Ludic

Install it with pip install ludic and follow the guide.

Read the docsBrowse the catalogSee examples