234 points · by memorable · 4y ago · 87 comments
Happy to see people continue to find my tutorial useful and relevant. If you have any questions, I'll be happy to answer.
Now this is my personal opinion, but you cannot say the same about FastAPI. Even though the ecosystem of asyncio compatible libraries has grown, it is still several orders of magnitude smaller than what is available for standard non-async Python. So in my view, in terms of access to 3rd party libraries Flask is a better choice.
Your Flask tutorials are my webapp I Ching. Thanks a bunch. Just about to launch another Flask webapp in the next few weeks, and your tutorials are invaluable.
Oh! Before I forget - flask-migrate! Lifesaver! Thanks Miguel!
Thanks again
One thing that people often complain is that it is hard and/or tedious to build tables with data, which is one of the building blocks most admin pages need. Last year I wrote a blog article where I show how I build tables in my Flask apps: https://blog.miguelgrinberg.com/post/beautiful-interactive-t....
Thanks to Miguel (and also official Flask docs) I have some Flask Apps running non stop for a few years by now.
Flask is extremely easy to write in regards that the mental model is easy to follow. Sure there is magic there but the abstractions seem "non-leaky". By contrast I can't get into Django because it seems too much magic.
The worst part of using Python for back end was deployment. Setting up WSGI server to host Python apps was a bit of a pain.
https://flask.palletsprojects.com/en/2.1.x/deploying/
I think I went with Gunicorn but there was a bit of learning curve there. I am not even sure if that was the right choice
https://cloud.google.com/run/docs/quickstarts/build-and-depl...
That being said, Django is great, and so is Python :)
I was a Data Scientist and I superficially used Flask to build an internal matching tool between internal products and competitors product (after an ML model). Then I started the Mega-Tutorial to build a small app to track our foosball matches and run a team tournament. Eventually, I wanted to accelerate my learning and joined a bootcamp (Le Wagon) where I learned Ruby on Rails from scratch. My Flask introduction with this tutorial was really helpful to understand things on almost all layers.
I always recommend this tutorial to people who come from Python and want a sneak peak to web development. I am very grateful for Miguel's work and for sharing it.
Try Django instead. If you read my comment history you'll see that I learnt this the hard way.
Flask is scaling really well for us and it’s flexibility is far better than Django’s opinionated approach.
While you get up and running faster with Django, in the long run you spend more time fighting it, whereas with Flask you only spend time on stuff that you actually need.
I even recommend people not to install too many flask extensions, just use Django if that is how you like your development. Flask is much better when you just build for it - It takes longer to deliver, but you'll be enjoying that project for years.
Flask is for longevity.
Haven't tried Flask myself though
I have some tips I use in all my Django projects:
1. Split your settings.py in multiple files
This is inspired by Elixir's compile-time config per environment/release/etc...
Instead of a huge settings.py, I have:
settings/__init__.py : Import the correct settings
settings/core.py : The stuff that Django generates and that you touch almost never
settings/base.py : The basic settings of your project (your apps, middlewares, ...)
settings/envs/dev.py : Development settings
settings/envs/prod.py : Production settings (read from env vars, the 12 factors app way)
settings/envs/test.py : Test suite settings
See my gist as an example for the `settings/__init__.py`: https://gist.github.com/linkdd/4aac2c2efc4a51af6ca4b05f395de...2. Separate your business code from your Django apps
In most project, I have the package `myproject.lib` which contains all the business code, easily testable and mockable. Then I have the Django apps in `myproject.apps.<appname>` which imports from `myproject.lib`.
This allows you to make your views quite small because all the logic is not there.
3. Most of the time, you don't need class-based views.
A small function which call your business code and render a JSON document, or an HTML document is more than enough. Django provides many function decorators that are easier to reason about (login_required, permission_required, require_http_methods, ...).
4. You might not need an API
Server Side Rendering is coming back into trends, huge SPA that are heavy on the client's resource are becoming less frequent. Then, why would you need a REST/GraphQL API?
You can work with HTMX, Django Forms, small views that call your business code and return some small rendered template, then sprinkle some Alpine.JS (or jQuery) on top of it, and you get your SSR SPA that scales well enough.
5. Deploy with gunicorn and whitenoise
By default, Django won't serve staticfiles in production, you need to distribute them via a CDN. Which is a bit more complicated to deploy. Instead, use Whitenoise to force Django to serve staticfiles but with the correct HTTP cache headers so services like Cloudflare can take over after the first request (usually done by your E2E test suite by the way).
Then you only need one Docker image, one gunicorn, and if you deploy in Kubernetes, one Ingress resource.
6. Use django-anymail
This is, by far, the best library out there. I usually make a mailjet account, add my API key to my settings, and use the mailjet backend of django-anymail. No SMTP setup required.
All of those tips might seem obvious, but they get you far, very far. Also, IMHO the urls.py from Django is vastly superior to the route decorator from Flask.
TL;DR: Don't fight Django, embrace it.
That's my impression at least.
It worked ok for internal apis not open to customers, but even then Django was much better as just setting up the admin side provided a lot of value to the business (for free).
All forgotten now but wasted a huge amount of time glueing things together that came for free with Django.
Flask is fantastic and I would recommend it to anyone starting out developing webapps.
I built a json ui for my flask apps the other day - Jsonify - https://github.com/xzava/jsonify
While I am a flask fan, a lot of good things can be said about fastapi.
I did his flask tutorial a year + ago before I started learning react. I'd love to see the workflow for developing a backend Flask API first (without Jinja/render_template). It seems less straight-forward than FastAPI which I am using because I think it's simpler to develop APIs first (ala the name)
There is more useful and battle tested solutions out there for flask than fastapi (due to it's obvious time in the market, but still).
It's also interesting to check number of github issues for both frameworks (14 vs >1k).
Nonetheless fastapi is great.
FastAPI is async web-framework for Python comparable [0] to Flask.
Read it as Flask is "old" and "slow" (i.e. synchronous and needs multiprocess deployment), whereas FastAPI is really fast, single threaded and async.
[0] IMHO comparable only at "hello world" level as FastAPI is much more and uses a lot of goodies of newer Python versions (typing, asyncio, etc.)
> synchronous and needs multiprocess deployment
Async python still has the issue of the GIL, you still need multiple processes to scale out with async.
The only seriously useful thing async gives you is the ability to run multiple async IO operations in parallel while processing a single request. But most async request processing is still being written in a somewhat sync way, "awaiting" each IO operation within a view/request processing function.
I tend to only use async for long running requests (web-sockets, SSE, long polling), or if I have a view that is calling lots of slow IO which doesn't depend on each other. Think views that are calling multiple REST APIs, and multiple DB requests, you can do them all at once. That is incredible rare though, usually requests do depend on each other and so have to be run sequentially.
A good starter to mess around in is this project https://github.com/tiangolo/full-stack-fastapi-postgresql
Surprisingly enough, it's terribly easy to put together a REST API with Jooby. I was expecting a lot of arcane tricks to set things up, but it's one of the less.verbose frameworks out there.
I wonder why it's adoption rate is so low.