Quickstart

This chapter walks you through a minimal but working Flask application, covering development mode and the project entry point.

Installation

python -m venv .venv
source .venv/bin/activate   # Windows: .venv\Scripts\activate
pip install flask

Minimal Application

# app.py
from flask import Flask

app = Flask(__name__)

@app.route("/")
def index():
    return "Hello, Flask!"

The __name__ passed to Flask(__name__) locates the application root, which is how Flask finds templates/ and static/.

Running the Development Server

flask --app app run --debug
  • --app app points at the app object in app.py (can be omitted when the file is named app.py or wsgi.py)
  • --debug enables the debugger and auto-reload: the server restarts when you save, and errors show an interactive traceback in the browser

Note: the FLASK_ENV=development environment variable seen in older tutorials was removed in Flask 2.3 — use the --debug flag instead.

Open http://127.0.0.1:5000 to see the output.

Adding More Routes

@app.get("/about")                     # GET only
def about():
    return "About page"

@app.post("/contact")                  # POST only
def contact():
    return "Received", 201

@app.route("/form", methods=["GET", "POST"])   # One view, both methods
def form():
    ...

Returning JSON and Templates

from flask import render_template

@app.get("/api/ping")
def ping():
    return {"status": "ok"}            # dicts are serialized to JSON automatically

@app.get("/hello/<name>")
def hello(name):
    return render_template("hello.html", name=name)
<!-- templates/hello.html -->
<h1>Hello, {{ name }}!</h1>

A Security Note on Debug Mode

The debugger allows executing arbitrary Python from the browser. Never enable --debug or debug=True in production. See the Deployment chapter for production setups.

Next Steps

  • Real projects should wrap the application in a factory function (create_app()) for easier testing and multi-instance creation — see the App Structure chapter.
  • Continue to the Routing chapter for URL parameters, converters, and url_for.