News for hackers. Stuff that matters.

Show HN: Htpy – generate HTML from Python without templates

https://htpy.dev

I built a library that to generate HTML from Python. We have been using this library with Django the last couple of months instead of classic templates and find it to be productive. It is easy to debug, works great with static type checkers and it is easy to build reusable components/partials. Give it a try!
( 2 comments | 2 points | on HN )
Re:Show HN: Htpy – generate HTML from Python without templates by rrr_oh_man on Sunday April 28, 2024 @05:01AM (#40187125)
Is that really easier than using templates?

> Wrapping Bootstrap Modal could be achieved with a function like this:

  def bootstrap_modal(*, title: str, body: Node = None, footer: Node = None) -> 
  Element:
      return div(".modal", tabindex="-1", role="dialog")[
          div(".modal-dialog", role="document")[
              div(".modal-content")[
                  div(".modal-header")[
                      div(".modal-title")[
                          h5(".modal-title")[title],
                          button(
                              ".close",
                              type="button",
                              data_dismiss="modal",
                              aria_label="Close",
                          )[span(aria_hidden="true")[Markup("×")]],
                      ]
                  ],
                  div(".modal-body")[body],
                  footer and div(".modal-footer")[footer],
              ]
          ]
      ]
Re:Show HN: Htpy – generate HTML from Python without templates by pelme on Sunday April 28, 2024 @07:28AM (#40187783)
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.