Skip to main content

🏷️ Tagflow

Tagflow is a Python library that generates HTML in a block-oriented way that works with ordinary control flow. Instead of nested function calls, it uses context managers and decorators to align HTML generation with Python's native language features.

So you can write HTML using familiar Python constructs: loops for repeated elements, conditionals for dynamic content, and try/except for error boundaries. Create reusable components with decorators and organize code into reusable components that yield to produce child content.

Most Python HTML generation libraries follow a pattern of nested call expressions to mirror HTML's tree structure, using lists, list comprehensions, and variable arguments to pass children. This forces us to abandon familiar control flow patterns (if, for, continue, try) in favor of functional-style composition, leading to convoluted code when dealing with conditional rendering, loops, or error handling.

Tagflow takes a different approach by using context managers to track the current position in the HTML tree, allowing developers to use ordinary Python control flow while building HTML documents step by step in nested with blocks.

We have not yet measured the performance of Tagflow, but it would be interesting to see how it compares to other Python HTML generation libraries. It's not optimized for speed but we haven't seen any major performance issues.

The current implementation uses Python's builtin ElementTree for maintaining the document tree and serializing it to HTML.

While streaming responses aren't implemented yet, Tagflow's design will support generating and sending HTML incrementally without buffering entire pages in memory.

Installation

Tagflow is available on PyPI with the name tagflow.

uv add tagflow

Usage

from tagflow import tag, text, document, attr

def page(title: str):
  # open a <html> tag in the current document
  with tag.html(lang="en"):
      with tag.head():
          with tag.title():
              # append a text node to the title tag
              text(title)
          with tag.script(src="https://cdn.tailwindcss.com"):
              pass # no content in the script tag

      # use "classes" to avoid conflict with the "class" keyword
      with tag.body(classes="bg-gray-50 p-4"):
          with tag.h1():
              text("Welcome!")
          # positional arguments are also used as class names
          with tag.p("serif", "mb-4"):
              # another way to set attributes
              attr("contenteditable")
              attr("spellcheck", False)
              # classes can also be a nested list of strings
              attr("class", ["text-lg text-gray-900", ["font-bold", "italic"]])
              # after emitting content you can't change the attributes
              text("This is a paragraph.")

def index_html() -> str:
  # document() is a context manager that creates a new document root
  with document() as root:
    page("Tagflow")
    return root.to_html()

You can use decorators to specify the tag structure for a function. This is a nice way to define reusable components.

from tagflow import tag, text, html

# decorator nesting lets us skip indentation for basic structure
@html.main(lang="en")
@html.article("w-prose mx-auto", ["font-bold", "italic"], data_role="content")
def welcome(name: str):
    with tag.h1():
        text(f"Welcome, {name}!")
    with tag.p():
        text("This is a paragraph.")

To define a component that takes children, just define a context manager. Here we also show some control flow examples.

from tagflow import tag, text, html
from dataclasses import dataclass
from contextlib import contextmanager

@contextmanager
def page(title: str):
    with tag.html(lang="en"):
        with tag.head():
            with tag.title():
                text(title)
            with tag.script(src="https://cdn.tailwindcss.com"):
                pass

        with tag.body("bg-gray-50 p-4"):
            yield

@contextmanager
def article():
    with tag.article("w-prose mx-auto"):
        yield

@dataclass
class Post:
    id: str
    title: str
    content: list[str]

def index(posts: list[Post]):
    with page("Posts"):
        # just use a loop; no special iteration feature
        for post in posts:
            # just use if; no special blank feature
            if not post.content:
                continue

            # just use try; no special error boundary feature
            try:
                render_post(post)
            except ValueError as e:
                with tag.p("text-red-500"):
                    text("Error rendering post: ")
                    text(str(e))

@html.article()
def render_post(post: Post):
    if not post.id:
        raise ValueError("Post ID is required")

    attr("id", post.id)
    with tag.h1():
        text(post.title)

    for block in post.content:
        with tag.p("mb-4"):
            text(block)

FastAPI response class

