Web Development Basics
Flask Introduction
A lightweight WSGI microframework for Python, perfect for small applications and microservices.
Interview: Core web framework. Frequently tested for route patterns, middleware, and backend API structures.
Flask is a lightweight web framework for Python based on Werkzeug (WSGI) and Jinja2 templates. Unlike Django, it adopts a "microframework" philosophy, providing only core routing and request handling, leaving database ORM, form validation, and authentication to extension libraries.
Microframework Architecture
Flask is extensible. The core application provides routing, request context, and simple rendering. By keeping the core simple, developers have the freedom to pick and choose the tools they want for database mappings (e.g. SQLAlchemy, Peewee) and forms.
Routing and Dynamic Parameters
Routes bind URL patterns to Python functions. Flask supports dynamic route parameters (e.g. /user/<username>) and types (e.g. <int:user_id>), converting parameters before passing them to the function.
Request Handling and JSON Response
Inside views, Flask exposes the global request context object to access request parameters, JSON, and headers. JSON responses are returned easily by returning dictionary types directly, or using jsonify().
Use Cases
Microservices — Building modular, isolated APIs that handle specific tasks in a large architecture.
Rapid Prototypes — Setting up quick, single-file APIs for frontend integration testing.
Machine Learning APIs — Exposing ML models for prediction (e.g. feeding features into a model and returning results as JSON).
Common Mistakes
Using global variables for mutable state — Storing application state in global lists/dicts (Flask is multi-threaded, causing race conditions in production).
Accessing request outside context — Attempting to read `request` parameters from helper modules without active request threads, causing RuntimeErrors.
Enabling debug mode in production — Leaving `debug=True` enabled in production, which exposes an interactive web-based debugger console allowing arbitrary python code execution.