HackerTrans
TopNewTrendsCommentsPastAskShowJobs

pelme

no profile record

Submissions

Show HN: Htpy – generate HTML from Python without templates

htpy.dev
7 points·by pelme·2 anni fa·2 comments

comments

pelme
·7 mesi fa·discuss
htpy supports passing data between multiple levels components with its context (very similar to React):

https://htpy.dev/usage/#passing-data-with-context
pelme
·anno scorso·discuss
(I am the maintainer of htpy). We use htpy for a big project with a lot of complex components/pages. We have yet to see htpy being a problem for performance reasons. For us, the performance problems always seem to be in our database queries or how we process our data. Also, to be clear, a lot of big sites use the Django template system which htpy seems to typically beat on performance. There are faster ways to generate HTML but htpy is not really slow either!

So to us, htpy performance has never been an issue which is why we have not been optimizing it further. If anyone is interested in re-implementing parts of htpy in cython/rust/something or improve the performance in other ways, that should be quite doable. The core htpy element/attribute implementation is a few hundred lines of code. It is heavily tested with 1000+ lines of tests. It could be a quite fun project, contributions are welcome!
pelme
·2 anni fa·discuss
htpy recently got a html2htpy command which can convert HTML to htpy code: https://htpy.dev/html2htpy/
pelme
·2 anni fa·discuss
Thanks, that makes sense! :)
pelme
·2 anni fa·discuss
In our company, where htpy was born, we are building a highly interactive application with htpy combined with Alpine.js+htmx. We have a couple of thousands lines of htpy code in production right now. We stick all HTML generation code into components/x.py or components.py files to keep it separate from other code. It is easy to grasp the structure. We use type hints so it is clear what data different components expect. "Goto defintion" just works so it is easy to navigate the code.

I agree about that HTML looks better with tags and it takes a bit of getting used to the python syntax. If something like JSX was possible in Python with all the tooling working, that would be great.
pelme
·2 anni fa·discuss
Thanks for making FastHTML, it is great to see more Python tooling that embraces Python for generating HTML.

What made you build FastTag instead of going with htpy? I am the author of htpy and any feedback would be very welcome!
pelme
·2 anni fa·discuss
We have been doing something similar with https://htpy.dev the last ~6 months.
pelme
·2 anni fa·discuss
Writing out the code to generate this may not be easier than using templates, but it is not really harder either. I think it mostly looks a little different.

The real benefit is using this bootstrap modal component within your app, since it is just a regular function call. You can leverage your editor "goto definition" and static type checking within all the functions make up your web sites/apps HTML.
pelme
·2 anni fa·discuss
Python's inline if's still read backwards to me and it would be nice to switch the order. But we are kind of stuck with it. I also don't want to introduce new control structures in this lib. Just want to use whatever Python has to offer in terms of control structures/syntax to make it more approachable.

htpy supports generators/callables as children (https://htpy.dev/streaming/). As long as you are careful to wrap things in generators/lambdas everything is lazy. Implementing if_ and for_ is straightforward and anyone can build constructs like that to use. But will not be lazy unless all arguments are wrapped in lambdas:

  from htpy import div, li, ul

  def if_(cond, a, b):
      return a() if cond() else b()

  def for_(items, func):
      return (func(item) for item in items())

  print(div[if_(lambda: True, lambda: "True!", lambda: "False!")])
  print(div[if_(lambda: False, lambda: "True!", lambda: "False!")])
  print(ul[for_(lambda: ["a", "b", "c"], lambda x: li[x])])
output:

  <div>True!</div>
  <div>False!</div>
  <ul><li>a</li><li>b</li><li>c</li></ul>
pelme
·2 anni fa·discuss
This looks great! Building HTML on the server with static types and htmx is a very productive combination!

At work, we have been exploring alternatives to templates in our Django project and just released it as a lib to help with this:

https://htpy.dev/