July 29th, 2024

FastHTML – Modern web applications in pure Python

FastHTML is a Python library for building web applications efficiently, supporting dynamic content, reusable components, and various databases. It simplifies development, focusing on performance and scalability for Python developers.

Read original articleLink Icon
ExcitementSkepticismAppreciation
FastHTML – Modern web applications in pure Python

FastHTML is a Python library designed for building modern web applications quickly and efficiently. It allows developers to create everything from simple dashboards to scalable applications using pure Python, without relying on complex frameworks. FastHTML integrates seamlessly with existing web technologies, providing full access to HTTP, HTML, JavaScript, and CSS. This flexibility enables developers to utilize any Python or JavaScript libraries they prefer.

The library supports dynamic content generation, reusable UI components, and various database systems, making it suitable for a wide range of applications, including dashboards, interactive reports, and content-heavy sites. FastHTML applications are designed to be fast and scalable, with easy deployment options on any service that supports Python, such as Railway, Vercel, and Hugging Face.

FastHTML is inspired by FastAPI, focusing on creating HTML applications rather than APIs. It is particularly well-suited for developing single-page applications (SPAs) and can work with HTMX, a JavaScript library that enhances browser interactions. Developers can create applications without needing extensive JavaScript knowledge, although incorporating JavaScript is possible for added functionality.

FastHTML has received positive feedback from experts in the field, who appreciate its intuitive design and clean architecture. Overall, FastHTML aims to simplify the web development process while maintaining performance and scalability, making it an attractive option for Python developers.

Related

AI: What people are saying
The comments on FastHTML reveal a mix of excitement and skepticism about the new Python library for web development.
  • Many users appreciate the simplicity and flexibility of FastHTML, noting its potential for rapid development and ease of use.
  • Some developers express concerns about the integration of HTML generation within Python, fearing it may complicate front-end development and violate separation of concerns.
  • There are comparisons to other frameworks like Dash and Streamlit, with users questioning how FastHTML stands out.
  • Several comments highlight the need for better documentation and examples, particularly regarding advanced features and testing.
  • Overall, the community is eager to explore FastHTML, but there are calls for clarity on its long-term viability and scalability.
Link Icon 80 comments
By @jph00 - 4 months
Hi all. Jeremy here -- I created this project. Thank you @bpierre for sharing it!

