There's a bunch of these kinds of html renderers. Here's mine: https://pypi.org/project/simple-html/ But there are many others. Not sure I understand the point of async rendering, unless you want to mix IO with rendering? Which feels a bit too close to old PHP+html for my blood.
Htmy – Async, pure-Python rendering engine
21–30 of 90 posts
Re: Htmy – Async, pure-Python rendering engine
#22Not clear why HTML rendering needed to be infected with async. None of the example code has a clear need for async - even the `is_admin()` method would be a prefetched property in any reasonable database model.
Imagine you have 2 big components, one fetches from an third-party API and the other from your backend, this way they can load at the same time instead of sequentially.
Re: Htmy – Async, pure-Python rendering engine
#23Re: Htmy – Async, pure-Python rendering engine
#24I think in almost-2025 any dataclass heavy library should probably use pydantic (or support it)
Dataclasses let you return "incorrect" data and that's a good thing. I'd rather get an unexpected None here and there (which can be handled) than have library code crash because the wrong type snuck into a field I don't even care about.
As for support, is any explicit support needed? You can Pydantic models into things expecting dataclasses and often the other way around too.
Re: Htmy – Async, pure-Python rendering engine
#25I think in almost-2025 any dataclass heavy library should probably use pydantic (or support it)
Please don't! Pydantic demands 100% type correctness at runtime in a language that can't guarantee basically anything at "compile" (lint) time. Screw up one type annotation for one edge case and your entire system turns into one big ValidationError. Dataclasses let you return "incorrect" data and that's a good thing. I'd rather get an unexpected None here and there (which can be handled) than have library code crash…
Re: Htmy – Async, pure-Python rendering engine
#26Re: Htmy – Async, pure-Python rendering engine
#27But what do you do use to create dynamic updates on the client side? I’m guessing it still has JS and makes API calls, no? And if so, it seems easier (to me) to just do all of the rendering client side and let the backend just be REST queries.
Re: Htmy – Async, pure-Python rendering engine
#28Oh it's server side "rendering"?
Re: Htmy – Async, pure-Python rendering engine
#29[flagged]
Re: Htmy – Async, pure-Python rendering engine
#30I was looking for something like this a few weeks ago. I typically use Django and hate the template engines limitations. I needed to make some reusable components and the best option available was switching to jinja to get their macro support, bleh. This reminds me of the best part of Flutter UI composition, but in a language I always return to. Have you done any benchmarking? I don't even know what the comparison wo…
Check this out: https://compone.kissgyorgy.me/ Much simpler than this library, components are simply functions, rendered to strings. I made one microbenchmark, it's "only" 2x slower than Jinja2 right now, but I know how to make it faster.
I've implemented a bunch of AlpineJS "components" as jinja macros in my current project and ... it works, but it's pretty ugly and it sucks not having type safety or ability for the IDE to understand connections between the template and the Python code
what I really want is something like JSX/TSX for Python... having gone through this process I can see why that approach is desirable. I kind of feel like libs which mimic the syntax but unable to provide the type-safety/IDE support are missing the point. So although I love the look of "Python HTML element objects" approach libs like yours and OP have I think for now it is probably the best way available.
for my current project we are pre-compiling all the jinja templates (via Jinja's own utils) for deployment as AWS Lambda
I did look into JinjaX but it has its own separate jinja env and secondary template cache and didn't look like it would be easy to plug it into the pre-compile step