Description
The swifties of HTMX on twitter are asking "why not HTMX?".
While I'm not as enamoured by HTMX as some, there is a good argument to build a frontend that requires no javascript, with optional improvement via HTMX.
The main advantage as I see it is to end users of the applications - less JS to download, less chance of clientside bugs that go unfixed, less chance of something built with this silently breaking in 10 years time when the maintainer is dead.
The real question is how to render the HTML, there are three options:
- Jinja2 (or other python template rendering engine), running as middleware or a reverse proxy to take models returned by the endpoint and render them to HTML
- something at the edge that takes the models and renders them to HTML, I tried to build something like this 3 years ago using wasm but the performance was too bad, AFAIK that's still an issue unless the app is very high volume, so you'd need to somehow render the HTML with pure javascript and no eval, doesn't sound easy, though it would be awesome
- I have a very experimental template rendering engine that uses JSX syntax and is written in Rust with Python bindings called psx, maybe one day if I get rich or end up in prison I'll finish it
In the mean time, the only realistic way to do this is option 1.
Are there are python bindings for one of the rust ports of jinja, like minijinja? (@mitsuhiko I seem to remember you talking about this on twitter, does it exist?)
Rather than middleware or a reverse proxy, we could have a function that renders the HTML at return time, so you do something like
@app.get('/api/')
async def my_view(...) -> HTMLResponse:
components = FastUI(root=[...])
return render_fastui_response_with_template_engine(components)
One very nice feature of returning JSON from an app is that unit tests are far easier than when returning HTML, therefore whatever solution we end up with (if we end up with on at all 🤷), we should be able to switch off the rendering and return JSON for tests.