Skip to main content

A small example package

Project description

fastertags, an HTML tag library

A typesafe library for writing HTML in Python:

import fastertags as h

if __name__ == "__main__":
    doc = h.html(
        h.head(
            h.title("An example document"),
        ),
        h.body(
            h.h1("Title"),
            h.p(class_="css-class-name")("some paragraph"),
            h.p("Some more paragraph"),
        ),
    )
    print(doc)

yields

<!DOCTYPE html><html><head><title>An example document</title></head><body><h1>Title</h1><p class="css-class-name">some paragraph</p><p>Some more paragraph</p></body></html>

Tags and Attributes are scraped from the WHATWG HTML Spec at https://html.spec.whatwg.org/. Version numbers reflect when the scraping was done, the current version 0.2024.9 was taken in September 2024.

HTMX

fastertags is a standalone building block for fasterhtml, which in turn uses HTMX. Most notably, fastertags serializes event-handler on* attributes as hx-on:*, for example hx-on:click instead of onclick.

This can be changed by setting HTMLElement.EVENT_HANDLER_PREFIX:

print(h.h1().on("click", "alert('clicked')")("Title"))
# <h1 hx-on:click="alert('clicked')">Title</h1>
h.HTMLElement.EVENT_HANDLER_PREFIX= "on"
print(h.h1().on("click", "alert('clicked')")("Title"))
# <h1 onclick="alert('clicked')">Title</h1>

Scoped CSS

fastertags emulates the scoped attributes on <style> tags by wrapping its contents in @scope { ... }. The h.style() function does feature the boolean attribute scoped that allows for True, False, or None. The default is None, which means contents will be wrapped in @scope { ... } if the element is not serialized as the child of a head element:

doc = h.html(
    h.head(
        h.style("not scoped"),
    ),
    h.body(
        h.style("scoped"),
    ),
)
print(doc)

yields

<!DOCTYPE html><html><head><style>not scoped</style></head><body><style>@scope {scoped}</style></body></html>

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

fastertags-0.2024.9-py3-none-any.whl (22.3 kB view details)

Uploaded Python 3

File details

Details for the file fastertags-0.2024.9-py3-none-any.whl.

File metadata

File hashes

Hashes for fastertags-0.2024.9-py3-none-any.whl
Algorithm Hash digest
SHA256 4dbe27f7ced6cc58fe5b01e0dbaecf166b452dc77315214b927bd724c66e8dc6
MD5 44fc41b67523552ae66cf15ae8c59983
BLAKE2b-256 445feacc9658f15d6bc31b2c5df5439aa7cd55e7e6a1a431ba58c19515b36238

See more details on using hashes here.

Supported by

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