ComputerGeek
search
Flask @app.route() Decorator — Syntax, Parameters, and Examples Updated: Mar. 22 2026 | Created: Mar. 22 2026

Flask @app.route() Decorator

The Flask @app.route() decorator is used to register a URL route in your application. When a client sends a request to a URL that matches the route, Flask calls the view function connected to it.

In simple terms, @app.route() tells Flask:

"When a user visits this URL, run this function."

It is one of the most important parts of Flask because it connects URL paths to Python functions.

What Is Flask @app.route()?

@app.route() is a decorator provided by the Flask application object. It is used to map a URL path to a view function.

Example:

from flask import Flask

app = Flask(__name__)

@app.route("/")
def home():
    return "Hello, World!"

In this example, visiting / runs the home() function.

Why @app.route() Is Used in Flask

Flask uses routes to decide which function should handle an incoming request. Without routes, Flask would not know what to do when a user visits a specific URL.

You use @app.route() to:

Flask @app.route() Syntax

@app.route(rule, **options)

Flask @app.route() Parameters

rule (required)

The rule parameter is the URL pattern that Flask should match.

Example:

@app.route("/")
def home():
    return "Home Page"

Here, "/" refers to the root URL.

You can also define dynamic routes:

@app.route("/user/<username>")
def profile(username):
    return f"User: {username}"

In this case, Flask captures the value from the URL and passes it to the function.

Key points about rule

**options (optional)

The **options parameter lets you customize how the route behaves.

Below are the most commonly used route options.

endpoint

The internal name of the route. Flask uses it with functions like url_for().

@app.route("/home", endpoint="homepage")
def home():
    return "Home"

methods

Defines which HTTP methods the route accepts.

@app.route("/users", methods=["GET"])
def users():
    return "Users page"

You can allow multiple methods:

@app.route("/submit", methods=["GET", "POST"])
def submit():
    return "Form submitted"

Common HTTP methods include:

strict_slashes

Controls whether /path and /path/ are treated as different URLs.

@app.route("/about", strict_slashes=False)
def about():
    return "About page"

defaults

Sets default values for route variables.

@app.route("/page/", defaults={"page": 1})
@app.route("/page/<int:page>")
def show_page(page):
    return f"Page {page}"

If the user visits /page/, Flask uses 1 as the default value for page.

subdomain

Used for routing requests from a specific subdomain.

@app.route("/", subdomain="api")
def api_home():
    return "API Home"

This is useful in more advanced Flask applications.

provide_automatic_options

Controls whether Flask automatically handles OPTIONS requests for the route.

Flask @app.route() Examples

Example 1: Static Route

A static route matches a fixed URL path.

@app.route("/")
def hello():
    return "Hello, World!"

URL:

http://127.0.0.1:5000/

Output:

Hello, World!

Example 2: Dynamic Route

A dynamic route accepts values from the URL.

@app.route("/<name>")
def hello(name):
    return f"Hello, {name}!"

URL:

http://127.0.0.1:5000/John

Output:

Hello, John!

Example 3: Route With Type Converter

Flask also supports converters such as int, float, and path.

@app.route("/post/<int:id>")
def show_post(id):
    return f"Post ID: {id}"

URL:

http://127.0.0.1:5000/post/10

Output:

Post ID: 10

Flask Route Converters

Route converters help Flask validate and convert values from the URL before passing them to your function.

Common converters include:

Example:

@app.route("/product/<int:id>")
def product(id):
    return f"Product ID: {id}"

Flask @app.route() Return Type

A function decorated with @app.route() must return a value that Flask can convert into a response object.

Common return types in Flask routes

1. String

@app.route("/")
def home():
    return "Hello, World!"

Flask automatically converts the string into an HTTP response.

2. Dictionary

@app.route("/json")
def json_route():
    return {"message": "Hello"}

Flask automatically converts a dictionary into a JSON response.

3. Tuple

You can return a tuple that includes the response body, status code, and headers.

return "Not Found", 404
return {"error": "Bad Request"}, 400
return "OK", 200, {"X-Custom": "value"}

4. Response object

from flask import Response

@app.route("/custom")
def custom():
    return Response("Custom response", status=200)

5. Generator or iterable

This is useful for streaming content.

def generate():
    yield "Hello "
    yield "World"

@app.route("/stream")
def stream():
    return generate()

Important notes

Flask @app.route() Complete Example

from flask import Flask

app = Flask(__name__)

@app.route("/")
def handle_static_path():
    return "Hello, World!"

@app.route("/<name>")
def handle_dynamic_path(name):
    return f"Hello, {name}!"

Output

Summary

The Flask @app.route() decorator is used to map URLs to Python functions. It is a core feature of Flask routing and helps you define how your application responds to different URL requests.

With @app.route(), you can:

If you are learning Flask, understanding @app.route() is essential because almost every Flask application uses it.

Related Topics