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.
_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.Data Initialization: The data
list of dictionaries holds the initial data for the table.
index
Route (Table Display):
index
route function generates the HTML for the table.data
list and creates table rows (<tr>
) with cells (<td>
).<input>
element, allowing the user to edit the values.<form>
element.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./update
Route (Data Processing):
update
route function handles the POST request when the "Bulk Update" button is clicked.await request.form()
.data
list and compares the new values from the form with the original values.data
list and adds a message to the changes
list.Div
containing the messages about the changes.@rt # Root route
def index():
# ... (code to generate the table HTML)
return Div(
Form(
Table( #... ),
Button('Bulk Update', hx_post="update", hx_target='#response', hx_indicator="#loading", _type="button")
),
Div(id='response'), # Target for the server response
Div(id="loading", style="display:none;", _class="loader"), # Loading indicator
)
@rt("update", methods=["POST"])
async def update(request):
# ... (code to process the form data and update the data list)
return Div(*[Div(change) for change in changes]) if changes else Div("No changes detected")