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 details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

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

Uploaded Python 3

File details

Details for the file htmx_gen-1.0.1.tar.gz.

File metadata

  • Download URL: htmx_gen-1.0.1.tar.gz
  • Upload date:
  • Size: 16.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.15 CPython/3.7.7 Linux/5.15.0-48-generic

File hashes

Hashes for htmx_gen-1.0.1.tar.gz
Algorithm Hash digest
SHA256 d1f27f6185955c9335f8a72b6067e374fa4f43ca8de076e12e54c77d0fe7a94f
MD5 26ae04f7073b3c8a44bf28cb6ad90464
BLAKE2b-256 e10663131f09e0a99263dda0c3e7c908f3acde24a692e41873ccc87f39038e9a

See more details on using hashes here.

File details

Details for the file htmx_gen-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: htmx_gen-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 16.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.15 CPython/3.7.7 Linux/5.15.0-48-generic

File hashes

Hashes for htmx_gen-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1141eb3bbeb9cb38b133e1fa1545dbc3d83d31cd0e05c7e2c352a1a16457bc2c
MD5 0645e157a577c40e08c362c04c037385
BLAKE2b-256 bc28ab9dcf4f6dc04d6dc0e37105c0531dcf4ce6502558200cb85e5517cd2237

See more details on using hashes here.

Supported by

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