Skip to main content

A lazy HTML element builder.

Project description

haitch


builds.sr.ht status PyPI - Version

haitch, an alternative pronunciation for the letter "H".

The haitch package provides a builder for composing HTML elements in a Document Object Model (DOM) tree. The element nodes will only be rendered when the __str__ method is invoked.

Why write your HTML in templates when you can do it in Python?

Core features:

  • Compose HTML elements.
  • Lazily evaluate DOM tree.
  • 100% test coverage.
  • 100% type annotated codebase.
  • Zero dependencies.

Upcoming features:

  • Explicit attribute annotations for known elements like a, img, etc.
  • Document elements based on MDN Web Docs.
  • Extra Annotations for common third-party plugins like htmx and alpine.

Requirements

Python 3.8+

Quickstart

Install haitch using pip:

$ pip install haitch

Import the library like so:

import haitch as H

By importing the root module, you now have access to any element you want:

# Render known `h1` tag.
h1 = H.h1("Hello, world")
str(h1) # <h1>Hello, world</h1>

# Render custom `foo` tag (useful for web components).
foo = H.foo("Hello, world")
str(foo) # <foo>Hello, world!</foo>

Here is a simple, real-world example that showcases the advantage of writing HTML in Python. Let's say we want a list of customer emails based on some business logic:

# Fetch customers from a data store.
customers = [
    ("jane@aol.com", False, True),
    ("bob@example.com", True, False),
    ("mark@mail.org", True, False),
    ("karen@hr.org", False, False),
]

# Build the DOM tree with attributes and children.
dom = H.div(class_="container")(
    H.h1("Customers to contact:"),
    H.ul()(
        *(
            H.li(
                H.a(href=f"mailto:{email}")(email),
            )
            for email, is_premium, is_new in customers
            if is_premium or is_new
        ),
    ),
)

print(dom)
# <div class="container">
#   <h1>Customers to contact:</h1>
#   <ul>
#     <li><a href="mailto:jane@aol.com">jane@aol.com</a></li>
#     <li><a href="mailto:bob@example.com">bob@example.com</a></li>
#     <li><a href="mailto:mark@mail.org">mark@mail.org</a></li>
#   </ul>
# </div>

FYI: The example output is prettified for better readability. The actual output is a minified string with no formatting.

Motivation

I was inspired by the fantastic htbuilder library as an alternative to template engines. The library met most of my needs, but I really wanted to expand its functionality by leveraging modern Python features like type annotations. This coincided with my excitement for Hypermedia-Driven Applications (HDAs) with tools like htmx and alpine.

While writing HDAs has simplified my application logic, the lack of autocompletion and diagnostics in templates are quite annoying. Therefore, my goal with this package is to make writing HTML as simple and fun as writing normal Python code by writing normal Python code.

License

haitch is distributed under the terms of the BSD-3-Clause license.

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

haitch-0.2.0.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

haitch-0.2.0-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file haitch-0.2.0.tar.gz.

File metadata

  • Download URL: haitch-0.2.0.tar.gz
  • Upload date:
  • Size: 6.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.26.0

File hashes

Hashes for haitch-0.2.0.tar.gz
Algorithm Hash digest
SHA256 b67f52cbe3a57da8ef98e4d2017fd79ed83d28b33f94c07d4b69f6cbcdba3898
MD5 d3b83804251d9c294fb404ea12175536
BLAKE2b-256 983092c415f113faff593b0c60ca63fc5afa1634a33827b90e0b50558e9583e9

See more details on using hashes here.

File details

Details for the file haitch-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: haitch-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.26.0

File hashes

Hashes for haitch-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a43ce1e7d27116de70df14e496577dda529df3057b31a085b08943ef398ae934
MD5 021eb22c0a428129bc2309861cd1e98f
BLAKE2b-256 04286275b317f61de19382a9a2ca5da79f4ec0534925a29d64224de8a2e7f9bd

See more details on using hashes here.

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