DEV Community

loading...

Migrating a Super Simple CRUD App from Flask to FastAPI

Allyn E Farach
・3 min read

What is FastAPI and Why Do I Need It?

Flask has been one of the more popular Python frameworks for many years, but it’s time for an update. Today, we’ll be discussing Flask’s faster, more efficient cousin: FastAPI. Compared to Flask, FastAPI is more performant and has Swagger documentation and other goodies built-in. If you want to run a Python web application in production in 2021, you’ll want to use FastAPI.

While there are some syntactical differences between how you write Flask and how you write FastAPI, you’ll find that they’re quite similar. Today we’re going to migrate a simple CRUD app from using Flask to Fast. So follow along!

Here is the original Flask code:

from flask import Flask, request

app = Flask(__name__)

@app.route('/basic_api/entities/<int:entity_id>', methods=['GET', 'PUT', 'DELETE'])
def entity(entity_id):
    if request.method == "GET":
        return {
            'id': entity_id,
            'message': 'This endpoint should return the entity {} details'.format(entity_id),
            'method': request.method
        }
    if request.method == "PUT":
        return {
            'id': entity_id,
            'message': 'This endpoint should update the entity {}'.format(entity_id),
            'method': request.method,
            'body': request.json
        }
    if request.method == "DELETE":
        return {
            'id': entity_id,
            'message': 'This endpoint should delete the entity {}'.format(entity_id),
            'method': request.method
        }
Enter fullscreen mode Exit fullscreen mode

Now we’re going to break down each piece of this Flask code, and show you the equivalent code in FastAPI. First up, importing the libraries and instantiating the application object:

from flask import Flask, request

app = Flask(__name__)


from typing import Optional
from fastapi import FastAPI

app = FastAPI()

Enter fullscreen mode Exit fullscreen mode

Next up, let’s rewrite the /basic_api/entities/<int:entity_id> endpoint in FastAPI.

@app.route('/basic_api/entities/<int:entity_id>', methods=['GET', 'PUT', 'DELETE'])
def entity(entity_id):
   if request.method == "GET":
       return {
           'id': entity_id,
           'message': 'This endpoint should return the entity {} details'.format(entity_id),
           'method': request.method
       }
   if request.method == "PUT":
       return {
           'id': entity_id,
           'message': 'This endpoint should update the entity {}'.format(entity_id),
           'method': request.method,
           'body': request.json
       }
   if request.method == "DELETE":
       return {
           'id': entity_id,
           'message': 'This endpoint should delete the entity {}'.format(entity_id),
           'method': request.method
       }

Enter fullscreen mode Exit fullscreen mode

Note that in FastAPI, the request methods are defined as methods on the FastAPI object, for instance @app.get, @app.put, @app.post, etc rather than as a parameter. Also note that instead of stating the type of the url parameter entity_id, within the route, it’s instead typed as a parameter in entity()

@app.get('/basic_api/entities/{entity_id}')
def entity(entity_id: int):
   return {
       'id': entity_id,
       'message': 'This endpoint should return the entity {} details'.format(entity_id),
   }


@app.put('/basic_api/entities/{entity_id}')
def entity(entity_id: int, body: Entity):
   return {
       'id': entity_id,
       'message': 'This endpoint should update the entity {}'.format(entity_id),
       'body name': body.name
   }


@app.delete('/basic_api/entities/{entity_id}')
def entity(entity_id: int):
   return {
       'id': entity_id,
       'message': 'This endpoint should delete the entity {}'.format(entity_id),
   }

Enter fullscreen mode Exit fullscreen mode

Also note that in the put request route, we are passing along, as body, an Entity object. To define this object, we create a new class that inherits from BaseModel.

All together, the FastAPI application looks like:

from pydantic import BaseModel


class Entity(BaseModel):
   name: str
   description: Optional[str] = None


from pydantic import BaseModel
from typing import Optional
from fastapi import FastAPI

app = FastAPI()


class Entity(BaseModel):
   name: str
   description: Optional[str] = None

@app.get('/basic_api/entities/{entity_id}')
def entity(entity_id: int):
   return {
       'id': entity_id,
       'message': 'This endpoint should return the entity {} details'.format(entity_id),
   }


@app.put('/basic_api/entities/{entity_id}')
def entity(entity_id: int, body: Entity):
   return {
       'id': entity_id,
       'message': 'This endpoint should update the entity {}'.format(entity_id),
       'body name': body.name
   }


@app.delete('/basic_api/entities/{entity_id}')
def entity(entity_id: int):
   return {
       'id': entity_id,
       'message': 'This endpoint should delete the entity {}'.format(entity_id),
   }

Enter fullscreen mode Exit fullscreen mode

And that’s it! You can save the main.py file and run the server with

uvicorn main:app --reload
Enter fullscreen mode Exit fullscreen mode

This will bring the server up on http://localhost:8000

One of the nice features of FastAPI is that it comes with Swagger already integrated. You can access it at http://localhost:8000/docs

From there, you can test each of your endpoints to see that they work!

Alt Text

Discussion (4)

Collapse
narenandu profile image
Narendra Kumar Vadapalli

Flask==2.0.0 also provides the app.get, app.post etc... Probably swagger is the only built in advantage here? Plugins like Flask-RestX provide the swagger functionality but with additional boilerplate. In that sense FastAPI is cleaner w.r.t documentation

Collapse
zayyadi profile image
zayyadi

Asynchronous support in FastAPI made it clear of Flask for API development

Collapse
sylflo profile image
Sylvain Chateau

I think Flask supports async since the version 2

Thread Thread
zayyadi profile image
zayyadi

Yeah, not as extensive as FastAPI