Skip to content

Latest commit

 

History

History
104 lines (72 loc) · 2.35 KB

File metadata and controls

104 lines (72 loc) · 2.35 KB

Hypermedia

Hypermedia is a pure python library for working with HTML. Hypermedia's killer feature is that it is composable through a slot concept. Because of that, it works great with </> htmx where you need to respond with both partials and full page html.

Hypermedia is made to work with FastAPI and </> htmx, but can be used by anything to create HTML.


Python Version from PEP 621 TOML


Documentation Stable | Source Code | Task Tracker

Features

  • Build HTML with python classes
  • Composable templates through a slot system
  • Seamless integration with </> htmx
  • Fully typed and Autocompletion for html/htmx attributes and styles
  • Opinionated simple decorator for FastAPI
  • Unlike other template engines like Jinja2 we have full typing since we never leave python land.

The Basics

All html tags can be imported directly like:

from hypermedia import Html, Body, Div, A

Tags are nested by adding children in the constructor:

from hypermedia import Html, Body, Div

Html(Body(Div(), Div()))

Add text to your tag:

from hypermedia import Div

Div("Hello world!")

use .dump() to dump your code to html.

from hypermedia import Bold, Div

Div("Hello ", Bold("world!")).dump()

output

<div>Hello <b>world!</b></div>

Composability with slots

from hypermedia import Html, Body, Div, Menu, H1, Div, Ul, Li

base = Html(
    Body(
        Menu(slot="menu"),
        Div(slot="content"),
    ),
)

menu = Ul(Li("home"))
content = Div("Some content")

base.extend("menu", menu)
base.extend("content", content)

base.dump()

output

<html>
    <body>
        <menu>
            <ul><li>home</li></ul>
        </menu>
        <div>
            <div>Some content</div>
        </div>
    </body>
</html>

Documentation

Head over to our documentation

If you are using this for HTMX, then please read the HTMX section of the documentation