Skip to main content

Add htmx to your FastAPI application

Project description

About

HotMix allows you to conveniently add htmx to your FastAPI app.

Getting Started

Install the packages

pip install uvicorn fastapi jinja2 hotmix

Create a tempate folder and add a template.

main.py
templates/
	index.html

Content of index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Simple Page</title>
  </head>
  <body>
    <h1>HotMix Hello World</h1>
    <p>Parameter from the API: {{ param }}</p>
    <p>You are accessing the path: {{ request.url.path }}</p>
  </body>
</html>

Content of main.py

from fastapi import FastAPI, Request
import hotmix as hm

app = FastAPI()
hm.init("templates")

@app.get("/")
@hm.htmx("index")
async def main(request: Request):
    return {"param": 37}

How it works

Initialize hotmix while setting the templates folder path

import hotmix as hm

hm.init("templates")

For each of the routes which should return some htmx content, add a decorator specifying the name of the template file, without the .html extension.

@app.get("/")
@hm.htmx("index")
async def main(request: Request):
    return {"param": 37}

Instead of returning the dictionary as JSON data, it will pass the dictionary to the jinja2 template engine, which will return the .html with the desired parameters.

HotMix can handle two kinds of parameters:

  • Explicit paramters: returned in the request answer dictionary. They are accessed by giving their names: {{ param }}.
  • Request parameters, accessed through the request keyword. For example {{ request.url.path }}.

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

hotmix-0.0.4.tar.gz (2.2 kB view details)

Uploaded Source

Built Distribution

hotmix-0.0.4-py3-none-any.whl (2.8 kB view details)

Uploaded Python 3

File details

Details for the file hotmix-0.0.4.tar.gz.

File metadata

  • Download URL: hotmix-0.0.4.tar.gz
  • Upload date:
  • Size: 2.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.4

File hashes

Hashes for hotmix-0.0.4.tar.gz
Algorithm Hash digest
SHA256 3683a5fb090c7858b4ba5f95913228317c1a0ce3038151fb19f6c6e24031f574
MD5 c4a39a0f39dc7d03d5da01db4292e274
BLAKE2b-256 f0bdeff0f0007906e736fce043a695f517606485802727d0382f93b12ca949b3

See more details on using hashes here.

File details

Details for the file hotmix-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: hotmix-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 2.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.4

File hashes

Hashes for hotmix-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 ce92d24d671a68495efabfe14bb4f10465cd0c5debcfd89a5dc52376dfc838cb
MD5 2b144c4a8030d6f19860956979f0b3c4
BLAKE2b-256 1deb55911d955014f0f7cd86476d82d9c0983aa05902e100a788416a90672572

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