Skip to main content

fast_html is a fast, minimalist HTML generator.

It is an alternative to templating engines, like Jinja, for use with, e.g., htmx.

Pros:

  • use familiar python syntax

  • use efficient concatenation techniques

  • optional automatic indentation

Unlike other HTML generators (e.g. Dominate) that use python objects to represent HTML snippets, fast_html represents HTML snippets using string generators that can be rendered extremely fast using join. (see here)

Like other HTML generators, one needs to remember:

  • the name of some tags and attributes is changed (e.g., class_ instead of class, due to Python parser)

  • there may be conflicts of function names with your code base

Installation

pip install fast_html or copy the (single) source file in your project.

Don't forget to add a star on GitHub <https://github.com/pcarbonn/fast_html>_ ! Thanks.

Tutorial:

>>> from fast_html import *

A tag is created by calling a function of the corresponding name, and rendered using render:

>>> print(render(p("text")))
<p>text</p>

Tag attributes are specified using named arguments:

>>> print(render(br(id=1)))
<br id="1">

>>> print(render(br(id=None)))
<br>

>>> print(render(ul(li("text", selected=True))))
<ul><li selected>text</li></ul>

>>> print(render(ul(li("text", selected=False))))
<ul><li>text</li></ul>

The python parser introduces some constraints:

  • The following tags require a trailing underscore: del_, input_, map_, object_.

  • The following tag attributes require a trailing underscore: class_, for_.

In fact, the trailing underscore in attribute names is always removed by fast_html, and other underscores are replaced by -. For example, the htmx attribute hx-get is set using hx_get="url".

>>> print(render(object_("text", class_="s12", hx_get="url")))
<object class="s12" hx-get="url">text</object>

>>> print(render(button("Click me", hx_post="/clicked", hx_swap="outerHTML")))
<button hx-post="/clicked" hx-swap="outerHTML">Click me</button>

The innerHTML can be a list:

>>> print(render(div(["text",
...                    span("item 1"),
...                    span("item 2")
...                  ])))
<div>text<span>item 1</span><span>item 2</span></div>

The innerHTML can also be a list of lists:

>>> print(render(div(["text",
...                   [span(f"item {i}") for i in [1,2]]
...                  ])))
<div>text<span>item 1</span><span>item 2</span></div>

>>> print(render([br(), br()]))
<br><br>

You can call generators too:

>>> def row(number, name):
...     yield "<tr>"
...     yield td(number)
...     yield td(name)
...     yield "</tr>"
>>> def table():
...     yield "<table>"
...     yield from row("1", "A")
...     yield from row("2", "B")
...     yield "</table>"
>>> print(render(table()))
<table><tr><td>1</td><td>A</td></tr><tr><td>2</td><td>B</td></tr></table>

The innerHTML can also be specified using the i parameter, after the other attributes, to match the order of rendering:

>>> print(render(ul(class_="s12", i=[
...                 li("item 1"),
...                 li("item 2")]
...      )))
<ul class="s12"><li>item 1</li><li>item 2</li></ul>

You can create your own tag using the tag function:

>>> def my_tag(inner=None, **kwargs):
...     yield from tag("my_tag", inner, **kwargs)
>>> print(render(my_tag("text")))
<my_tag>text</my_tag>

Options:

By default, the inner string of a tag is not escaped: characters &, < and > in it are not converted to HTML-safe sequences.

>>> print(render(p("<bold>text</bold>")))
<p><bold>text</bold></p>

Of course, you can escape strings before calling fast_html:

>>> from html import escape
>>> print(render(p(escape("<bold>text</bold>"))))
<p>&lt;bold&gt;text&lt;/bold&gt;</p>

If your policy is to escape every inner string, you can activate escaping by setting the variable escape to True (or by calling escape_it(True)).

>>> escape_it(True)
>>> print(render(p("<bold>text</bold>")))
<p>&lt;bold&gt;text&lt;/bold&gt;</p>

When debugging your code, you can set global variable indent to True (or call indent_it(True)) to obtain HTML with tag indentation, e.g.,

>>> indent_it(True)
>>> print(render(div(class_="s12", i=["text\n", span("item 1"), span("item 2")])))
<div class="s12">
  text
  <span>
    item 1
  </span>
  <span>
    item 2
  </span>
</div>
<BLANKLINE>

You can also convert an HTML string to a function-based code representation:

>>> print(html_to_code('<div class="example"><p>Some text</p></div>'))
[div([p(['Some text'], )], class_="example")]

Metadata

Release files for fast_html 1.0.12

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

Source distribution (sdist)

Source distribution for fast_html 1.0.12
File Size Uploaded
fast_html-1.0.12.tar.gz 8.3 kB Details

Built distribution (wheel)

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

Total release size: 17.5 kB

Release files / fast_html-1.0.12.tar.gz

Download URL fast_html-1.0.12.tar.gz
Size 8.3 kB
Tags Source
SHA-256 checksum
How to use checksums
2a8dc0fa97e18481f5260c9d74bb8a0a09ce4339ede0708dfe605b281cb5211f
BLAKE2b-256 checksum
How to use checksums
07ed728ce00793837c858a7ce787b13c238ff6ce660878dcb72c2fb8994cd411
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.5 CPython/3.12.3 Linux/6.8.0-57-generic

Release files / fast_html-1.0.12-py3-none-any.whl

Download URL fast_html-1.0.12-py3-none-any.whl
Size 9.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b93a6f5326484a7930ebb872ac8c4aa0afb7b8c6e85611064f39d360651fefe5
BLAKE2b-256 checksum
How to use checksums
a93117f12a12a4f453882cf95623c29ba763353bf43ef9518da8209f69f7054d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.5 CPython/3.12.3 Linux/6.8.0-57-generic

Release history Release notifications | RSS feed

This release

1.0.12 This release

2 release files

1.0.11

2 release files

1.0.10

2 release files

1.0.9

2 release files

1.0.8

2 release files

1.0.7

2 release files

1.0.6

2 release files

1.0.5

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

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