Difficulty: Beginner
Flask is a lightweight web framework for Python, often called a microframework because it does not require particular tools or libraries beyond what it ships with. Created by Armin Ronacher in 2010 as an April Fools' joke that turned into a serious project, Flask has grown to become one of the most popular Python web frameworks alongside Django.
The term 'microframework' does not mean Flask lacks functionality. It means Flask keeps its core small and extensible. While Django follows a 'batteries-included' philosophy with a built-in ORM, admin panel, authentication system, and more, Flask gives you the freedom to choose your own tools. Need a database? Pick SQLAlchemy, Peewee, or MongoDB. Need authentication? Choose Flask-Login, Flask-Security, or roll your own. This philosophy makes Flask incredibly flexible and a great choice for both small APIs and large applications.
Flask is built on two key dependencies. The first is Werkzeug (German for 'tool'), a comprehensive WSGI utility library that handles the low-level HTTP details like request parsing, response formatting, URL routing, and cookie management. The second is Jinja2, a powerful template engine that lets you generate HTML dynamically using Python-like syntax with variables, loops, conditionals, and template inheritance.
Flask follows the WSGI (Web Server Gateway Interface) standard, which is a specification that defines how a web server communicates with a Python web application. When a request comes in, the web server (like Gunicorn or uWSGI) passes the request data to Flask through the WSGI interface. Flask processes the request, runs your view function, and returns a response that the web server sends back to the client.
One of Flask's most attractive features is its simplicity. A minimal Flask application is just five lines of code. You import Flask, create an application instance, define a route with a decorator, write a view function that returns a response, and run the app. This low barrier to entry makes Flask an excellent choice for beginners learning web development, for prototyping ideas quickly, and for building microservices in production.
Flask also provides a built-in development server with an interactive debugger, a RESTful request dispatching system, support for secure cookies (client-side sessions), Unicode support, and extensive documentation. The Flask ecosystem includes hundreds of extensions for every imaginable use case, from sending emails to handling WebSockets.
Companies like Netflix, Reddit, Lyft, Zillow, and Mozilla use Flask in production. Pinterest originally used Flask as their web framework. Flask's combination of simplicity, flexibility, and a thriving community makes it a top choice for Python web development.
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return 'Hello, World!'
if __name__ == '__main__':
app.run(debug=True)
This is the simplest possible Flask app. Flask(__name__) creates the application instance. The @app.route('/') decorator binds the URL path '/' to the hello() function. When a browser visits http://localhost:5000/, Flask calls hello() and returns the string as an HTTP response.
# Flask - you choose your components
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager
app = Flask(__name__)
db = SQLAlchemy(app)
login = LoginManager(app)
# Django - batteries included
# django.contrib.auth (built-in)
# django.db.models (built-in ORM)
# django.contrib.admin (built-in admin panel)
# django.forms (built-in form handling)
Flask lets you pick each component individually. Django provides everything out of the box. Neither approach is inherently better; it depends on your project needs. Flask gives more control, Django gives more convenience.
# Werkzeug handles low-level HTTP
from werkzeug.serving import run_simple
from werkzeug.wrappers import Request, Response
# Jinja2 handles templating
from jinja2 import Template
# Flask wraps both into a cohesive framework
from flask import Flask, render_template, request, jsonify
# You rarely use Werkzeug or Jinja2 directly,
# but understanding them helps you debug issues
# and extend Flask's behavior.
Flask abstracts Werkzeug and Jinja2 behind its own API. The flask.request object is actually a Werkzeug Request. The render_template function uses Jinja2 internally. Understanding this layering helps when you need to debug or customize behavior.
from flask import Flask, current_app
app = Flask(__name__)
# Outside a request, you need the app context
with app.app_context():
print(current_app.name) # prints '__main__'
@app.route('/')
def index():
# Inside a request, the context is automatic
return f'App name: {current_app.name}'
Flask uses application and request contexts to make certain objects globally accessible without passing them around. Inside a view function, current_app and request are automatically available. Outside, you must push a context manually.
Flask, Microframework, Python Web, WSGI, Werkzeug, Jinja2