I wrote my first web app ~30 years ago, and have built some pretty big projects, including founding fastmail (written in Perl) and leading the first major production version of Kaggle (written in C#). Frankly, I've enjoyed creating web apps less and less over the last few years. So I decided to try to create something that I'd personally enjoy using. I like coding with Python, it's got a great ecosystem, and deployments like Dropbox and Instagram show that it can scale right up.

FastHTML brings together Python, hypermedia based apps with HTMX, the powerful and flexible ASGI/Uvicorn/Starlette trio, a new Python component system called FastTag (FT -- based on many similar projects from the functional programming world), an API design inspired by FastAPI, and a few more bits and pieces into something I've now written around a dozen apps with. I'm really loving it!

I hope some of you get a chance to try it out -- let me know what you think.

By @Yenrabbit - 4 months
I've been using this while it was in development and it's a pleasure to work with. Highlights for me:

- Incremental complexity - starts super simple and I can add stuff as I need it. I don't like frameworks where step 1 already leaves you with lots of files and a bunch of things you need to know.

- Easy escape hatches. I like some of the simpler demo/dashboard things but inevitably hit a ceiling that suddenly requires a lot of hacking to get past. Since FastHTML is a lot more transparent it's very easy to go right in and do something with JS or mess with the request or make something custom. So you're not stuck with only the widgets a framework gives you or anything like that.

By @kylerush - 4 months
Nice work! I think the Python community definitely needs something like this. The thought never occurred to me to use HTMX w/Python for both server rendered HTML and dynamic behavior in the browser.

I have a few questions for you.

1. Why do you recommend conda or pip and not uv? Is this because the plug and play deployment platforms are configured to use pip?

2. Do you plan to make this “batteries included” like Django? E.g. it looks like currently you have to manage database schema and migrations outside of FastHTML.

3. Perhaps not in scope for this, but it seems to me making LLM API requests in the FastHTML backend could cause some scaling problems since these i/o operations can take a really long time and tie up the same threads required to render web pages. Any thoughts on that?

EDIT: Added third question.

By @throwaway89988 - 4 months
Hi Jeremy, congratulations for the launch and the website looks very nice indeed.

I am honestly mostly interested in your reason, to mix HTML/CSS generation into the Python code. Disclaimer, I am very biased towards separation of concern and like my backend just returning JSON/XML/whatever data and a templating system. Of course this increases the ramp-up time to learn a framework, but then it is IMHO very powerful, flexible and fast.

Could you perhaps elaborate on your choice for FastHTML and what tradeoffs you see?

By @sadlion - 4 months
I wasn’t expecting to see Jeremy when I opened the link. I’m a long time fan of his work and have been recently playing with Claudette. Claudette is written using a Jupyter notebook in a literate programming style. Seeing Jeremy deconstruct problems and build solutions from first principles is always amazing. I have experience with multiple JS frameworks and I am excited to try fasthtml. Thank you Jeremy for all your contributions.
By @lyjackal - 4 months
I’ve been trying out fasthtml as a more scalable prototyping tool for a side project. I’ve really enjoyed using it! I tried gradio first, but 1. didn’t like the look, and 2. You can’t really go off the beaten path. So far I’ve really enjoyed working with fast HTML and htmx. Honestly my biggest complaint on working with “Python-only” dev has been the CSS. I wanted to give the app an easy, but unique/customized look. Most CSS libraries expect to be part of a JS based build pipeline for any type of customization. bootstrap still requires scss customizations, tailwind is its own thing of configuration, pre-processors and tree shaking. Really wish there was a robust css library that relied on css-variables to customize. There are a few but they’re relatively anemic. Anyone know of any good options out there that would be a good fit, or did tailwind just eat everything up?
By @polyrand - 4 months
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/

By @ianbutler - 4 months
I was talking with my cofounder today about how we'd likely need to become a multilingual platform once we eventually take on more than backend applications and I'm glad to see projects like this. They give me hope that we won't have to make that jump.

I'm really excited to give this a try seeing as this should just run on our cloud with minimal to no changes given the premise.

I know of one or two other projects like this in the ecosystem, but this approach seems the most promising so far.

Also I'm not sure when Jeremy finds time to sleep given all the other exciting work from Answer.AI. and his various courses :P

I recently implemented deepspeed + qlora in a finetuning library and that was also entirely based on the fsdp implementation him and his various associates wrote.

So he really is just making great contributions all over the place.

By @hamelsmu - 4 months
I am a python developer who has been envious of modern application dev frameworks & typescript, but never had the time to invest in another stack. This is so exciting. I suspect this might be catalyst that empowers more people to ship stuff
By @chompychop - 4 months
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.
By @jll29 - 4 months
While the design of it violates the separation of concern principle (keep data and code separate), I have to say this is most impressive, thanks for writing and sharing it.

I have always been reluctant to accept any boilerplate code (esp. such that one cannot fully understand) in my codebase, and this does not have ANY! All the sample code looks absolutely beautiful, so I will give this a try for my next Web app projects.

By @durraniu - 4 months
This looks really cool. I have experience with shiny apps in R, and Python has a shiny package too now. FastHTML looks a lot like Python shiny without routes. I think both of these frameworks are great for people with no web dev experience. It would be great if there is some discussion of htmx and why it is used in the tutorials section of FastHTML docs.
By @BerislavLopac - 4 months
I'm personally always confused with those batteries-included frameworks like Django, FastAPI and similar. Sure, they might be easier for a beginner to quickly whip up a simple Web site/app, but in my experience as your requirements grow they quickly start getting in your way.

Starting with more flexible initial components (e.g. Starlette) and adding batteries (SQLAlchemy, Jinja2, HTMX...) as needed allows for a sensible evolutionary approach and prevents painting yourself into a corner with early decisions.

By @jeanlucas - 4 months
Hey, just looking this quickly, the ideal case are for python developers that don't use Flask or Django? I'm a web developer for just 10 years, and I like seeing HTMX being applied, but I don't see why I should consider adopt it.

Maybe I'm not the ideal user, but would like to know from you who do you think this is for.

By @skeledrew - 4 months
This looks really nice. I'm just wondering how it would combine with Pyscript, which I've been watching for a good while now. As a primarily Python user wanting to do some web dev, I'd rather not touch JS/TS at all, beyond importing 3p packages.
By @darkteflon - 4 months
Oh my goodness. I like to keep things boring where possible and swore I would never stray from Django + HTMX + Django Ninja, but I am exceedingly tempted to use this in an upcoming project. Lovely architectural choices - bravo!
By @jaehong747 - 4 months
FastHTML is an impressive and innovative idea. It seems like a web development tool similar to Streamlit, but with more precise control.

FastHTML's concept led me to consider a feature that allows direct deployment of PyQt code as web services, even without HTML knowledge, like "PyQtWeb."

PyQtWeb > FastHTML > [Streamlit, Gradio]

By @ptero - 4 months
Thank you! I'm another engineer who uses python (and C, Matlab and a few others) and whenever I want a web app I end up with some Rube Goldberg style contraption. Looking forward to trying your software.
By @harel - 4 months
First, I welcome any project that enriches a software ecosystem, and this project no doubt does just that. However, I have two points which will deter me from using this (or any python->html/js framework) in a commercial production project:

1. It silos front end development in Python world. It might be great if your entire team are and always will be Python devs, but what happens when you want dedicated from end developers? What happens when you need to deviate out of what the framework gives you in a front-end context? What happens when you need to eject from "python" into a dedicated front end environment? All your front end code is now written in Python. Worst, you now might even have JavaScript code embedded inside Python code. I keep hearing "CoffeeScript" in the back of my mind...

2. Any python project using FastAPI (which is fantastic), flask, etc. and is growing in scope, will ultimately build Django. For example, FastAPI (which is great), has SqlModel (which is awesome) which makes SqlAlchemy less sucky and more like Django. Start to factor in all the other batteries we got used to getting with Django, and it starts adding up. If the project is smallish in scope and well defined to know it will stay such, sure it's a valid and excellent choice. The same applies here - unless batteries are included, or this is (as suggested in a comment) available as a Django app, you'll end up building Django.

By @vikaspooddar001 - 4 months
Hey fastHTML team, congratulations on first public release of fastHTML. I just want to point out fastHTML, fastAI and fastAPI can bering together to form a python stack for training, deploying ml application python native fashion. The stack will known as faststack
By @pokipoke - 4 months
I haven't seen such bad Python code (fasthtml repo) for a long time. It feels like its written in 2008 using Python2
By @openrisk - 4 months
What would be cool++ (and potentially very impactful) is if somebody builds a python/htmx native "wordpress" on top of this. The Python ecosystem offers django/wagtail and some other CMS like options but imho they have not (yet?) taped into the vast potential of the Python ecosystem once the algorithmic / data science part is natively integrated with cms type web apps.

The .ml domain extension may be exactly the placeholder needed :-)

