- Flask is a lightweight Python web framework used to build web apps and APIs.
- It’s called a microframework because it only provides essentials (routing, request/response) and relies on extensions for extra features.
- Flask → Lightweight, flexible, minimal setup, great for small/medium apps.
- Django → Full-stack, batteries-included (ORM, admin, auth), good for large projects.
- FastAPI → Modern, high-performance, async-based, auto API docs, best for APIs.
@app.route()is a decorator that maps a URL to a function.- Example:
@app.route("/hello")
def hello():
return "Hello, Flask!"Visiting /hello runs hello().
- Path parameter → Part of the URL itself. Example:
/user/5. - Query parameter → Passed after
?in the URL. Example:/search?name=suraj.
from flask import request
@app.route("/search")
def search():
name = request.args.get("name")
return f"Searching for {name}"-
Enables debug mode:
- Shows detailed error messages in browser.
- Auto-reloads server when code changes.
- Represents the incoming HTTP request.
- Lets us access form data, query params, JSON, headers, cookies, and HTTP method.
- Client sends a request (URL + data).
- Flask matches the request to a route (
@app.route). - Flask runs the matched view function.
- Function returns a response (HTML, JSON, text).
- Flask sends the response back to the client.
- Type hinting specifies the expected types of variables/returns in a function.
def add(x: int, y: int) -> int:
return x + ydef is_prime(n: int) -> bool:
if n < 2:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
print(is_prime(7)) # True
print(is_prime(10)) # False