Tagflow provides a custom FastAPI response class and middleware that make it easy to integrate with FastAPI endpoints. The middleware automatically sets up a fresh document context for each request, while the response class handles rendering:

from fastapi import FastAPI
from tagflow import tag, text, DocumentMiddleware, TagResponse

app = FastAPI()
app.add_middleware(DocumentMiddleware)

@app.get("/", response_class=TagResponse)
def home():
    with tag.html(lang="en"):
        with tag.head():
            with tag.title():
                text("Home")
        with tag.body():
            with tag.h1():
                text("Welcome!")

# Works with async endpoints too
@app.get("/posts/{id}", response_class=TagResponse)
async def view_post(id: str):
    post = await get_post(id)
    with tag.html():
        with tag.body():
            render_post(post)

You can also set the FastAPI default response class to TagResponse in your FastAPI app.

from fastapi import FastAPI
from tagflow import TagResponse

app = FastAPI(default_response_class=TagResponse)

@app.get("/")
def home():
    with tag.html():
        with tag.body():
            with tag.h1():
                text("Welcome!")

Live Documents (early working prototype)

Tagflow also offers "live documents" that asynchronous server tasks can update in real time after the initial page load. It works a bit like Phoenix LiveView: the browser runs a script that exposes a DOM mutation capability via WebSocket, letting the server send updates to elements in the document.

Let's look at a simple example of a live document that updates a counter.

from tagflow import tag, text, document, clear, spawn, transition
from tagflow import TagResponse, DocumentMiddleware, Live

from contextlib import asynccontextmanager

from fastapi import FastAPI
import anyio

live = Live()

app = FastAPI(lifespan=live.run)
app.add_middleware(DocumentMiddleware)

@app.get("/counter", response_class=TagResponse)
async def counter():
    session = await live.session()

    with tag.html():
        with tag.body():
            live.script_tag()
            session.client_tag()

            with tag.h1():
                async def loop():
                    i = 0
                    while True:
                        # A transition is like a transaction.
                        # After the block exits, the change is sent via WebSocket.
                        # The browser script applies it using a DOM View Transition.
                        with transition():
                            # This applies to the H1 element which is the current node.
                            clear()
                            text(str(i))
                        await anyio.sleep(1)
                        i += 1

                # We can spawn a task in the session's task scope.
                # All session tasks are cancelled when the session is closed.
                spawn(loop)

This feature uses AnyIO for structured concurrency and can work with either asyncio or trio as the backend.

It remains to be seen whether the "session" concept makes sense, and how to think about session lifecycles, reconnects, etc.

License

Tagflow is open source software released under the MIT license. See the LICENSE file for more details.

Metadata

Release files for tagflow 0.12.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tagflow 0.12.0
File Size Uploaded Provenance Actions
tagflow-0.12.0.tar.gz 42.3 kB Without provenance Details for tagflow-0.12.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for tagflow 0.12.0
File Interpreter ABI Platform Actions
tagflow-0.12.0-py3-none-any.whl Python 3 none any Details for tagflow-0.12.0-py3-none-any.whl

Total release size: 56.9 kB

Release files / tagflow-0.12.0.tar.gz

Download URL tagflow-0.12.0.tar.gz
Size 42.3 kB
Tags Source
SHA-256 checksum
How to use checksums
f8ea237be494c0a1b9e40e3801d639c23428de5cbf980431607887a34eb92790
BLAKE2b-256 checksum
How to use checksums
a7322acead5d3e005a4ea052c334db14d16f85a37e1649c776d20dfa07deef99
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.5.1

Release files / tagflow-0.12.0-py3-none-any.whl

Download URL tagflow-0.12.0-py3-none-any.whl
Size 14.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b843205d1e5a369f91e80baa160f2331cad6c83d401efd607b56588f80897d29
BLAKE2b-256 checksum
How to use checksums
75770fcd1170a810de78fee338ee9e60b98894c22ae29c6dd612f97394b9faaa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.5.1

Security for tagflow 0.12.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page