By @bartron - 4 months
I have been looking for something like this for a while and am very excited to see this project.

I am currently settled on [ludic](https://getludic.dev) which is very similar to my eyes and has been discussed here [1]. The developer is responsive and the repo has a comparable number of stars to FastHTML on github.

Ludic's big feature is type-guided-components[2] that allow compile time checking of the compatibility of components forming a structure---and autocomplete while writing. So for example the component `WithSideBar` from the catalog[3] needs to contain a `SideBar` component and a list oof other child components. It seems elegantly put together too.

Looking forward to trying out FastHTML.

[1] https://news.ycombinator.com/item?id=39776199 [2] https://getludic.dev/docs/components [3] https://getludic.dev/catalog/layouts#sidebar

By @pietz - 4 months
This looks cool and I will check it out but I'm also quite happy with my tech stack.

I started to couple my FastAPI backend with native Jinja2 templates and noticed that I hate Jinja2 with passion (no disrespect). I tried HTPY which seemed great but this Python abstraction of HTML just felt weird and I found myself converting HTML to HTPY all the time. I even created a GPT for it. Then I found JinjaX and noticed that this hits the nail on the head for me. It's a Jinja2 preprocessor that allows the usage of components instead of the weird extends and macro syntax.

I'm happy to look at FastHTML but I'm not sure what type of benefit I can expect.

By @pzo - 4 months
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?

By @amai - 4 months
What is the advantage over e.g. https://streamlit.io/ ?
By @giancarlostoro - 4 months
One thing I'm dying to see is a Python template engine that builds to WASM. This is the killer feature of C# for me right now. Blazor removes any need for me to ever touch React or JavaScript ever again. I think if done as a stand alone template engine, then every web framework could benefit from it, including this one.

I just might have to research.

By @ostwilkens - 4 months
"What's the FastAPI of frontend?" - A tweet I wrote 2022 I'll give this a go for my next project!
By @vaylian - 4 months
This looks really impressive. What is the idiomatic way to test webapps created with FastHTML?
By @CodeCompost - 4 months
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?

By @DonnyV - 4 months
Python isn't really known for its speed. The syntax seems like a nightmare. Very magic syntax like Ruby on Rails. I feel bad for the person that will need to maintain this in 5 years when its grown to large.
By @throwaway86586 - 4 months
Did you try Reflex.dev? Any opinions on it?
By @EternalFury - 4 months
Python is fast compared to something? Maybe fast enough to generate HTML.
By @langcss - 4 months
Are there any UI libraries developed for this? Or other ecosystem stuff.
By @hopfenspergerj - 4 months
I'm looking at the very first example, and I'm a little confused. The function `home()` displays a list of messages, but they aren't passed into `home()`. Instead, `messages` is basically a global variable, and some other functions can append messages to it. Then I went looking at some more examples, and I see this pattern repeated. Is this how you're supposed to build webapps with this package? How does it isolate the list of messages for different users?
By @looop101 - 4 months
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"

By @ammar_x - 4 months
Hi Jeremy, congratulations for the launch.

How does this compare to Dash?

I've used Dash for many applications, so I'm wondering what are the advantages of FastHTML?

By @65 - 4 months
I looked through the examples and man is the code ever unreadable to me. If you're using HTMX why not just use Flask or Django and use much easier to read templating engines?

I always found trying to read function calls as markup to get unwieldy. Realistically people are most likely going to either be using Python with traditional templating engines or Python as an API with a JS framework on top.

Good luck to this project, perhaps it isn't for me.

By @2wrist - 4 months
It's you!

Just wanted to say, nice job, love how much work has gone in to this and especially the site/docs to help people get going.

By @anoojb - 4 months
Thank you for building this :-)

