htpy is a library that makes writing HTML in Python fun and efficient, without the need for a template language.
-
Define HTML elements in Python...
from htpy import html, body, h1, img is_cool = True print(html[ body(class_={"cool": is_cool})[ h1("#hi")["Welcome to htpy!"], img(src="cat.jpg"), ] ])
-
...and render it as HTML.
<!doctype html> <html> <body class="cool"> <h1 id="hi">Welcome to htpy!</h1> <img src="cat.jpg"> </body> </html>
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.
-
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.