Publish AI, ML & data-science insights to a global community of data professionals.

FastAPI versus Flask

What is all that excitement? Don't we already have Django?

Photo by Tharoushan Kandarajah on Unsplash
Photo by Tharoushan Kandarajah on Unsplash

Recently, it seems to me that there has been a buzz around FastAPI as an alternative Python web framework. I was never that fond of Django because it seemed so heavy for my particular use cases, but I always loved Flask because it was quick and easy to build. So now we have a framework that dares to call itself FastAPI, and naturally, given my love of sports cars, I just had to do a project and, well, take it for a spin!.

How I use Flask

Since I have arrived at the ‘web framework dealership’, it might be best to describe what I usually drive, and that reflection can provide the required context for the test drive.

I use Flask with the built-in development server for quick prototypes or proof of concept work with local development. So that is fine for limited local machine users. However, I use a different approach once I deploy to Heroku or AWS virtual machines on the public internet. I use Flask, with Gunicorn or uWSGI, and Nginx. Now, I am sure you will know this, we are talking about a Python backend with API services. Python backends serve my use cases because of libraries like scikit-learn and nltk. I have even deployed my favourite stack on a RaspberryPi computer without hassle.

You can watch a video here where I demonstrated an entire project using Python and Flask. In addition, I did a series and explained the whole development process end to end, which is available here. Finally, I wrote articles here on Medium supporting the video series.

I always use vue.js with bootstrap for my styling on the frontend. I even wrote about making the Python backend server and vue application play nice without all that Docker container talk and CORS issue I read in many recent articles.

So I am an opinionated buyer in the ‘web framework dealership’. Like any would-be buyer, there is a little voice in my head saying…

https://expressjs.com/ - image is from the Express website
https://expressjs.com/ – image is from the Express website

Stop it! Well, after all, we all ignore those little voices in our heads, don’t we? Shiny object syndrome is genuine, after all. I will admit that I use Express and the clear advantage is the same coding language in the backend and frontend. However, grudgingly, I would also have to acknowledge that Express’s response seems and feel much faster than responses from an equivalent Flask arrangement.

It isn’t a single ‘web framework dealership’; it’s a crowded mall full of dealerships. I see the Java dealership further down, and that voice in my head is getting louder and louder; stop it!!

Where to go for the spin?

The other day I took a Golf GTI for a test drive. You bet they didn’t let me go far because the car had no fuel in it. So the test drive was about 1 mile as I was nervous about running out of gas. Those guys! Later I said no thanks!

Let us revisit one of my earlier projects and take FastAPI for a short spin along with that use case.

Semantic Highlighting

Semantic Highlighting is part of my current project to exploit NLP services for helping people with their resumes and job hunting. The Github repository for the spin contains the two approaches. The original Flask approach and the new FastAPI process.

With Flask

Running the Flask backend with Mac Mini M1 - image by the author
Running the Flask backend with Mac Mini M1 – image by the author
The frontend application, with Vue.js, demonstrating semantic Highlighting - image by author
The frontend application, with Vue.js, demonstrating semantic Highlighting – image by author

So we know where we are going and broadly what we expect to see on the test drive. The little voice in my head is getting in the back seat!

The test drive

Ok, let’s start it up!

The FastAPI backend running - localhost - image by the author
The FastAPI backend running – localhost – image by the author
Photo by the author - Brilliant! It is the same! I used different port numbers!
Photo by the author – Brilliant! It is the same! I used different port numbers!

The big difference is the lack of warning from the Flask development server. Better pop the hood and look at the engine; perhaps that is different! The voice in my head says, "what would you know about an engine" Stop it!!!!!!

At first glance, the code doesn’t seem that different! Instead of importing Flask, we import fastapi. There is still an app.route("/") which is now app.get("/"). We even have line 15, which points the backend server to the dist folder where we have that built version of the vue.js application. As a reminder, we have to build the vue.js application, which creates index.html and associated files in a folder called dist. You can read the instructions in the readme file in the repository.

Ok, so we are back from the test drive, and frankly, we can still do what we could do with Flask! Yawn!

What about creature comforts?

You’ve got to love those sales guys! They always have an angle.

http://localhost:8000/docs

With FastAPI, try navigating to /docs and pick up your jaw off the ground!

Swagger UI interface from /docs - image by the author.
Swagger UI interface from /docs – image by the author.

No more talk is needed, I will buy it!

Testing my single endpoint with the built-in Swagger-UI page - image by author
Testing my single endpoint with the built-in Swagger-UI page – image by author

If you do full-stack development, as in your backend and frontend work, you probably know that there are many frontend and backend steps for every piece of data needed in the User Interface codebase. So wiring up API calls and consuming those API’s can become a bit complicated, especially if you are using a "Big Ball of Mud design pattern".

Using FastAPI then presents an advantage to me over Flask. First, I get that Swagger-UI page, which helps me think about my endpoints and documentation and even use a better engineering pattern. But, more importantly, I can test my endpoints in a more user-friendly way, and naturally, the speed!

What is all that excitement?

There is a bunch of gobblygook under the hood of FastAPI, much talk about asynchronous versus synchronous, but it nets down to:-

  • async
  • await

All you need to know about Async Await in JavaScript

Django Async vs FastAPI vs WSGI Django: Choice of ML/DL Inference Servers – Answering some burning…

In my context, I am designing an application to process text documents, involving calls to NLP routines, and needing to maintain a fast response time for my users. Long-running text processing routines would hang the front end and alienate my users quickly. So now, with FastAPI, I can have the advantages of an Express server with JavaScript and the benefits of the Python libraries for Machine Learning without a significant compromise for my end users.

That is exciting and probably about time!

Photo by Jakob Owens on Unsplash
Photo by Jakob Owens on Unsplash

Join Medium with my referral link – David Moore


Towards Data Science is a community publication. Submit your insights to reach our global audience and earn through the TDS Author Payment Program.

Write for TDS

Related Articles