Flask CLI Recipes
Ready-to-use CLI commands for Flask development.
Running the Application
# Development server (with auto-reload)
flask run --debug
# Specify host and port
flask run --host=0.0.0.0 --port=5001
# With environment variables
FLASK_APP=app.py FLASK_DEBUG=1 flask run
# Using python directly
python app.py
Interactive Shell
# Start Flask shell with app context
flask shell
Inside the shell, the app context is automatically available:
>>> from models import db, User, Client
>>> User.query.all()
>>> db.session.add(User(name='Test', email='test@example.com'))
>>> db.session.commit()
Route Inspection
# List all registered routes
flask routes
# Output:
# Endpoint Methods Rule
# -------------------- ------- -----------------------
# auth.login GET,POST /login
# auth.register GET,POST /register
# clients.clients GET /clients
# static GET /static/<path:filename>
Database Migrations (Flask-Migrate)
# Initialize migrations (once)
flask db init
# Create migration after model changes
flask db migrate -m "Add birthday column to clients"
# Apply migrations
flask db upgrade
# Rollback last migration
flask db downgrade
# Show current migration
flask db current
# Show migration history
flask db history
Custom CLI Commands
Register custom commands using Click decorators:
import click
from flask.cli import with_appcontext
@app.cli.command('seed')
@with_appcontext
def seed_db():
"""Seed the database with sample data."""
from models import db, User
user = User(name='Admin', email='admin@example.com')
db.session.add(user)
db.session.commit()
click.echo('Database seeded.')
@app.cli.command('cleanup')
@click.argument('days', default=30)
@with_appcontext
def cleanup_old_data(days):
"""Remove records older than N days."""
cutoff = datetime.now() - timedelta(days=days)
# ...
click.echo(f'Cleaned up records older than {days} days.')
flask seed
flask cleanup 60
Environment Variables
| Variable | Purpose | Default |
|---|---|---|
FLASK_APP |
Application module | app.py or wsgi.py |
FLASK_DEBUG |
Enable debug mode | 0 |
FLASK_ENV |
Environment name | production |
FLASK_RUN_HOST |
Server host | 127.0.0.1 |
FLASK_RUN_PORT |
Server port | 5000 |
Use a .flaskenv file (with python-dotenv installed) to set defaults:
# .flaskenv
FLASK_APP=app.py
FLASK_DEBUG=1
FLASK_RUN_PORT=5001
Production Server
# Gunicorn (Linux/macOS)
pip install gunicorn
gunicorn "app:create_app()" --bind 0.0.0.0:8000 --workers 4
# Waitress (Windows-compatible)
pip install waitress
waitress-serve --call "app:create_app"