

Abandoning Flask for FastAPI
source link: https://medium.com/python-in-plain-english/abandoning-flask-for-fastapi-20105948b062
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Abandoning Flask for FastAPI
I’m sorry Flask, but FastAPI is so much better.
All across Medium and social media, my feeds have constantly been posts about why people should use FastAPI for writing APIs in Python. Seeing this, I was a little shocked, because I was under the impression that Flask and Django were the major Python API frameworks. For the last few years, Flask has been my go-to framework for writing APIs. With all this hype surrounding FastAPI, I decided to give it a try.
Giving it a Try
With the discovery of FastAPI, I decided to revisit an API that was built a few months ago to handle application logging. Essentially, the plan was to convert this logging API from Flask to FastAPI. The first step in this process is to install a couple of Python modules.
pip3 install fastapi
pip3 install uvicorn
pip3 install pydantic
After installing the modules, I refreshed my memory on the endpoints for the previous version of the logging API.
- GET request — /loggingapi/v1/logs
- POST request — /loggingapi/v1/log
The GET request is pretty straightforward. It gathers all logs for a given application ID. Right now, I am dealing with a relatively small amount of logs, so returning all records will be fine. However, in the future, this will probably get changed to date parameters to limit the number of records being returned.
The POST request is even easier than the GET request. Essentially, its purpose is to add new log records to the database. It takes an object parameter that looks similar to this:
{
"appId": 7542a72b-b6eb-4b9d-a672-a8d82ad0dadf
"message": "This is my log message."}
With my memory of the endpoints refreshed, code writing could begin. The first bit I wrote was a new initialization of FastAPI with some metadata.
After that, I started writing the GET request.
Making comparisons to Flask, there are quite a few differences. Instead of having to use “methods=[“GET”]”, this has been turned into a function that contains the endpoint URL. The other major difference is that all endpoint parameters whether they are passed through the URL or passed in the body of the request must be passed to the endpoint function. The “storage” module contains the functions to query the database. Not major changes were required for the transition to FastAPI.
Since I was getting a little itchy to try out the new endpoint, I decided to start up the API in development mode. In Flask, when not using a virtual environment, it was customary to use the “python3” command when running an API. However, this will not work with FastAPI. The “uvicorn” command is an ASGI server implementation. The command I used to run the API was this:
uvicorn app:app --reload
Now that the server had the API running, I opened up Postman, entered the endpoint, an application ID, and then sent the request. Unfortunately, the formatting of the output looked a little funny. I soon discovered that FastAPI sends responses as JSON by default. Therefore, I needed to remove the “json.dumps()” in my “storage” module. Below is the output of the second attempt to send the request.

With the GET request looking pretty good, I wanted to check out the built-in swagger documentation. Not going to lie, I thought it was a little too good to be true, but was completely blown away when I navigated to the documentation page.

Before doing any more experimentation, I really need to finish the POST endpoint. Unlike the GET endpoint, this one takes a JSON object in the body of the request. In Flask, I was accustomed to using “request.json” to get the data. However, FastAPI uses “pydantic” to create a class object. Here is the one I created.
Like the GET endpoint, the log parameter needs to be passed through the endpoint function as type “Log”.
At this point, I was able to run the test for the POST request in Postman.

Conclusion
I think it is safe to say that I will be using the FastAPI framework for all my Python APIs. Even though I was just converting from Flask to FastAPI, I noticed a considerable amount of time savings while writing out the endpoints. I love how asynchronous tasks are integrated by default. Flask does have support for asynchronous tasks, but the “celery” module needs to be imported. Same thing when it comes to sending responses in Flask (“json” module is needed). FastAPI by default sends responses as JSON objects. Finally, the swagger documentation is amazing! To be honest, I never tried to get swagger working with Flask. Again, FastAPI supports it by default and makes it so easy to configure and update. Feel free to leave a comment about what you like/dislike about FastAPI. Until we meet again, cheers!
Recommend
-
30
Dysfunction has consequences. So does infighting between teams, as well as a general lack of interest and excitement about building on top of a technology protocol. The BCH network has begun to pay the price for its decision t...
-
15
To the surprise—and likely consternation—of BSD fans everywhere, FreeNAS vendor ixSystems is building a new version of its core product, TrueNAS , on top of Debian Linux....
-
12
Escape From ISS...
-
8
请不要把 Flask 和 FastAPI 放到一起比较 发表回复 The war has begun. Let’s end the war between Flask and FastAPI. 去年在知乎上看...
-
13
PyCon US 2021: FastAPI Seems Good, so Why Don’t We Build Something Similar For Flask?李辉greyli.com
-
6
Abandoning GitHub I do not agree with GitHub's unauthorized and unlicensed use of copyrighted source code as training data for their ML-powered GitHub Copilot product. This produc...
-
6
FastAPI or Flask?从用户出发,才是王道 ✏️ 编者按: 前段时间,Milvus demo 使用的服务框架从 Flask 切换为 FastAPI,这一改动引发了社区小伙伴们讨论:为什么要改服务框架?改了以后有什么好处?为了解答社区小伙伴们的疑问,Zilliz...
-
11
2020年是时候更新你的技术武器库了:Asgi vs Wsgi(FastAPI vs Flask)首页 - Python/2020-07-22
-
20
Python Web 框架三巨头:Flask、Django 和 FastAPI 2023-10-09 18:17:52 Python 作为主要语言的受访者大多将其用于 Web 开发 (23%)。作为辅助语言,Python 最常用于数据分析 (16%) 和 DevOps (14%),Web 开发排在...
-
8
Flask 已死,FastAPI 永生 4条回复 好险,刚刚要写一个新项目,执行了 pip install flask。等得无聊所以打开了知乎,突然看到首页推...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK