|
| 1 | +# Bulk Data Update with Fasthtml and HTMX |
| 2 | + |
| 3 | +This example demonstrates how to create a simple web application using Fasthtml and HTMX that allows users to bulk update data displayed in an HTML table. Users can modify the values in the table's input fields and then click a button to submit all the changes at once. |
| 4 | + |
| 5 | +## Key Features |
| 6 | + |
| 7 | +* **Dynamic Table Generation:** The HTML table is dynamically generated from a Python list of dictionaries. Each dictionary represents a row in the table. |
| 8 | +* **Client-Side Updates (with HTMX):** The changes are submitted via an AJAX-like request using HTMX. This avoids a full page reload, providing a smoother user experience. |
| 9 | +* **Server-Side Processing:** The updates are processed on the server using a Python function. |
| 10 | +* **Data Persistence (In-Memory):** The example uses an in-memory data structure (a list of dictionaries) to store the data. In a real-world application, you would replace this with a database or other persistent storage. |
| 11 | +* **Preventing Accidental Submissions:** The "Bulk Update" button uses `_type="button"` to prevent the form from being submitted when the user presses Enter in the input fields. This ensures that only a button click triggers the update process. |
| 12 | + |
| 13 | +## How it Works |
| 14 | + |
| 15 | +1. **Data Initialization:** The `data` list of dictionaries holds the initial data for the table. |
| 16 | + |
| 17 | +2. **`index` Route (Table Display):** |
| 18 | + - The `index` route function generates the HTML for the table. |
| 19 | + - It iterates through the `data` list and creates table rows (`<tr>`) with cells (`<td>`). |
| 20 | + - Each cell in the 'Name' and 'Age' columns contains an `<input>` element, allowing the user to edit the values. |
| 21 | + - The table is wrapped in a `<form>` element. |
| 22 | + - A "Bulk Update" button is included in the form. The `hx_post` attribute on the button specifies the route (`/update`) that will handle the form submission. The `hx_target` attribute specifies where the response from the server should be displayed (`#response`). `hx_indicator` shows a loading indicator while the request is in progress. `_type="button"` prevents form submission on Enter key press. |
| 23 | + |
| 24 | +3. **`/update` Route (Data Processing):** |
| 25 | + - The `update` route function handles the POST request when the "Bulk Update" button is clicked. |
| 26 | + - It retrieves the form data using `await request.form()`. |
| 27 | + - It iterates through the `data` list and compares the new values from the form with the original values. |
| 28 | + - If a value has changed, it updates the `data` list and adds a message to the `changes` list. |
| 29 | + - Finally, it returns a `Div` containing the messages about the changes. |
| 30 | + |
| 31 | +## How to Use |
| 32 | + |
| 33 | +1. Run the example. |
| 34 | +2. The table will be displayed in your browser. |
| 35 | +3. Edit the 'Name' and 'Age' values in the input fields as needed. |
| 36 | +4. Click the "Bulk Update" button. |
| 37 | +5. The changes will be processed, and a message will appear below the button indicating which rows were updated. |
| 38 | + |
| 39 | +## Code Explanation (Key Parts) |
| 40 | + |
| 41 | +```python |
| 42 | +@rt # Root route |
| 43 | +def index(): |
| 44 | + # ... (code to generate the table HTML) |
| 45 | + return Div( |
| 46 | + Form( |
| 47 | + Table( #... ), |
| 48 | + Button('Bulk Update', hx_post="update", hx_target='#response', hx_indicator="#loading", _type="button") |
| 49 | + ), |
| 50 | + Div(id='response'), # Target for the server response |
| 51 | + Div(id="loading", style="display:none;", _class="loader"), # Loading indicator |
| 52 | + ) |
| 53 | + |
| 54 | +@rt("update", methods=["POST"]) |
| 55 | +async def update(request): |
| 56 | + # ... (code to process the form data and update the data list) |
| 57 | + return Div(*[Div(change) for change in changes]) if changes else Div("No changes detected") |
0 commit comments