Skip to main content

Generate HTML conveniently and efficiently

Project description

This module contains functions to generate HTML conveniently and efficiently.

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

Pros:

  • use familiar python syntax

  • use efficient concatenation techniques (join, see here)

  • optional automatic indentation

Cons:

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

  • possible conflicts of function names in your code base

Installation

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

Don’t forget to add a star on GitHub ! Thanks.

Tutorial:

>>> from htmx_gen 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>

Some tag attributes are changed: you must add _ to tag (or attribute) names conflicting with Python reserved names, (e.g. class_ instead of class), and you must use _ instead of - in attribute names.

>>> print(render(p("text", class_="s12", hx_get="url")))
<p class="s12" hx-get="url">text</p>
>>> 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>

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>

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", span("item 1"), span("item 2")])))
<div class="s12">
  text
  <span>
    item 1
  </span>
  <span>
    item 2
  </span>
</div>
<BLANKLINE>

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

htmx_gen-1.0.1.tar.gz (16.5 kB view hashes)

Uploaded Source

Built Distribution

htmx_gen-1.0.1-py3-none-any.whl (16.3 kB view hashes)

Uploaded Python 3

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