When comparing FastAPI vs Flask, the Slant community recommends FastAPI for most people. In the question“What are the best backend web frameworks?” FastAPI is ranked 4th while Flask is ranked 7th. The most important reason people chose FastAPI is:
It has a simple but powerful dependency injection system, it can be used to handle authentication, per-user rate limiting, authorization controls (e.g. with roles), etc.
Specs
Ranked in these QuestionsQuestion Ranking
Pros
Pro Dependency injection
It has a simple but powerful dependency injection system, it can be used to handle authentication, per-user rate limiting, authorization controls (e.g. with roles), etc.
Pro Standards
It is based on standards: OpenAPI, JSON Schema and OAuth 2.0.
Pro Data validation
It validates the data using the types you declared. Even in deeply nested JSON requests.
Pro High-performance
It's based on Starlette and Pydantic, so, it's one of the fastest Python frameworks.
Pro Editor completion
It is based on Python type declarations, so, editors and tools can give great support. Including type checks and autocompletion everywhere.
Pro One of the fastest growing communities
Pro Fast is really fast (!)
It's easy to develop API based applications in Python on deadlines for Android and IOS Development.
Pro Automatic docs
It generates interactive API documentation automatically from your code.
Pro Database independent
It's independent of database or ORM, but compatible with all of them. Including relational databases and NoSQL.
Pro Async IO / optional
It's based on Async IO, which gives it high concurrency. But you can use non-async libraries and it runs them appropriately.
Pro World class documentation
It has some of the best documentation of any framework.
Pro WebSockets
Because it's an async framework, it can handle async-native protocols like WebSockets.
Pro OAuth 2.0
It has integrated support for OAuth 2.0. Including declaring required scopes per endpoint. So, you can easily integrate it with external OAuth 2.0 providers or build your own with it.
Pro Background tasks
Included support for background tasks, thanks to being based on Starlette.
Pro Supports GraphQL
Python's graphene library is included as an optional dependency meaning that GraphQL API's are supported out of the box, with no additional tweaking needed.
Pro Minimalist without losing power
Flask is very easy to get up and going, with vanilla HTML or with bootstrap pieces. It doesn't take much lines of Python to load Flask to get headers working, etc, and since it's all modular you don't have to have something you don't want in your application.
Pro Lots of resources available online
Flask is one of the most popular Python web frameworks, if not the most popular one. As such, there's plenty of guides, tutorials, and libraries available for it. A large number of important Python libraries, such as SQLAlchemy have libraries for Flask, which add valuable bindings to make the development process and the integration between these libraries and Flask as easy as possible.
Pro Extremely easy to build a quick prototype
Even though it's pretty minimalistic out of the box, Flask still provides the necessary tools to build a quick prototype for a web app right after a fresh install. With all the main components pretty much packed in the flask
package, building a simple web app in a single Python file is as easy as it gets.
Pro Very flexible
Flask gives developers a lot of flexibility in how they develop their web applications.
For example, the choice of not having an ORM, but instead choosing one suited to the task, or another area where Flask gives a lot of options to developers is the templating. They can use Jinja2, Flask's default templating language or choose from a number of different templating languages they desire.
Pro Great documentation
The official documentation is very thorough and complete. Everything is explained in-depth and followed by extremely well-explained tutorials that tackle real-world problems.
Pro Able to use ORM or "true SQL"
Cons
Con Smaller community
Since FastAPI is relatively new, its community is smaller than Django Rest Framework. But it can grow with time.
Con Not async-friendly
Flask is explicitly not designed to handle async programming.
Con Setting up a large project requires some previous knowledge of the framework
Setting up a large project with Flask is not that easy considering how there's no "official" way of doing it. Blueprints are a useful tool in this regard but require some additional reading and are a bit tricky to get right for a beginner.
The lack of some defaults can also be problematic. Having to choose between different libraries for a certain task is never easy, especially if you have never worked with Flask before.
Con Threadlocals and globals used everywhere
The default way of creating applications in flask makes it hard to use reusable and clean code.
Con HTML-oriented, not API-oriented
Not necessarily designed for making APIs, though that is possible