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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file hotmix-0.0.3.tar.gz
.
File metadata
- Download URL: hotmix-0.0.3.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
Algorithm | Hash digest | |
---|---|---|
SHA256 | c553fd7e07f23b1510508fc88b179ff841cf3e9643327976e892f1e00d78f459 |
|
MD5 | 3a28d3931fbb14a75951fb6f19d8ad70 |
|
BLAKE2b-256 | ae75ddbb3011abc8049f97d85defcc049ab9dee7cf9afc30bd0f9ced67f7208d |
File details
Details for the file hotmix-0.0.3-py3-none-any.whl
.
File metadata
- Download URL: hotmix-0.0.3-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
Algorithm | Hash digest | |
---|---|---|
SHA256 | 748b28308610acd1b578cb99ba7264d764c129e0c3b58a4713f027cc8072e84f |
|
MD5 | 52d599060f044998cf5503833df3c008 |
|
BLAKE2b-256 | 4f75b60f1e855478f3ee6075a14b529537edd181c9e8eff669fe4599c00baf97 |