Python - as you may know already - is a language that is becoming more and more popular when it comes to building web applications. As it is so, there are also numerous web frameworks to choose from. In this article, we will look at two popular micro-frameworks - one of them is Flask - which has been on the market for quite some time, and the other one is FastAPI - basically a newcomer to the Python tech landscape but already an important one. After reading this piece, you will learn about both main characteristics- hopefully, it will allow you to make a better choice regarding the framework used in your application.
What is Flask
Flask is basically a web framework, and a web framework is a collection of libraries and modules that helps software engineers create web applications without having to worry about low-level things like protocols, threads, etc. Flask is a Python framework based on the Werkzeug WSGI toolkit and Jinja2 template engine. It is also referred to as a micro-framework - allowing you to keep the application core simple and efficient to scale it quickly. As a micro-framework, it does not offer an extensive set of features like a full-stack framework (such as Django). It also allows lower resource consumption.
What is FastAPI
Similar to Flask, FastAPI is also a micro web framework. It’s great for creating fast APIs (especially for serverless applications). FastAPI is based on Pydantic and type hints to validate, serialize, and deserialize data. It supports asynchronous programming and can run with Gunicorn and ASGI servers for production.
Main benefits of Flask
Let’s take a look at the main benefits of using Flask for your Python projects:
- There is a built-in development server"
The fact that you can simulate the production environment makes it very easy to test your application without putting it on production - and as we know - this also saves time, which is a precious resource:)
- Unit Testing
Flask supports unit testing, making it easy to simulate different scenarios, test your application with ease, and be sure it can withstand the production environment.
- Low entry point
Flask is easy to understand and easy to start with, so it’s a perfect choice not only for experienced engineers but also beginners.
Flask is extremely easy to scale - it allows the engineers to freely add new features on the go - which again - saves the development process precious time.
- Great community
Flask has been around for quite some time, making it well documented and supported throughout the community.
Despite being a micro-framework, Flask very well supports most of the security vulnerabilities, such as XSS, CSRF, or JSON security protocols. There are also numerous extensions that can improve the security even more - but obviously, we wouldn’t recommend them without doing proper research beforehand as they may decrease the performance.
Main benefits of FastAPI
Now let’s take a look at what FastAPI has to offer.
FastAPI is way faster than Flask, and it’s actually one the fastest web frameworks for Python. The only framework that is faster than FastAPI is Starlette (fun fact - FastAPI is actually built on Starlette).
It used to be hard, but since Python 3.4 Async I/O was added. FastAPI allows out-of-the-box concurrency. You will also avoid creating event loops or async/await management. As you may imagine, this will eventually impact the efficiency of your application.
- Documentation support
FastAPI generates really cool documentation that is easy to read and use by frontend engineers and simplifies API endpoints’ testing. The documentation is browser-based, and it’s based on Swagger UI.
- Dependency injection
FastAPI comes with a built-in dependency injection solution. It ensures that classes are not directly dependent on each other. Thanks to that, the changes in the code are easier to make. The modularity of the code is also increased, making it easy to scale. The URL path operation function allows engineers to declare relevant dependencies.
- Seamless central exception handling
To do exception handling, you just need to use the @app.exception_handler annotation or app.add_exception_handler function to register the response for an Exception, which will be managed automatically by FastAPI.
- Validation of data
Last but not least is the validation of data. It detects wrong data types and returns proper exceptions in JSON. It uses the Pydantic module to simplify the process, making the code less prone to errors.
Drawbacks of Flask
Now that we gathered both frameworks’ benefits let’s look at potential issues/drawbacks. Let’s start with Flask.
- Single Thread
As you may have realized from what's been written so far, Flask is a single-thread framework, meaning that requests are handled one after another. I don’t even think I need to explain why it’s bad:)
- Use of Modules
Flask uses modules, and those are usually third-party solutions, making it somewhat of a security threat and yet another thing to think about within the development process.
- HTML-oriented web framework
Flask is not a framework whose main objective is to write APIs. It’s a web framework, so there are no standard ways of using it to write APIs.
- Database migrations support
It’s hard to keep track of migrations and versions of the databases. There are some additional third-party solutions, but it’s some additional effort and will require additional libraries for each database.
- Session management
There is no built-in session management, so it means you will have to implement one yourself. It’s no rocket science but still requires additional work.
Drawbacks of FastAPI
The last thing we need to take a look at is the drawbacks of FastAPI. There are not a lot of those, but it’s still important to take a look at them :)
There is no built-in security system. Instead, you have to use fastapi.security module to manage it.
- Small community
FastAPI is relatively new (in comparison to Flask), so the community and amount of materials are a bit smaller. But this will surely change over time, so it’s not really a huge issue.
So which should you choose?
Now that we’ve gone through the upsides and downsides of each framework, it’s time to decide which one you choose for your project. Actually, both of them are pretty easy and quick to set up, so there need to be other factors that need to be considered. It all depends on the purpose of the application.
FastAPI is much better when it comes to performance and speed. So choose it when you’re building an app that is expecting heavy traffic. It’s also way better for creating API, so if you’re building a backend whose main purpose is to serve as one, then you should definitely choose FastAPI.
Flask, on the other hand, is better when it comes to simpler applications. It’s very easy to use and scale - even if you are unsure which direction your app will take. There is also built-in security to make things even easier.
The final decision should naturally be based on your requirements, so the examples mentioned above are just a short way to present how your decision process should go. As always, when it comes to engineering, there are also business-oriented issues - for example - if your app is already built on Flask, it will probably be a better choice to build a new API in the same framework.
If you have any doubts or questions, feel free to reach us - we have vast experience in both of mentioned frameworks (and more), so we can help you make the right decision for your business.