Skip to main content

An opinionated way to work with html in pure python with htmx support.

Project description

Hypermedia

hypermedia is a pure python library for working with html. hypermedias killer feature is that it is html code is composable through slot concept. Because of that, it works great with HTMX partials and full page reloads.

Hypermedia is made to work with FastAPI and HTMX. but can be used by any web server.

Here are some 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 Html, Body, Div

Html(text="Hello world!")

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

from hypermedia import Html, Body, Div

Html(text="Hello world!").dump()

# outputs
# '<html>hello world</html>'

Composability with slots

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

base = Html(
    Body(
        Menu(slot="menu"),
        Header(slot="header", text="my header"),
        Div(slot="content"),
    ),
)

menu = Ul(Li(text="main"))
content = Div(text="Some content")

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

base.dump()

# outputs
# '<html><body><menu><ul><li>main</li></ul></menu><header>my header</header><div><div>Some content</div></div></body></html>'

With FastAPI:

coming

Using dependency injection.

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

hypermedia-2.0.0.tar.gz (11.6 kB view hashes)

Uploaded Source

Built Distribution

hypermedia-2.0.0-py3-none-any.whl (13.8 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