🐍 Python Advanced

What is Flask in Python?

Answer

Flask is a lightweight, micro web framework for Python. Unlike Django's "batteries included" approach, Flask provides only the core (routing, request handling, templating with Jinja2) and lets you choose your own components. A minimal Flask app: from flask import Flask; app = Flask(__name__); @app.route("/"); def home(): return "Hello, World!". Key components: Blueprints (modular app organization), Flask-SQLAlchemy (ORM), Flask-Migrate (database migrations), Flask-Login (authentication), Flask-WTF (forms), Marshmallow (serialization). The application context (g) and request context (request, session) are thread-local proxies. Flask is excellent for microservices, APIs, and projects where Django's conventions would be overkill. FastAPI is a modern alternative to Flask with automatic API documentation, async support, and Pydantic integration.