Flask
A lightweight Python web framework for building websites and HTTP APIs. It matches incoming requests to Python functions and turns what those functions return into HTTP responses.
What Flask gives you
Flask handles the web-facing parts of a Python application: it routes incoming requests, makes request data available to your code, and builds HTTP responses. A handler can return JSON for a mobile app or render an HTML page for a browser. The name microframework describes the size of the core, not the size of the application you can build with it. Flask ships with routing and Jinja template integration, but unlike Django it doesn't supply an ORM, database migrations, or a ready-made admin site. You choose those pieces if you need them.
What happens when a request arrives
In a typical production setup, a server such as Gunicorn passes the request to Flask using WSGI, the standard interface between Python web applications and WSGI servers. Flask checks the URL path and HTTP method against its registered routes. If no route matches the path, it returns 404 Not Found. If the path matches but the method isn't allowed, it returns 405 Method Not Allowed.
For a matching route, Flask calls its handler, also known as a view function. Values captured from the path arrive as function arguments. The handler can inspect the query string, headers, and body through request. Its return value determines the response: for example, a Python dictionary becomes a JSON response, while a rendered template produces HTML.
Example: a JSON endpoint
from flask import Flask
app = Flask(__name__)
@app.get('/items/<int:item_id>')
def get_item(item_id):
return {'id': item_id}
GET /items/42 returns 200 OK with {"id": 42}. The route's int converter means the handler receives the integer 42, not the string '42'. GET /items/abc returns 404 because the path doesn't match. This endpoint echoes the ID; it doesn't check whether an item exists. To try it locally, save the file as app.py and run flask --app app run.
Where Flask fits and common pitfalls
Flask works for internal tools, JSON APIs, and sites that render pages on the server. As the route list grows, blueprints let you register related routes together, such as all the endpoints under /billing. Keep the built-in development server for local work. Also avoid storing per-user state in a module-level variable: concurrent requests in one process can overwrite it, and separate worker processes don't share that variable.
How Flask comes up in interviews
You may be asked to explain why a route returns 404 rather than 405, or to test the endpoint with app.test_client(), which sends a request through Flask without opening a network port. Another common topic is why request looks global even though it holds different data for each request. It's a proxy that resolves to the request object in the current request context, not a single mutable object shared by all handlers.