https://github.com/scravy/fastertags
An HTML tag library for writing HTML in Python
https://github.com/scravy/fastertags
html library markup python3
Last synced: 5 months ago
JSON representation
An HTML tag library for writing HTML in Python
- Host: GitHub
- URL: https://github.com/scravy/fastertags
- Owner: scravy
- License: mit
- Created: 2024-10-12T00:35:24.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-12-23T02:23:03.000Z (over 1 year ago)
- Last Synced: 2025-09-19T19:45:25.062Z (10 months ago)
- Topics: html, library, markup, python3
- Language: Python
- Homepage: https://pypi.org/project/fastertags
- Size: 84 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# 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
```
An example document
Title
some paragraph
Some more paragraph
```
Tags and Attributes are scraped from the WHATWG HTML Spec
at [https://html.spec.whatwg.org/](https://html.spec.whatwg.org/).
Version numbers reflect when the scraping was done,
the current version `0.2024.11` was taken in December 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`:
```
doc = h.h1().on("click", "alert('clicked')")("Title")
print(doc)
#
Title
h.HTMLElement.EVENT_HANDLER_PREFIX= "on"
print(doc)
#
Title
```
## Scoped CSS
`fastertags` emulates the `scoped` attributes on `` 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@scope {scoped}