Live data from Hacker News

FastHTML – Modern web applications in pure Python

fastht.ml

61–70 of 229 posts

Re: FastHTML – Modern web applications in pure Python

#62
Very cool! After trying different approaches to render HTML from Python objects (including lxml, xml, etc.) I ended up liking htpy[0] the most, and the apps I built look similar to the examples in the FastHTML docs. I'll definitely try it.

One pattern I use is putting all the functions that generate HTML inside their own class. That way, I can more easily create and reuse components like:

  class Views:
      ...

  def comp1(self):
      return Div(self.header(), P("too"))
Then `self.header()` can be reused in other parts, or to return partial HTML. It also makes it easy to pass the "request" object to the class, and do conditional rendering based on it (cookies, auth, language, etc).

[0]: https://htpy.dev/

Re: FastHTML – Modern web applications in pure Python

#63
post #46

Not a very good "ad" as your page is quite slow and skips many frames, especially when scrolling past "The fastest way to create a real web application." "Fast"

I'm not seeing that. What browser/device are you using? The interactivity on the home page is just using Tailwind. I don't see why it would be slow for you (other than that the site is quite visually complex, so it naturally requires some baseline level of performance on your device).

Funny that you say "Visually complex" and "requires some baseline level of performance" when this same machine handles 3D games that are a thousand orders of magnitude more complex than your HTML displaying text and a few colours. What am I expecting though, that's the state of the web these days. Keep on creating more of the same rather than trying to fix this brainrot foundation you're building on.

Re: FastHTML – Modern web applications in pure Python

#64

Very cool! After trying different approaches to render HTML from Python objects (including lxml, xml, etc.) I ended up liking htpy[0] the most, and the apps I built look similar to the examples in the FastHTML docs. I'll definitely try it. One pattern I use is putting all the functions that generate HTML inside their own class. That way, I can more easily create and reuse components like: class Views: ... def comp1(s…

Yes htpy is nice! Other interesting examples of functional HTML include Elm-html (Elm), hiccl (Common Lisp), hiccup (Clojure), Falco.Markup (F#), Lucid (Haskell), and dream-html (OCaml). FastHTML's system, called "FastTag" (FT) is a bit of a mashup of all of them plus some extra bits. I seriously considered just using htpy actually -- but in the end decided I preferred something a little different.

I've wondered about a class-based approach like that -- interesting to hear it's worked for you. I should try it! I'm using a purely functional approach for re-use, as you see in this example of the code for about.fastht.ml:

https://github.com/AnswerDotAI/fh-about/blob/main/overview.p...

Re: FastHTML – Modern web applications in pure Python

#65
Sorry, but I hate server-side "helper" functions that generate HTML. For one thing it's never the same as what eventually gets shown on the page. 99.9% of the time you're missing attributes when needs to be hacked around. Debugging is a nightmare. Refactoring is hell. And css programmers have no clue what to do with this.

Maybe I'm missing something here. Why not a templating engine?

Re: FastHTML – Modern web applications in pure Python

#66
One check I always like to do with a new Python-based framework is this - does it support the creation of a dynamic number of components at runtime, AND each having their own component state? Most frameworks I've tried support one or the other, but not both. Is there an example that demonstrates something like this in FastHTML - user provides a number n at runtime, n cards are generated, each card has its own text field which can be modified by the user without affecting the other cards' text fields.

Re: FastHTML – Modern web applications in pure Python

#67

Python is fast compared to something? Maybe fast enough to generate HTML.

Fast enough for YouTube, Instagram, and Dropbox. If you need to scale up bigger than that then maybe reach for something else I guess.

Today's HN launch of FastHTML's home page was running on a $5/month hobbyist account at Railway.app, where it averaged 1% utilization of 1 VCPU.

(The trick, as always, is to optimise the inner loops in your app as needed; often that just means using pre-existing fast libs for that bit, but sometimes you may need to reach for cython/PyO3/etc. Often you'll find you don't need anything extra. FastHTML's own home page doesn't need anything extra.)

Re: FastHTML – Modern web applications in pure Python

#68

Sorry, but I hate server-side "helper" functions that generate HTML. For one thing it's never the same as what eventually gets shown on the page. 99.9% of the time you're missing attributes when needs to be hacked around. Debugging is a nightmare. Refactoring is hell. And css programmers have no clue what to do with this. Maybe I'm missing something here. Why not a templating engine?

In this case, it's a 1:1 mapping to what's on the page, so your concern doesn't apply here. Debugging and refactoring is far easier with Python functions than templates, and CSS programmers just use CSS the usual way.

To answer your question, I'll quote from https://about.fasht.ml:

Templates were originally created for web development in the 1990s, back when web design required complex browser-specific HTML. By using templates, designers were able to work in a familiar language, and programmers could “fill in the blanks” with the data they needed. Today this is not needed, since we can create simple semantic HTML, and use CSS to style it.

Templates have a number of disadvantages, for instance:

- They require a separate language to write the templates, which is an additional learning curve

- Template languages are generally less concise and powerful than Python

- Refactoring a template into sub-components is harder than refactoring Python code

- Templates generally require separate files

- Templates generally do not support the Python debugger.

By using Python as the HTML-generation language, we can avoid these disadvantages. More importantly, we can create a rich ecosystem of tools and frameworks available as pip-installable Python modules, which can be used to build web applications.

Re: FastHTML – Modern web applications in pure Python

#70
post #49

Look very nice, I love simplicity. Wondering how it would scale in real life - game of pi example feels slow. Is it possible to mix it with gradio? E.g. Make most of layout and UI in fastHTML but reuse some complex high level components from gradio?

The home page was running on a $5/month hobby account at launch today and reached 1% use of 1 VCPU -- so speed seems pretty good to me! Having said that, not all the example apps are well optimised, since we're aiming largely to teach the basics.

I'd love to see gradio-style components written in FastHTML -- I actually raised this idea with the founder of gradio today. It would be a great combo IMO.

Post reply on HN