0
0
Flaskframework~10 mins

Route decorator (@app.route) in Flask - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Route decorator (@app.route)
Define function
Apply @app.route decorator
Register URL path with function
Start Flask server
Browser sends HTTP request to URL
Flask matches URL to function
Function runs and returns response
Response sent back to browser
This flow shows how a function is linked to a URL path using @app.route, so when a browser visits that URL, Flask runs the function and sends back the response.
Execution Sample
Flask
from flask import Flask
app = Flask(__name__)

@app.route('/')
def home():
    return 'Hello, world!'
This code sets up a Flask app and uses @app.route('/') to link the home() function to the root URL, returning a greeting.
Execution Table
StepActionDetailsResult
1Define function home()Function created but not linked yethome() exists
2Apply @app.route('/') decoratorLink '/' URL path to home()Route '/' registered to home()
3Start Flask serverApp ready to receive requestsServer running
4Browser sends GET request to '/'Request received by FlaskRequest matched to home()
5Run home() functionFunction executes return statementReturns 'Hello, world!'
6Send response to browserResponse contains 'Hello, world!'Browser displays greeting
7EndRequest handled successfullyWaiting for next request
💡 Request handled and response sent; server waits for next request
Variable Tracker
VariableStartAfter Step 2After Step 5Final
appFlask instance createdRoute '/' linked to home()No changeNo change
homeFunction definedLinked to route '/'Executed, returns stringNo change
requestNo requestNo requestGET '/' receivedNo request (after response)
Key Moments - 3 Insights
Why does the function run only when the URL is visited?
Because @app.route('/') registers the function to that URL path, Flask calls it only when a matching request arrives, as shown in execution_table step 4 and 5.
What happens if we define a function without @app.route?
The function exists but is not linked to any URL, so Flask never runs it on requests. This is shown in execution_table step 1 where the function is defined but not registered.
Can multiple URLs use the same function?
Yes, by applying multiple @app.route decorators or listing multiple paths in one decorator, Flask registers all those URLs to the same function.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what happens at step 4?
AFlask starts the server
BBrowser sends GET request to '/'
CFunction home() is defined
DResponse is sent to browser
💡 Hint
Check the 'Action' and 'Details' columns at step 4 in the execution_table
At which step does Flask link the URL '/' to the function home()?
AStep 2
BStep 5
CStep 1
DStep 6
💡 Hint
Look for when @app.route('/') is applied in the execution_table
If we remove @app.route decorator, what changes in the execution flow?
AServer fails to start
BFunction runs automatically on server start
CFunction never runs on HTTP requests
DBrowser shows an error immediately
💡 Hint
Refer to key_moments about function without @app.route and execution_table step 1 vs step 2
Concept Snapshot
Use @app.route('path') above a function to link that URL path to the function.
When a browser visits that path, Flask runs the function and sends its return value as response.
Without @app.route, the function is not connected to any URL and won't run on requests.
This is how Flask maps URLs to code simply and clearly.
Full Transcript
The @app.route decorator in Flask connects a URL path to a Python function. When you write @app.route('/') above a function, you tell Flask to run that function whenever someone visits the root URL. The flow starts by defining the function, then applying the decorator which registers the URL. When the Flask server runs and a browser sends a request to '/', Flask matches the URL to the function and runs it. The function returns a response string, which Flask sends back to the browser to display. If the decorator is missing, the function exists but Flask never calls it on requests. This simple pattern lets you build web pages by linking URLs to Python code.