A web framework supplies reusable structure for handling requests, routing, application behavior and responses.
Compare framework responsibilities and follow a request through validation, a handler and response serialization.
A request reaches GET /orders/42. Choosing the handler is only the first step: the application still needs validation, access checks, storage and a response. Compare frameworks through those responsibilities, then build a small router that makes just the matching mechanism visible.

Build the same endpoint three ways
One endpoint — list today's orders — in three frameworks. Read these for the shape, not to run them:
# Flask
from flask import Flask
app = Flask(__name__)
@app.route("/orders")
def list_orders():
return {"orders": ["Tea", "Samosa"]}# Django (urls.py + views.py)
# urls.py
from django.urls import path
from . import views
urlpatterns = [path("orders", views.list_orders)]
# views.py
from django.http import JsonResponse
def list_orders(request):
return JsonResponse({"orders": ["Tea", "Samosa"]})# FastAPI
from fastapi import FastAPI
app = FastAPI()
@app.get("/orders")
def list_orders():
return {"orders": ["Tea", "Samosa"]}Flask and FastAPI both use a decorator to register a route right above
the function that handles it — @app.route vs @app.get, the same registration idea within different request-handling systems. Django separates the two: urls.py lists every
path, views.py holds the function each one points to. FastAPI additionally
reads list_orders's own type hints to validate the request and build
interactive API docs automatically — the same hints from the previous lesson,
put to work at runtime.
Preparing this topic’s interactive investigation…
Compare the rest of the field
Past the three most-used frameworks, the rest split by what they're optimised for, not by how routes are declared:
| Framework | Built for |
|---|---|
| gevent | not a framework by itself — a library that makes blocking code cooperate with many connections via greenlets |
| aiohttp | both an async HTTP client and server, built directly on asyncio |
| Tornado | one of the first Python servers built for thousands of long-lived connections (chat, live updates) |
| Sanic | Flask-shaped syntax, but async-first request handling; throughput still depends on the workload |
| Pyramid | starts minimal like Flask, but scales up to Django-sized apps without switching frameworks |
| Plotly Dash | a framework for analytical web applications using Python component and callback APIs |
A team wants Flask's simple, minimal feel but needs the server to handle many slow, concurrent connections efficiently. Which is the closest match?
Follow the request beyond the route match
Finding GET /orders/42 is only the first decision. The request still needs input validation, authentication, authorization, application logic, storage access and a response contract. A route that matches successfully can still return an invalid answer or expose another user's order.
Trace the request as responsibilities rather than framework syntax:
| Boundary | Question |
|---|---|
| routing | which handler owns this method and path? |
| validation | can 42 become the required identifier type? |
| authorization | may this caller read order 42? |
| service/storage | does the order exist, and did the query succeed? |
| response | which status and safe fields describe the outcome? |
The browser router exercise checks only the first boundary. Its single 404 fallback deliberately omits the distinction between a missing route and a known path with an unsupported method (405). A real framework may also normalize paths, decode URL components and apply converters. Do not add these silently to the exercise's contract.
Choose a framework using your application constraints: an existing Django model/admin ecosystem, a small service with selected Flask extensions, or typed API contracts and async integrations in FastAPI. A two-line endpoint comparison cannot establish throughput, maintainability or security. Flask and Django also support async views under documented conditions; their execution model and deployment stack matter.
Transfer to an AI endpoint: POST /summaries accepts text, calls a model provider and stores a result. Mark where payload limits, authentication, provider timeouts and output validation belong. An async def handler that calls a blocking library still blocks its event-loop thread unless the call is offloaded appropriately. Keep parsing and result validation in ordinary functions so they can be tested here with fake provider responses; the HTTP server and real provider calls are local/deployed labs, not browser execution.
References: FastAPI concurrency, Flask async views, and Django async support.
Match a request to a handler
Strip away the decorators and URL config, and every framework is doing the same lookup a plain dict can do:
The key is the pair (method, path) — not just the path. Miss that and
two different operations on the same URL silently overwrite each other:
routes = {}
routes["/orders"] = "list_orders" # GET /orders
routes["/orders"] = "create_order" # meant to be POST /orders — same key!
print(routes["/orders"])Real paths aren't all fixed strings, either — /orders/42 needs to match a
pattern, /orders/<id>, and hand "42" to the handler as a parameter.
That match is still just string work: split both the pattern and the real
path on /, compare segment by segment, and capture anything written as
<name>.
Quick practice
GET /orders and POST /orders should be two different routes, but the second overwrites the first and dispatch never even looks at method. Fix the keys so both survive, and print both handlers.
def split_path(path):
return [seg for seg in path.strip("/").split("/") if seg]
print(split_path("/orders/42/"), split_path("/"))pattern_segs = ["orders", "<id>"]
path_segs = ["orders", "42"]
params = {}
for p, s in zip(pattern_segs, path_segs):
if p.startswith("<"):
params[p[1:-1]] = s
print(params)FastAPI reads list_orders's own type hints to validate incoming requests automatically. What does that cost, compared to Flask reading no hints at all?
🎯 Your turn
Write route_request(method, path). Build a router dict with these three
fixed routes, then look up (method, path) in it:
("GET", "/")→"home"("GET", "/health")→"health_check"("POST", "/login")→"login"anything else →
"404 Not Found"route_request("GET", "/health")→"health_check"route_request("DELETE", "/orders")→"404 Not Found"
Hint: routes = {("GET", "/"): "home", ...}, then
return routes.get((method, path), "404 Not Found").
Press ▶ Run, try the Live App chips, then ✓ Check. After that, the practice set below grows this lookup table into a router that matches path parameters like a real framework does.
Practice — level up
0/5 solvedSolve each in its own editor — Run to try, Check to grade. Stuck? Reveal a Hint, or the full Solution + walkthrough.
Write add_route(router, method, path, handler). router is a dict of {"METHOD path": handler}. Return a NEW dict with the route added, without changing the original.
Write dispatch(router, method, path). Look up f"{method} {path}" in router and return the handler, or "404" if there's no match.
Write split_path(path). Return a list of the non-empty segments between slashes: "/orders/42" -> ["orders", "42"], "/" -> [].
Write match_route(pattern, path). pattern may contain segments like "<id>". Return a dict of captured parameters if every segment matches (literal segments must be equal; <name> segments capture anything), or None if the segment counts differ or a literal segment doesn't match.
Write route_with_params(routes, method, path). routes is a list of [method, pattern, handler] triples. Find the first route whose method matches AND whose pattern matches path (via the same rule as match_route). Return [handler, params], or ["404", {}] if nothing matches.