Introduction

Flask and FastAPI are two popular web frameworks in Python. They allow you to quickly create web applications and APIs.

Feature Flask FastAPI
Type Micro-framework web Modern and high-performance web framework
Speed Slower (synchronous) Faster (asynchronous by default)
Typing Optional Extensive use of types (Pydantic)
Documentation Manual or via extensions Automatically generated (Swagger/OpenAPI)

Basics of Flask

Installation

pip install flask

Minimal Example

from flask import Flask

app = Flask(__name__)

@app.route('/')
def home():
    return "Hello Flask!"

if __name__ == '__main__':
    app.run(debug=True)

Running the Application

python app.py

The application runs on http://127.0.0.1:5000

Adding Parameters

@app.route('/hello/<name>')
def hello(name):
    return f"Hello, {name}!"

Basics of FastAPI

Installation

pip install fastapi uvicorn

Minimal Example

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def read_root():
    return {"message": "Hello FastAPI!"}

Running the Application

uvicorn app:app --reload

The application runs on http://127.0.0.1:8000

Automatic Documentation

FastAPI automatically generates two interfaces:


Handling Parameters

URL and Query Parameters (FastAPI)

@app.get("/items/{item_id}")
def read_item(item_id: int, q: str = None):
    return {"item_id": item_id, "q": q}

Request Body (FastAPI)

from pydantic import BaseModel

class Item(BaseModel):
    name: str
    price: float

@app.post("/items/")
def create_item(item: Item):
    return {"item": item}

When to Use What?

  • Use Flask if:
    • You want simplicity and full control
    • You are comfortable with WSGI
    • You are developing a small app or an existing project
  • Use FastAPI if:
    • You need performance and scalability
    • You want strong typing and automatic validation
    • You are creating a modern API (e.g., frontend/backend separation)

Useful Resources