FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.7+ based on standard Python type hints. It uses the uvicorn
server, which is an ASGI (Asynchronous Server Gateway Interface) server, to handle requests asynchronously. This allows FastAPI to handle a large number of concurrent requests efficiently. FastAPI also provides automatic validation, serialization, and documentation generation based on the type hints.
Flask is a lightweight web framework for Python. It is often referred to as a “microframework” because it provides only the essential components for building a web application, such as routing, request handling, and template rendering. Flask is highly flexible and allows developers to add additional functionality using extensions. It uses the Werkzeug
WSGI (Web Server Gateway Interface) toolkit and the Jinja2
templating engine.
Django is a high - level Python web framework that follows the model - view - controller (MVC) architectural pattern (although it is often referred to as model - view - template, MTV). It provides a built - in ORM (Object - Relational Mapping), an admin interface, authentication, and many other features out of the box. Django is designed to help developers take applications from concept to completion as quickly as possible.
First, install FastAPI and uvicorn
:
pip install fastapi uvicorn
Here is a simple example of a FastAPI application:
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"Hello": "World"}
@app.get("/items/{item_id}")
def read_item(item_id: int, q: str = None):
return {"item_id": item_id, "q": q}
To run the application:
uvicorn main:app --reload
Install Flask:
pip install flask
Here is a basic Flask application:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
@app.route('/user/<username>')
def show_user_profile(username):
return f'User {username}'
To run the Flask application:
export FLASK_APP=main.py
flask run
Install Django:
pip install django
Create a new Django project:
django-admin startproject myproject
cd myproject
Create a new app within the project:
python manage.py startapp myapp
Add the following view to myapp/views.py
:
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the myapp index.")
Configure the URL routing in myproject/urls.py
:
from django.contrib import admin
from django.urls import path
from myapp.views import index
urlpatterns = [
path('admin/', admin.site.urls),
path('', index, name='index'),
]
Run the Django development server:
python manage.py runserver
from fastapi import FastAPI
from pydantic import BaseModel
class Item(BaseModel):
name: str
price: float
app = FastAPI()
@app.post("/items/")
def create_item(item: Item):
return item
async
and await
in your view functions when dealing with I/O - bound operations.from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db'
db = SQLAlchemy(app)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80))
from django.db import models
class Book(models.Model):
title = models.CharField(max_length=200)
author = models.CharField(max_length=200)
def __str__(self):
return self.title
TestClient
provided by FastAPI to write unit and integration tests for your API.from fastapi.testclient import TestClient
from main import app
client = TestClient(app)
def test_read_root():
response = client.get("/")
assert response.status_code == 200
assert response.json() == {"Hello": "World"}
import os
from flask import Flask
app = Flask(__name__)
app.config.from_object(os.environ.get('APP_SETTINGS', 'config.DevelopmentConfig'))