Skip to content

DBProductions/htmx-examples

Repository files navigation

HTMX Examples

This repository demonstrates how to build modern, dynamic user interfaces using HTMX with a FastAPI backend in Python 3.12.6.
Instead of relying on JSON APIs and heavy JavaScript, this project follows the Hypermedia-Driven Application (HDA) architecture: the backend sends HTML fragments directly, and HTMX handles DOM updates seamlessly.

🛠️ Tech Stack

All frontend dependencies are loaded directly from index.html via <script> tags.

▶️ Running the App

Install dependencies and start the development server:

python -m venv ./venv
source ./venv/bin/activate

pip install -r requirements.txt
uvicorn main:app --reload

App is reachable under: http://127.0.0.1:8000/

Swagger: http://127.0.0.1:8000/docs

Redoc: http://127.0.0.1:8000/redoc

🧪 Run tests

For tests, user data is loaded from user_data.json instead of being fetched from randomuser.me.

All tests:

pytest tests/*.py -v

Run a specific test:

pytest tests/test_htmx_form.py -v
pytest tests/test_htmx_scrolling.py -v
pytest tests/test_htmx_stream.py -v
pytest tests/test_htmx_table.py -v

🌟 Examples

All examples fetch user data from https://randomuser.me/api/.

Data Table

Dynamically load and display user data in a responsive table.
Some _hyperscript is used to handle HTML form validation. Sweetalert2 is used to confirm a deletion.

data table screenshot data table add screenshot data table delete screenshot

Form Handling

Submit and validate forms without page reloads.
Some JavaScript is used to handle HTML form validation.

form step 1 screenshot form step 2 screenshot form success screenshot

Server-Sent Events (SSE) Streaming

Real-time updates via HTMX streams.
Good way to get data pushed from the server for interaction websockets needs to be used.

stream screenshot

Infinite Scrolling

Load additional content as the user scrolls.
A typincal thing while consuming content to scroll over endless entries.

scrolling screenshot

💬 Feedback

Star this repo if you found it useful. Use the github issue tracker to give feedback on this repo.

About

Some examples following the Hypermedia-Driven Application with HTMX and FastAPI

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published