FastHTML – Modern web applications in pure Python
61–70 of 229 posts
Re: FastHTML – Modern web applications in pure Python
#62One 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
#63Not 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).
Re: FastHTML – Modern web applications in pure Python
#64Very 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…
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
#65Maybe I'm missing something here. Why not a templating engine?
Re: FastHTML – Modern web applications in pure Python
#66Re: FastHTML – Modern web applications in pure Python
#67Python is fast compared to something? Maybe fast enough to generate HTML.
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
#68Sorry, 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?
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
#69Re: FastHTML – Modern web applications in pure Python
#70Look 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?
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.