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 hotmix, fastapi, uvicorn

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 fastpai import FastAPI, Request
import hotmix as hm

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

@app.get("/")
@hm.htmx("index")
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")
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.2.tar.gz (2.2 kB view details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: hotmix-0.0.2.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.2.tar.gz
Algorithm Hash digest
SHA256 10245cf96135cebc4c2cef9cfc4b17804fedab50c9e28bf7ad3595ed4aacb9d2
MD5 89da71be5d89978fc6a2ab6492c70767
BLAKE2b-256 212677e61bb7d44c47de0ba9cba2eacdc966fe0c75d393a51866830beac41206

See more details on using hashes here.

File details

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

File metadata

  • Download URL: hotmix-0.0.2-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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 74e1720b1f08ceecfd5d45228fda7ca4cbe9b3fbe901627fb76dd0a973dd6b31
MD5 e8ba9f77d8df860f9de01b59ef5d56f7
BLAKE2b-256 2783c79089d7e774adc0072e1754835223f24ee08b30780013165242fa7d81c9

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