Skip to main content

simplify your HTML building.

Project description

haitch logo

simplify your HTML building.

builds.sr.ht status PyPI - Version

haitch

alternative pronunciation for 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 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 example that showcases the advantage of writing HTML in Python. Let's say we want an ordered list of stored org domain emails:

# Fetch emails from data store.
emails = ["jane@aol.com", "bob@example.com", "mark@mail.org", "karen@hr.org"]

# Build an ordered list with org domain emails.
dom = H.ol(class_="email-list")(
    H.li(H.a(href=f"mailto:{email}")(email))
    for email in sorted(emails)
    if email.endswith(".org")
)

print(dom)
# <ol class="email-list">
#   <li><a href="mailto:karen@hr.org">karen@hr.org</a></li>
#   <li><a href="mailto:mark@mail.org">mark@mail.org</a></li>
# </ol>

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

Documentation

Activate your virtual environment where haitch is installed and run:

$ python -c "help('haitch')"

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.

Releases

This library adheres to semantic versioning and keeps a changelog.

Examples

Intended to help you to integrate haitch into your application:

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

Uploaded Source

Built Distribution

haitch-0.10.1-py3-none-any.whl (56.2 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for haitch-0.10.1.tar.gz
Algorithm Hash digest
SHA256 1208f7466b28258a23db1fcf68aaa754015599aff646b8344944d82db8aa77e3
MD5 0f594211c9b435fb786848e24cc8cae0
BLAKE2b-256 ab795aac1844c1eb90886c19fa351d5790cf0171ef9d0a9f5286fb7160a76b27

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for haitch-0.10.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1bae4bcbb169e3ac68898f190a0734474be7f4b8ce46853fe7d3e5ed70b2b185
MD5 c7f94f15f2b09e88f2031c0870b17f6a
BLAKE2b-256 2c34c5ba5a9301c7e9e786a274fdb16791b4e90a943dd0a7bc02894be70164dc

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