0
0
Flaskframework~10 mins

Why routing is Flask's core - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a route in Flask that shows 'Hello World!'.

Flask
from flask import Flask
app = Flask(__name__)

@app.route([1])
def hello():
    return 'Hello World!'
Drag options to blanks, or click blank then click option'
A"/hello"
B"hello"
C"/"
D"home"
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting the leading slash in the route path.
Using a string without quotes.
2fill in blank
medium

Complete the code to run the Flask app only if the script is executed directly.

Flask
if __name__ == [1]:
    app.run(debug=True)
Drag options to blanks, or click blank then click option'
A"main"
B"__init__"
C"__main__"
D"start"
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'main' without underscores.
Using '__init__' which is for package initialization.
3fill in blank
hard

Fix the error in the route decorator to correctly map the URL '/about'.

Flask
@app.route([1])
def about():
    return 'About page'
Drag options to blanks, or click blank then click option'
A"/about"
B"about"
C'about/'
D'about'
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting the leading slash in the route string.
Using single quotes inconsistently.
4fill in blank
hard

Fill both blanks to create a route that accepts a username parameter and returns a greeting.

Flask
@app.route([1])
def greet_user([2]):
    return f"Hello, {username}!"
Drag options to blanks, or click blank then click option'
A"/user/<username>"
Buser
Cusername
D"/username"
Attempts:
3 left
💡 Hint
Common Mistakes
Not matching the parameter name in the route and function.
Forgetting angle brackets around the parameter in the route.
5fill in blank
hard

Fill all three blanks to create a route that returns a JSON response with a message and status code.

Flask
from flask import jsonify

@app.route([1])
def status():
    response = [2]({{"message": "OK"}})
    response.status_code = [3]
    return response
Drag options to blanks, or click blank then click option'
A"/status"
Bjsonify
C200
D"status"
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string instead of an integer for status code.
Forgetting to import or use jsonify.