Using FastAPI to Recreate the Flask Tutorial

November 8, 2021 | ~1100 words | 6 min read


I originally wrote this post on Towards Data Science before moving it here.

Flask and FastAPI are two popular Python web frameworks. It’s easy to find tons of comparisons of the two frameworks online — if you’re reading this post you’ve probably already read your share of “Flask vs. FastAPI” articles.

Here’s a very quick summary. Flask has been around longer and is geared towards small web apps. Being the older framework, it tends to have a larger user base and more tutorials and answered questions. FastAPI is newer and is geared towards creating REST APIs. It’s quickly growing in popularity, especially for machine learning use cases. But generally both frameworks are very similar—anything you can do with Flask can probably be done with FastAPI and vice versa.

One thing Flask has is a great beginner tutorial for building a simple app where users can register, log in, and create posts. FastAPI has good documentation for building APIs, but it’s lacking a simple tutorial for a basic app like the Flask example. I’m in the process of learning both frameworks, so I decided it would be a good exercise to recreate the Flask tutorial app using FastAPI. I thought other beginners might benefit from what I learned.

How to get the most out of this post

  1. View the source code on GitHub. I’m not writing a full tutorial here. I’ll call out some key differences between the Flask and FastAPI apps, but you’ll have to go to GitHub to see the full source code and how everything fits together.
  2. Read the Flask tutorial. It explains the app we’re building, plus there’s a lot of good information in the tutorial that isn’t framework specific. If you’re already familiar with web frameworks you can just skim the tutorial, but if you’re brand new it might be beneficial to complete it (it’ll only take a couple hours, max).
  3. Do it yourself! Code the app from scratch, start from Flask example and convert it to FastAPI, or clone my GitHub repository tinker with it. The best way to learn is by doing!

The tutorial app

In the Flask tutorial we built an app called Flaskr. I built an equivalent app using FastAPI and called it Fastr. Both apps allow users to register with a username and password, log in, and make posts in a blog format. The apps run on top of a SQLite database which stores user and post data.

The same web app created using Flask (left) and FastAPI (right).

The same web app created using Flask (left) and FastAPI (right).

Differences between the Flask and FastAPI apps

Other than the superficial changes you can see in the fonts and colors, there are some key differences between the way the apps are implemented behind the scenes. I’ll highlight some of them here, approximately in order of where you’d encounter them in the Flask tutorial. I won’t get too technical in this article so be sure to reference the source code on GitHub for more details.

This isn’t a complete list, but here are some key differences between the Flask tutorial app and my implementation in FastAPI:

Wrapping up

If you compare the source code for the Flask tutorial app and my FastAPI implementation, you’ll see that they’re more similar than they are different. There’s a bit more boilerplate code in the FastAPI app to replicate some of the functionality that comes built in with Flask. But you also get some of the benefits of FastAPI, such as data validation from type hints and automatically generated interactive documentation.

Would I recommend Flask or FastAPI for this type of app? I don’t know. For something so simple it doesn’t really matter. Once we start adding more functionality we might benefit from one framework more than the other. For example, if we want to add support for asynchronous requests, FastAPI includes that capability natively. Or if we have a specific and complex feature we want to implement quickly, we might have better luck finding a starting point in the huge number of Flask community examples. Ultimately, either one can get the job done and it’s easy to transfer your knowledge from one to the other.


Tags: coding web apps learning flask fastapi