In the world of Python web development, "CRUD" (Create, Read, Update, Delete) is the bread and butter of almost every application. But for beginners, the first hurdle isn't the logic—it's the tooling. Do you go with Flask, the reliable "micro-framework" that has been a industry staple for over a decade? Or do you reach for FastAPI, the high-performance newcomer that’s taking the dev world by storm?
The biggest difference often comes down to how you run them. While FastAPI requires an external server like uvicorn to handle its asynchronous powers, Flask comes with its own "no-nonsense" built-in server for quick local development.
fastapi_server.py
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from typing import Dict
app = FastAPI()
# Data Schema
class User(BaseModel):
name: str
email: str
# In-memory "Database"
db: Dict[int, User] = {}
@app.post("/users/{user_id}")
def create_user(user_id: int, user: User):
if user_id in db:
raise HTTPException(status_code=400, detail="User already exists")
db[user_id] = user
return {"status": "Created", "data": user}
@app.get("/users/{user_id}")
def read_user(user_id: int):
if user_id not in db:
raise HTTPException(status_code=404, detail="User not found")
return db[user_id]
@app.put("/users/{user_id}")
def update_user(user_id: int, user: User):
if user_id not in db:
raise HTTPException(status_code=404, detail="User not found")
db[user_id] = user
return {"status": "Updated", "data": user}
@app.delete("/users/{user_id}")
def delete_user(user_id: int):
if user_id not in db:
raise HTTPException(status_code=404, detail="User not found")
del db[user_id]
return {"status": "Deleted"}
# To run: uvicorn fastapi_server:app --reload
Test: test_fastapi.py
import requests
BASE = "http://127.0.0.1:8000/users"
# POST
print("POST:", requests.post(f"{BASE}/1", json={"name": "Alice", "email": "alice@web.com"}).json())
# GET
print("GET:", requests.get(f"{BASE}/1").json())
# PUT
print("PUT:", requests.put(f"{BASE}/1", json={"name": "Alice Smith", "email": "alice@web.com"}).json())
# DELETE
print("DELETE:", requests.delete(f"{BASE}/1").json())
Server: flask_server.py
from flask import Flask, request, jsonify
app = Flask(__name__)
# In-memory "Database"
db = {}
@app.route('/users/<int:user_id>', methods=['POST'])
def create_user(user_id):
if user_id in db:
return jsonify({"error": "Exists"}), 400
db[user_id] = request.json
return jsonify({"status": "Created", "data": db[user_id]}), 201
@app.route('/users/<int:user_id>', methods=['GET'])
def read_user(user_id):
user = db.get(user_id)
return jsonify(user) if user else (jsonify({"error": "Not found"}), 404)
@app.route('/users/<int:user_id>', methods=['PUT'])
def update_user(user_id):
if user_id not in db:
return jsonify({"error": "Not found"}), 404
db[user_id] = request.json
return jsonify({"status": "Updated", "data": db[user_id]})
@app.route('/users/<int:user_id>', methods=['DELETE'])
def delete_user(user_id):
if user_id in db:
del db[user_id]
return jsonify({"status": "Deleted"})
return jsonify({"error": "Not found"}), 404
if __name__ == '__main__':
# Built-in server (No uvicorn needed)
app.run(port=5000, debug=True)
Test: test_flask.py
import requests
BASE = "http://127.0.0.1:5000/users"
# POST
print("POST:", requests.post(f"{BASE}/1", json={"name": "Bob", "email": "bob@web.com"}).json())
# GET
print("GET:", requests.get(f"{BASE}/1").json())
# PUT
print("PUT:", requests.put(f"{BASE}/1", json={"name": "Bob Jones", "email": "bob@web.com"}).json())
# DELETE
print("DELETE:", requests.delete(f"{BASE}/1").json())
Which one should you choose?
Now that you’ve seen both in action, the choice depends on your project goals:
Choose Flask if you want a simple, lightweight setup where you have total control over every line of code and want to run your app with a simple python app.py command.
Choose FastAPI if you’re building something modern that needs to scale, or if you simply love the idea of automatic data validation and instant, interactive documentation.
Top comments (0)