Flask Tutorial
Flask is a lightweight Python micro-framework. It gives you the core tools and lets you choose your own libraries — perfect when you want full control without the opinions of a full-stack framework.
Flask vs Django
| Feature | Flask | Django |
|---|---|---|
| Size | Micro (minimal) | Full stack (batteries included) |
| ORM | Your choice (SQLAlchemy) | Built-in Django ORM |
| Admin | Flask-Admin (optional) | Auto-generated admin |
| Flexibility | Very high | Opinionated structure |
| Best for | Microservices, small APIs, custom apps | Large full-stack apps |
Python – Flask Core Concepts
# Simulating Flask routing and request handling from functools import wraps class Flask: def __init__(self, name): self.name = name self.routes = {} self.filters = {} self.before_req = [] def route(self, path, methods=None): def decorator(fn): m = tuple(methods or ["GET"]) self.routes[(path, m)] = fn return fn return decorator def before_request(self, fn): self.before_req.append(fn); return fn def dispatch(self, path, method="GET", **kwargs): for _ in self.before_req: _() for (p, m), fn in self.routes.items(): if p == path and method in m: return fn(**kwargs) return ("404 Not Found", 404) app = Flask(__name__) @app.before_request def log_request(): print("[LOG] Request received") @app.route("/") def index(): return ("Welcome to Flask!", 200) @app.route("/users", methods=["GET", "POST"]) def users(): return ([{"id": 1, "name": "Alice"}], 200) print(app.dispatch("/")) print(app.dispatch("/users")) print(app.dispatch("/missing"))
Try It – Flask Routing
Code Editor
Output
Click ▶ Run…
Quick Quiz
What is the name of Flask's built-in templating engine?
Flask Coding Labs
8 hands-on labs from basic routing to REST APIs and testing.
Lab 1 – REST API with JSON
Flask REST API Lab
Code Editor
Output
Click ▶ Run…
Lab 2 – Blueprint Pattern
Flask Blueprints Lab
Code Editor
Output
Click ▶ Run…