This content originally appeared on Level Up Coding - Medium and was authored by Pratik Choudhari
This in Flask, that in FastAPI
The python API development space has been changing rapidly. Earlier, this space was owned by Flask, which apart from its API building capabilities also boasted an integration with Jinja template engine. Hence, starting a wave of website development using flask.
A few years back FastAPI was released and it bought a plethora of features to the table, which made building APIs a relatively easy task. FastAPI featured optional async capabilities, so even if someone is transitioning from flask, they can use normal synchronous programming. It’s integration with Pydantic and Startlette are what made it shine amongst others.
FastAPI is best suited when an application relies a lot on I/O operations and due to the blocking nature of I/O operations, execution of other code snippets is blocked too. So, using asynchronous programming we can serve other requests while waiting for the long running tasks to get executed.
Next, We are going to dive straight into our topic.
1. App initialization
<script src=”https://gist.github.com/pratik-choudhari/78761b8133220dc58ff3c1aa375b98d2.js"></script>
Flask class takes __name__ as a parameter whereas Fastapi doesn’t. The __name__ in flask is mainly used for locating template and static files.
2. Creating a route
The key difference in declaring a route in these two libraries is that flask needs the HTTP method to be described as a parameter to the route function. On the other hand, FastAPI needs user to declare the HTTP method as name of the decorator.
3. Accessing query parameters
When FastAPI receives a request with query parameters it parses these parameters and passes them to the decorated route function. Flask too parses the query parameters but instead of passing them as kwargs, it makes them available in the request.args object.
4. Fetching the request body
<script src=”https://gist.github.com/pratik-choudhari/b39fcd5eea32e442de86676dc19b36d4.js"></script>
Parsing the request body as json is strikingly similar in both libraries. Unlike flask, fastapi doesn’t allow direct use of request module to access request data rather it needs to be passed to the route function.
5. Startup-Shutdown events
<script src=”https://gist.github.com/pratik-choudhari/cefa4f2371129c8124e39a579857aa70.js"></script>
Flask doesn’t provide any means to execute a piece of code based on events like starting and stopping of an app. Whereas, FastAPI does provide 2 events to play around with namely startup and shutdown event.
These kind of triggers are especially important in the case of starting a database connection on start and gracefully invalidate the connection on shutdown.
6. Data validation
<script src=”https://gist.github.com/pratik-choudhari/5ed012c863d56c59e2670ad54ab7ea41.js"></script>
Flask does not come packaged with any data validation module. FastAPI makes use of Pydantic, a modern package that enforces type hints to follow certain user defined data validation protocols.
Apart from ensuring the structure of data, pydantic also implements validators through which custom checks can be applied to ensure data homogeneity.
7. JSON response
<script src=”https://gist.github.com/pratik-choudhari/f42df6d6688edfdd168479a5ba9ae7ef.js"></script>
With fastapi user does not have to worry about JSON encoding data before returning a value, fastapi under the hood applies a JSON encoder on every return value. In flask, user should use jsonify() to specify a JSON response.
8. Handling CORS
<script src=”https://gist.github.com/pratik-choudhari/a691e4566d562b4534093e3aa8c43060.js"></script>
CORS are important while building an API that communicates with other apps from different origins. Flask does not enforce CORS out-of-the-box, it requires an extension library called as flask_cors.
In Fastapi, through its middle ware functionality, CORS are handled flawlessly. There’s no need to install any other module for support.
Thanks!
I am glad you read this article till the end. Do not hesitate to discuss your views about fastapi, flask or about this article in the comment section. See you soon :)
How common API building tasks translate from Flask to FastAPI was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.
This content originally appeared on Level Up Coding - Medium and was authored by Pratik Choudhari
Pratik Choudhari | Sciencx (2021-08-03T15:16:24+00:00) How common API building tasks translate from Flask to FastAPI. Retrieved from https://www.scien.cx/2021/08/03/how-common-api-building-tasks-translate-from-flask-to-fastapi/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.