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
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.
A tag is created by calling a function of the corresponding name, and rendered using render:
Tag attributes are specified using named arguments:
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 - (except for the _ attribute). For example, the htmx attribute hx-get is set using hx_get="url".
The innerHTML can be a list:
The innerHTML can also be a list of lists:
You can call generators too:
The innerHTML can also be specified using the i parameter, after the other attributes, to match the order of rendering:
You can create your own tag using the tag function:
By default, the inner string of a tag is not escaped: characters &, < and > in it are not converted to HTML-safe sequences.
Of course, you can escape strings before calling fast_html:
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)).
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.,
You can also convert an HTML string to a function-based code representation:
.png)


