How it works · web framework for Python

How Flask works

You write a function for each address. Flask finds the right one for a request, calls it, and turns what it returns into a response.

Think of it like this. A receptionist with a list of who handles what: reads the request, calls the right person, and sends back their answer.

  1. The app One object that knows your routes and settings, and is what the web server calls. src/flask/app.py
  2. Routing The @app.route decorator records each address and its function. Matching is done by Werkzeug. src/flask/sansio/scaffold.py
  3. The context What belongs to the request being handled now: the request, the session, and a scratch space called g. src/flask/ctx.py
  4. The globals request, session, g and current_app: names you import once that always mean “the current one”. src/flask/globals.py
  5. Templates render_template fills an HTML file with your values, using Jinja. src/flask/templating.py
  6. Sessions Remembers a visitor between requests in a cookie that is signed, so it can’t be changed. src/flask/sessions.py

Follow one thing through: A browser asks for /hello

  1. The web server calls the app with the raw request. That call lands in wsgi_app.src/flask/app.py
  2. Flask makes a context for this request and pushes it, so request now means this one.src/flask/ctx.py
  3. Any functions you marked @before_request run first. One of them can answer early.src/flask/app.py
  4. The address is matched to an endpoint, and your view function is called.src/flask/app.py
  5. Whatever you returned (text, a dict, a tuple with a status) is turned into a real response by make_response.src/flask/app.py
  6. The @after_request functions run, the response goes out, and the context is popped.

The clever bit: A global that is different for everyone

from flask import request looks like one shared variable, yet two requests handled at the same moment each see their own. request is a proxy: every time you touch it, it looks up the context of whoever is running right now, kept in a Python context variable. You get the ease of a global without passing request into every function.

src/flask/globals.py

Words you’ll meet

WSGI
The agreed way a Python web server hands a request to an app: one function call per request.
view function
Your function that answers one address.
blueprint
A group of routes you can write once and plug into an app.
Werkzeug
The library under Flask that does the HTTP work: matching addresses, parsing requests, building responses.

Good questions to ask next

Open Flask in Bento and ask them there. It reads the project in your browser, draws its parts as a map you can press, and opens the real code behind each one.

Take Flask apart yourself.

Free and open source. Nothing to install, no account.

More projects, taken apart

Written by hand from the code as it was on 2026-10-30 (commit 49943b5). Flask belongs to its authors; this page is an independent explanation. Spot a mistake? Tell us.