|
| 1 | +# HTMX Examples |
| 2 | + |
| 3 | +This repository demonstrates how to build modern, dynamic user interfaces using **HTMX** with a **FastAPI** backend in Python 3.12.6. |
| 4 | +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. |
| 5 | + |
| 6 | +## 🛠️ Tech Stack |
| 7 | + |
| 8 | +- **Backend**: [FastAPI](https://fastapi.tiangolo.com/) (Python) and [Jinja2](https://jinja.palletsprojects.com/) as template engine |
| 9 | +- **Frontend**: [HTMX](https://htmx.org/) with plain HTML, [Pico CSS](https://picocss.com/) and some [_hyperscript](https://hyperscript.org/) |
| 10 | +- **Testing**: [Playwright](https://playwright.dev/python/) for end-to-end tests |
| 11 | +- **Data Source**: [Random User API](https://randomuser.me/api/) |
| 12 | + |
| 13 | +All frontend dependencies are loaded directly from `index.html` via `<script>` tags. |
| 14 | + |
| 15 | +## ▶️ Running the App |
| 16 | + |
| 17 | +Install dependencies and start the development server: |
| 18 | + |
| 19 | + python -m venv ./venv |
| 20 | + source ./venv/bin/activate |
| 21 | + |
| 22 | + pip install -r requirements.txt |
| 23 | + uvicorn main:app --reload |
| 24 | + |
| 25 | +App is reachable under: `http://127.0.0.1:8000/` |
| 26 | +Swagger: `http://127.0.0.1:8000/docs` |
| 27 | +Redoc: `http://127.0.0.1:8000/redoc` |
| 28 | + |
| 29 | +## 🧪 Run tests |
| 30 | + |
| 31 | +For tests, user data is loaded from `user_data.json` instead of being fetched from `randomuser.me`. |
| 32 | + |
| 33 | +All tests: |
| 34 | + |
| 35 | + pytest tests/*.py -v |
| 36 | + |
| 37 | +Run a specific test: |
| 38 | + |
| 39 | + pytest tests/test_htmx_form.py -v |
| 40 | + pytest tests/test_htmx_scrolling.py -v |
| 41 | + pytest tests/test_htmx_stream.py -v |
| 42 | + pytest tests/test_htmx_table.py -v |
| 43 | + |
| 44 | +## 🌟 Examples |
| 45 | +All examples fetch user data from [https://randomuser.me/api/](https://randomuser.me/api/). |
| 46 | + |
| 47 | +### Data Table |
| 48 | +Dynamically load and display user data in a responsive table. |
| 49 | +Some _hyperscript is used to handle HTML form validation. Sweetalert2 is used to confirm a deletion. |
| 50 | + |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | +### Form Handling |
| 56 | +Submit and validate forms without page reloads. |
| 57 | +Some JavaScript is used to handle HTML form validation. |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | +### Server-Sent Events (SSE) Streaming |
| 64 | +Real-time updates via HTMX streams. |
| 65 | +Good way to get data pushed from the server for interaction websockets needs to be used. |
| 66 | + |
| 67 | + |
| 68 | + |
| 69 | +### Infinite Scrolling |
| 70 | +Load additional content as the user scrolls. |
| 71 | +A typincal thing while consuming content to scroll over endless entries. |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | +## 💬 Feedback |
| 76 | +Star this repo if you found it useful. Use the github issue tracker to give feedback on this repo. |
0 commit comments