Skip to main content

A lazy HTML element builder.

Project description

haitch logo

A lazy HTML element builder.

builds.sr.ht status PyPI - Version

haitch

alternative pronunciation to the letter "H"

The haitch library provides a builder for composing HTML elements together. The element nodes are functional and will only be rendered when __str__ is invoked.

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

Features

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

Roadmap

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")
print(h1) # <h1>Hello, world</h1>

# Render custom `foo` tag (useful for web components).
foo = H.foo("Hello, world")
print(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(id_="email-customer-list")(
        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 id="email-customer-list">
#     <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>

The printed output above is prettified for better readability. The actual output is a minified string with no formatting.

Motivation

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 typing. 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.

Non-goals

The following features will not be supported:

  • Deprecated elements and attributes: if you want to use them, that is fine by me as haitch supports generic elements and attributes. However, I am not going to write the annotations and documentation for them.
  • 100% input validation: this library is meant to assist the developer to write valid HTML with the help of type annotations and documentation. In the end, it is up to the developer and browser to verify that the input is of the correct type. So for example, I will not add any validation to make sure that a <col> element is directly nested inside of a <colgroup> element or that the span attribute for the <col> element is a positive integer.

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.7.0.tar.gz (28.2 kB view details)

Uploaded Source

Built Distribution

haitch-0.7.0-py3-none-any.whl (46.8 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for haitch-0.7.0.tar.gz
Algorithm Hash digest
SHA256 41a4e756dd8c9c38086d4a94ef6d4a730299feb8776b5faad17978c30f0d13a8
MD5 0d55047e41247169796361e9e221494e
BLAKE2b-256 bcb0fd8b3a9516512b133a8a6d578b26cf07bf485547994f019bc82503bb251b

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for haitch-0.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e6024bbcdb76e99e263944424b130ec71d16f3e8c69ae8e00a8a092221d9bcc8
MD5 cab02f029e7941ba7d187892c0fb4e2f
BLAKE2b-256 c26bda4f1a17a872ea5df6c87850aa97cd0f18cf6b7c95ff3c46557c4cf8a3e9

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