Flask
In short
Flask is a lightweight Python web framework offering routing, request handling and templates, leaving the database and project structure to the developer.
What is Flask?
Flask was written by Armin Ronacher and released in 2010, and it is maintained by the Pallets project. It calls itself a microframework: the core is small and built on two libraries, Werkzeug for the HTTP and WSGI plumbing and Jinja for HTML templates. A complete app can be a single file with a few decorated functions.
Routes are declared with decorators such as @app.route("/books/<int:id>"), and the function returns a string, a template or a dictionary that Flask turns into JSON. Everything else comes from extensions: Flask-SQLAlchemy for databases, Flask-Login for sessions, Flask-WTF for forms. Blueprints split larger apps into modules.
Flask is popular for small web apps, internal tools, prototypes, teaching and simple APIs, and for wrapping machine learning models behind an HTTP endpoint. Its explicit, unmagical style makes it easy to see what happens on each request, which is why many Python developers learn web development with it.
A common misconception is that Flask is only for toy projects. Large applications run on it too; it simply asks the team to choose and organize the pieces that Django includes out of the box. It runs behind a production WSGI server such as Gunicorn, not the built-in development server.
Key takeaways
- Flask is a lightweight Python web framework released in 2010.
- It is built on Werkzeug for HTTP and Jinja for templates.
- Routes are functions marked with decorators such as @app.route.
- Databases, auth and forms come from extensions you choose.
- Production apps run behind a WSGI server such as Gunicorn.
Example
from flask import Flask, jsonify, render_template
app = Flask(__name__)
BOOKS = {1: "Dune", 2: "Neuromancer"}
@app.route("/")
def home():
return render_template("index.html", books=BOOKS) # Jinja template
@app.route("/api/books/<int:book_id>")
def book(book_id):
if book_id not in BOOKS:
return jsonify(error="Not found"), 404
return {"id": book_id, "title": BOOKS[book_id]} # returned as JSON
# Development: flask --app app run --debug
# Production: gunicorn app:appReaders ask
What is the difference between Flask and Django?
Flask is minimal and lets you pick your own database layer, forms and structure. Django includes an ORM, admin panel, authentication and a fixed project layout. Flask gives flexibility; Django gives more ready-made features.
Is Flask good for APIs?
Yes, especially small and medium ones. For APIs that need automatic validation and generated documentation, FastAPI is often chosen instead.
What is WSGI?
The Web Server Gateway Interface, the standard way Python web apps talk to web servers. Flask apps are WSGI apps and run on servers such as Gunicorn or uWSGI.
Often compared
See also
- PythonProgramming Languages, p. 26Python is a general-purpose, dynamically typed programming language known for readable syntax and wide use in data science, automation, and web backends.
- DjangoBackend & APIs, p. 11Django is an open-source Python web framework that comes with an ORM, an admin panel, authentication and security protections, so teams can build quickly.
- FastAPIBackend & APIs, p. 17FastAPI is a modern Python API framework that uses standard type hints to validate requests, convert data and generate OpenAPI documentation automatically.
- Web ServerBackend & APIs, p. 46A web server is software, or the machine running it, that accepts HTTP requests from browsers and other clients and responds with pages, files, or data.
- REST APIBackend & APIs, p. 38A REST API is a web API that exposes data as resources identified by URLs and lets clients read or change them using standard HTTP methods.
- APIBackend & APIs, p. 2An API is a set of rules that lets one piece of software request data or actions from another in a predictable, documented way.
Spotted a mistake or something missing on this page?Suggest an edit