from flask import Flask app = Flask(__name__) @app.route('/') def home(): return 'Welcome to Flask!' if __name__ == '__main__': app.run()
The route '/' is linked to the home function which returns the string 'Welcome to Flask!'. Flask sends this string as the response body, so the browser displays it.
from flask import Flask app = Flask(__name__) @app.route('/hello') def hello(): return 'Hello!' if __name__ == '__main__': app.run()
Option A correctly uses the decorator, defines the function with a colon, and indents the return statement. Other options have missing indentation or colons causing syntax errors.
from flask import Flask app = Flask(__name__) @app.route('/user/<name>') def user(name): return f'Hello, {name}!' if __name__ == '__main__': app.run()
The route captures the part after '/user/' as the variable name. The function returns 'Hello, {name}!' with the actual name from the URL, so visiting '/user/Alice' returns 'Hello, Alice!'.
from flask import Flask app = Flask(__name__) @app.route('/about/', strict_slashes=True) def about(): return 'About page' if __name__ == '__main__': app.run()
Flask treats '/about/' and '/about' as different routes. The route is registered with a trailing slash, so visiting '/about' (no slash) causes a 404 error.
Routing maps web addresses (URLs) to Python functions that generate responses. This mapping is fundamental because it controls what the app shows for each URL.