Skip to main content

FastAPI Tags

Adds s-expression HTML tags to FastAPI views. Inspired by FastHTML's use of fastcore's FT components.

Test Package version Supported Python versions

Installation:

uv:

uv add fastapi-tags

pip:

pip install fastapi-tags
uv pip install fastapi-tags

Usage:

from fastapi import FastAPI
import fastapi_tags as tg

app = FastAPI()

@app.get("/", response_class=tg.TagResponse)
async def index():
    return tg.Html(tg.H1("Hello, world!", style="color: blue;"))

If you want to do snippets, just skip the tg.Html tag:

@app.get("/time", response_class=tg.TagResponse)
async def time():
    return tg.P("Time to do code!")

With HTMX

If you want to detect HTMX use dependency injection:

from fastapi import Depends

@app.get("/hello", response_class=tg.TagResponse)
def test_endpoint(is_htmx: bool = Depends(tg.is_htmx_request)):
    if is_htmx:
        return tg.H1("Hello, hx-request! Here's a partial of the page.")
    else:
        return tg.Html(tg.H1("Hello normal request, Here's the full page!"))

Custom Tags

There are several ways to create custom Tags

Subclassing

class AwesomeP(tg.P) -> tg.Tag:
    def render(self) -> str:
        return f"<p{self.attrs}>AWESOME {self.children}!</p>"
AwesomeP('library')
<p>AWESOME library!</p>

Custom tags built as functions

def PicoCard(header: str, body: str, footer: str) -> tg.Tag:
    return tg.Article(
        tg.Header(header),
        body,
        tg.Footer(footer)
    )
@app.get("/card", response_class=tg.TagResponse)
async def card():
    return PicoCard(
        'FastAPI Tags',
        'Adds s-expression HTML tags (Tags) to FastAPI views.',
        'by various contributors'
    )
<article>
    <header>FastAPI Tags</header>
    Adds s-expression HTML tags (Tags) to FastAPI views.
    <footer>by various contributors</footer>
</article>

Raw HTML Content

For cases where you need to render raw HTML:

from fastapi_tags import RawHTML

# Render raw HTML content
raw_content = RawHTML('<strong>Bold text</strong> and <em>italic</em>')

# Note: RawHTML only accepts a single string argument
# For multiple elements, combine them first:
html_string = '<p>First</p><p>Second</p>'
raw = RawHTML(html_string)

Release files for fastapi-tags 0.5.1

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

Source distribution (sdist)

Source distribution for fastapi-tags 0.5.1
File Size Uploaded
fastapi_tags-0.5.1.tar.gz 12.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fastapi-tags 0.5.1
File Interpreter ABI Platform
fastapi_tags-0.5.1-py3-none-any.whl Python 3 none any Details

Total release size: 21.8 kB

Release files / fastapi_tags-0.5.1.tar.gz

Download URL fastapi_tags-0.5.1.tar.gz
Size 12.3 kB
Tags Source
SHA-256 checksum
How to use checksums
b9b3b7197c1386d7dd53b5385e562b3a5ba1ad499d89ffe8985243473ffd601c
BLAKE2b-256 checksum
How to use checksums
186341198a1c78395e7d23be597e5f3bbfec8fe57cc11ace6cd413e6ccf3e2d3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.7.3

Release files / fastapi_tags-0.5.1-py3-none-any.whl

Download URL fastapi_tags-0.5.1-py3-none-any.whl
Size 9.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4e4a4a9ee5d20aa1808dbba19e3d39ef97fd78bab5e253f7600bf3e4dd6d5251
BLAKE2b-256 checksum
How to use checksums
6e30990f11ec744ed0ed026fa07e5d1c69b0515ea8279a698a28ced75bd48cd8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.7.3

Release history Release notifications | RSS feed

This release

0.5.1 This release

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

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