Can you address the longevity question? Do you think you and/or other highly motivated/enthusiastic folks could be maintaining this project for the long-term?

Or should we only be building projects on top of this framework with a 2-3 year time-frame?

By @smrt - 4 months
Jeremy, this is awesome. I hope this catches on
By @hum3hum3 - 4 months
I have used and liked FastHTML although I was going in a different direction (not very modern and no javascript) https://github.com/drummonds/lofigui. It works well.
By @nurbl - 4 months
Looks neat! It seems at least superficially similar to https://github.com/getludic/ludic which I quite like too.
By @Art9681 - 4 months
I love everything about this. I have been using HTMX heavily for a side project and glad to see it used in this project. Is fast.ai hiring? I would love to make contributions to their mission.
By @iamcreasy - 4 months
This looks very interesting.

Is it possible to inject custom JS wherever I want in the app? Also, is the generate html/css/javascript readable as the application scale up?

By @evanhughes - 4 months
I'm so going to use this for my next project
By @OutOfHere - 4 months
By @bruce343434 - 4 months
Whoah, this comment section is surreal. People really aren't bothered by the propensity for runtime errors in python? How slow it is? That it has no good features for managing complexity in large codebasea? The fact that abstractions like these pretty much always break, or at some point you want to do something more outside of the box, and you have to put in a monumental effort?

I'm working on a Django+graphQL app and I'm basically considering buying a farm at this point. Python is really not the right language.

By @crimsoneer - 4 months
Honestly, Jeremy and the Fast.ai team really deserve some kind of award/medal of honour/general sainthood at this point.
By @ultrasounder - 4 months
Came here to post this but HN hivemind beat me to it. Can't stress this enough. This Coupled with generative AI for code generation will make the barrier to entry sure low. Time to migrate my bootstrapped Flask/JINJA2 templates website(www.voxtodo.com) to this shiny new.
By @rekabis - 4 months
Will there be a FastHTML.NET, using C# and F# instead of Python?
By @Nathanael_M - 4 months
Weirdly topical. Currently investigating Django as a backend for product redevelopment. I'd like to avoid a fully separate frontend app, because frankly it's overkill. I was thinking of HTMX, Alpine, and cobbling together some component-esque template organization. I'll check this out!
By @leke - 4 months
This looks like a lot of fun, but I wouldn't probably have any need for it. I currently find an AI prompt in the code editor, along with AI code completion, a fantastic way to rapidly code. Mixing Latte template files with a minimal framework like Flight PHP, and HTMX makes me just as productive. I guess python devs would be more appreciative though.
By @098799 - 4 months
Yikes. I'm going to follow this one cause it's right up my alley, but I'm worried I will absolutely hate the process if some standards don't change, e.g. having to have multiple functions called "get()" for them to be a GET request is going to drive mypy/flake8 mad.
By @bapetel - 4 months
Python to Python HTML to HTML JS to JS CSS to CSS

