fasthtml
fasthtml copied to clipboard
Allow callable functions to be passed directly into hx_* attributes
Solves #864
Summary of the Issue & Changes
1. What the Problem Was
When passing router-decorated callable functions directly into hx_* attributes (like hx_post, hx_get, etc.), FastHTML did not automatically resolve them to their string URL paths. Developers were forced to explicitly wrap every function in str(), creating unnecessary friction, type-annotation mismatches, and messy code when attempting to use reverse routing.
2. What I Changed (The Solution)
I implemented automatic string resolution for callables passed into HTMX attributes to enable clean, native reverse routing.
- Core Attribute Logic (
fasthtml/components.py): Modified the component rendering logic to intercept callables inhx_*proxy attributes and automatically convert them to their corresponding string paths internally. - Documentation & Notebooks (
nbs/api/01_components.ipynb): Updated the source notebook to formally support this behavior, adjusted the component documentation examples, and added structural verification tests to ensure decorated functions resolve perfectly without manual casting.