Skip to main content

htpy - HTML in Python

Project description

htpy - HTML in Python

htpy is a library that makes writing HTML in plain Python fun and efficient, without a template language.

Define HTML in Python:

from htpy import body, h1, html, img, li, ul

is_fun = True
menu = ["spam", "ham", "eggs"]

print(
    html[
        body(class_={"fun": is_fun})[
            h1("#hi")["Welcome to htpy!"],
            img(src="rabbit.jpg"),
            ul(".menu")[(li[item] for item in menu)],
        ]
    ]
)

And get HTML:

<!DOCTYPE html>
<html>
  <body class="fun">
    <h1 id="hi">Welcome to htpy!</h1>
    <img src="rabbit.jpg">
    <ul class="menu">
      <li>spam</li>
      <li>ham</li>
      <li>eggs</li>
    </ul>
  </body>
</html>

Motivation for this project

At Personalkollen, where htpy was originally developed we often found ourselves hitting walls when using classic templates. htpy was created to improve the productiveness and experience of generating HTML from a Python backend.

Key features

  • Leverage static types: Use mypy or pyright to type check your code.

  • Great debugging: Avoid cryptic stack traces from templates. Use your favorite Python debugger.

  • Easy to extend: There is no special way to define template tags/filters. Just call regular functions.

  • Create reusable components: Define components, snippets, complex layouts/pages as regular Python variables or functions.

  • Familiar concepts from React: React helped make it popular writing HTML with a programming language. htpy uses a lot of similar constructs.

Installation

htpy is available on PyPI. You may install the latest version using pip:

pip install htpy

Documentation

The full documentation is available at https://htpy.dev.

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

htpy-24.3.17.tar.gz (56.1 kB view hashes)

Uploaded Source

Built Distribution

htpy-24.3.17-py3-none-any.whl (6.2 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