Getting Started
First Application
Flask is a microframework — it provides the essentials and lets you add what you need. debug=True enables auto-reload and detailed error pages.
# install Flask
pip install flask
# app.py
from flask import Flask, render_template
app = Flask(__name__)
@app.route("/")
def home():
return render_template("index.html")
if __name__ == "__main__":
app.run(debug=True)Running the Development Server
Never use the built-in server in production. FLASK_APP points to the module containing the app instance. Use --host=0.0.0.0 to make it reachable on the network.
# Set the entry point and enable debug mode
export FLASK_APP=app.py
export FLASK_ENV=development
flask run
# Override host and port
flask run --host=0.0.0.0 --port=5000
# Or run the module directly
python app.pyVirtual Environment
A virtual environment isolates project dependencies from the system Python. requirements.txt records exact versions so others can reproduce the environment with pip install -r requirements.txt.
# Create an isolated environment
python -m venv venv
# Activate (Linux/macOS)
source venv/bin/activate
# Activate (Windows PowerShell)
venv\Scripts\Activate.ps1
# Install dependencies
pip install flask
pip freeze > requirements.txtProject Structure
Flask does not enforce a layout, but separating static files, templates, and config is conventional. The instance/ folder holds deployment-specific secrets that should not be version-controlled.
myapp/
app.py # application factory or instance
config.py # configuration classes
requirements.txt # pinned dependencies
instance/
config.py # secret instance config (not in VCS)
static/ # CSS, JS, images
css/style.css
templates/ # Jinja2 templates
base.html
index.html
models.py # database models
views.py # route handlersApplication Factory Pattern
The factory pattern defers app creation until a function is called, which is essential for multiple instances, testing, and extensions. Flask's --app option accepts a callable that returns an app.
from flask import Flask
def create_app(config_name="default"):
app = Flask(__name__)
app.config.from_object(f"config.{config_name.title()}Config")
from .views import bp as views_bp
app.register_blueprint(views_bp)
@app.route("/health")
def health():
return {"status": "ok"}
return app
# Run with: flask --app myapp create_app run
# or export FLASK_APP=myapp:create_appMinimum Application Anatomy
Passing __name__ tells Flask where to find static files and templates. You can override folder locations with constructor arguments if your layout differs from the defaults.
from flask import Flask
app = Flask(
__name__,
static_folder="static",
template_folder="templates",
static_url_path="/static",
)
# __name__ lets Flask locate resources relative to the module.
# Route decorators map URL patterns to view functions.
@app.route("/")
def index():
return "Hello, Flask!"Routing
Variable Rules
Sections marked with <converter:varname> capture URL segments. Built-in converters: string (default), int, float, path (accepts slashes), uuid. The value is passed as a function argument.