That's it, no more complexity

By @ironfootnz - 4 months
I think this is a very useful framework to write about on Wikipedia on how not to use HTML in your python code. There's a canonical reason on why we shouldn't.

Readability, reusability ... the list goes on.

By @wodenokoto - 4 months
How does this compare to streamlit?
By @hyuuu - 4 months
does this integrate with django?
By @truth_seeker - 4 months
will it compatible with pypy now or in future to speed it up ?
By @nothrowaways - 4 months
Cool domain, fastHT.ML
By @sweca - 4 months
Fantastic design!
By @yashbindal - 4 months
This is so cool
By @lofaszvanitt - 4 months
This is the future, right after React.
By @Iacjr - 4 months
IS It work in termux?
By @kissgyorgy - 4 months
Frameworks like this are really next-gen, but I wish people would think in terms of the bigger Python ecosystem and not just their own framework. This is about the fifth web framework which are not compatible with each other: Streamlit, ReactPy, FastHTML, Dash, Shiny, etc..

I created a truly reusable Python component framework which is just a a string generation library and can be used for ANY existing Python web framework and even more: HTML, XML, RSS, SVG generation, even robots.txt generation as a silly example. I use it with Django and HTMX but it doesn't have an opinion about anything how should you use it. If you pass a Component to Django HttpResponse instead of a string or template, it just works.

I guess I should just write some documentation and release it before the 6th one of these appears :) so we ALL can collaborate with the same API on a bunch of Component sets like Twitter Bootstrap or Material components!

https://github.com/kissgyorgy/compone

By @fraugdib - 4 months
Dude - Fastmail was the shit back in the day
By @MemphisTrain - 4 months
I have no interest on magical sugary functions that make something quick. The modern reactive concept doesn't impress me. What I want is a very well thought-out set of tools that allow me to do anything and everything, because I will be refactoring and fine-tuning my functions a lot, and I will do it my own way, I don't need some automatic shortcut which is not going to help me if I can't modify its full functionality. (I don't mind decorators if they make sense)

I want to see how I can manually wire and create anything I want, is what I'm saying and this demo felt like it capitalized on how fast you can do very simple functionality with a couple of functions, which was a let down.

I want to see how I can route (GET/POST), create a database schema, use the database, use CSS (this is very important) yet what I saw was a simple calls to some database store, and no CSS examples. And "a single python file" sounds unrealistic since anything complex enough is going to be split into a series of files. Maybe I'm not the target audience.

I felt very comfortable using Flask recently because it allowed me to do anything I needed.

I do like the idea of building and manipulating HTML elements through python, so hopefully something good comes out of this.

By @bbminner - 4 months
I have been reading about these kinds of projects for some time, and even prototyped my own a while back, but one question that keeps popping up is - all these python abstractions over how html and js and dom interact tend to be extremely leaky. So, as soon as you go beyond the simple todo example, you need to manage BOTH the standard browser model and the abstraction model in your head, and all the funny ways in which they interact. Do these kinds of libraries prove useful for people beyond their authors (who have a good mental model of a framework in their head anyway because they are developing it)?
By @rasmus1610 - 4 months
We have 75 comments and no one has mentioned the awesome domain name? c'mon :D

really excited for this project. I hope it catches on. It has some really nice ideas in it, like all the stuff jeremy does!

By @tiffanyh - 4 months

  <!doctype html>
  </!doctype>
OT: is there a reason to open/close the DocType at the beginning of the homepage source?
By @cynicalsecurity - 4 months
People do all kinds of crazy useless things just in order not to do it the proper way in PHP.
By @jwmoz - 4 months
This is not the way to do html with python.