Skip to main content

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

Project description

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!")

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>

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastapi_tags-0.3.0.tar.gz (8.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastapi_tags-0.3.0-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_tags-0.3.0.tar.gz.

File metadata

  • Download URL: fastapi_tags-0.3.0.tar.gz
  • Upload date:
  • Size: 8.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.3

File hashes

Hashes for fastapi_tags-0.3.0.tar.gz
Algorithm Hash digest
SHA256 310b7ab98a8e70aef61262d69720ec5cc539c0cb15b6bc38dd6faf92c130af7b
MD5 e9505964ab28c823649558a186b01261
BLAKE2b-256 8be1a3d90b6834a66a51064afcc23e20fc2a04a694431c44de016f6c78221756

See more details on using hashes here.

File details

Details for the file fastapi_tags-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_tags-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 351aec5fb70edbc4947f85cad6fa01e47e66c4454be3313be2d9e121de5c532e
MD5 266331e0f75135c971f864728bfd31dd
BLAKE2b-256 47b59e8c69da62d717e3d406f7aac23d9d621ffea365d3ca4c1810700cc8d0c8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page