Flask
A deliberately small web framework — routing and request handling, and you choose everything else.
What it is
Flask is a lightweight and micro web framework for Python, ideal for building small to medium-sized web applications and APIs. It provides the essential tools for web development without imposing a specific project structure, giving developers flexibility and simplicity.
Flask allows you to define routes, request handlers, templates, and more. It supports RESTful routing, URL parameters, sessions, cookies, and templating via Jinja2. You can build APIs, web services, or full-fledged web applications with ease.
- Maturity
- Very mature and stable
- Licence
- BSD 3-clause
When to use it
The question documentation cannot answer for you — because it cannot recommend something else.
Reach for it when
- Small services, internal tools and prototypes where you want to see all the moving parts
- You prefer assembling your own stack over accepting a framework's opinions
- Wrapping a machine-learning model or script in an HTTP endpoint
Look elsewhere when
- You want validation, serialisation and API docs handled for you — FastAPI does that natively
- You are building a large application with users, permissions and an admin area — Django will save months
Installation
pip install flaskGetting started
The smallest useful thing you can do with it, and what each part means.
from flask import Flask
app = Flask(__name__)
@app.get('/')
def home():
return 'Hello, World!'
if __name__ == '__main__':
app.run(debug=True)from flask import Flask
app = Flask(__name__)
@app.get('/user/<username>')
def greet_user(username):
return f'Hello, {username}!'
app.run(debug=True)Advanced usage
Where the library earns its place over a simpler alternative.
from flask import Flask, jsonify
app = Flask(__name__)
@app.get('/data')
def data():
return jsonify({'name': 'Flask', 'type': 'web framework'})
app.run(debug=True)from flask import Flask, request, jsonify
app = Flask(__name__)
@app.post('/submit')
def submit():
data = request.json
return jsonify({'received': data})
app.run(debug=True)from flask import Flask, render_template
app = Flask(__name__)
@app.get('/')
def home():
return render_template('index.html', title='Welcome')
app.run(debug=True)Errors and fixes
The failures you are most likely to hit, and what actually resolves them.
- 404 Not Found
- Use `@app.errorhandler(404)` to define a custom handler for missing routes.
- 500 Internal Server Error
- Use `@app.errorhandler(500)` to catch unhandled exceptions and log them.
Best practices
- Organize your app into blueprints for modularity.
- Use environment variables for configuration (Flask config object).
- Enable debug mode only in development.
- Validate user inputs to prevent injection attacks.
- Use Flask extensions for added functionality (Flask-Login, Flask-Migrate, etc.).
Alternatives
Comparable options, and the reason you would pick one over the other.
Background
Why it exists, and what it was reacting to.
Flask was created by Armin Ronacher in 2010 as part of the Pocoo project. It was designed to provide a minimal yet extensible web framework that doesn’t force particular dependencies or structure. Flask emphasizes simplicity, readability, and freedom of choice, making it a popular choice for web apps, APIs, and rapid prototyping.
