0
0
Flaskframework~10 mins

Flask route as API endpoint - Interactive Code Practice

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

Complete the code to create a Flask route that returns '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"/world"
B"hello"
C"/hello"
D"world"
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting the leading slash '/' in the route path.
Not using quotes around the route path.
2fill in blank
medium

Complete the code to import the Flask class correctly.

Flask
[1] Flask
app = Flask(__name__)
Drag options to blanks, or click blank then click option'
Afrom flask import
Bimport flask
Cfrom Flask import
Dimport
Attempts:
3 left
💡 Hint
Common Mistakes
Using uppercase 'Flask' in the module name.
Using 'import flask' instead of importing the Flask class.
3fill in blank
hard

Fix the error in the route decorator to make it a valid Flask route.

Flask
@app.route([1])
def greet():
    return 'Hi there!'
Drag options to blanks, or click blank then click option'
A'greet'
B"greet"
C'/greet'
D"/greet"
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting the leading slash '/' in the route path.
Using quotes incorrectly or missing them.
4fill in blank
hard

Fill both blanks to create a Flask route that returns JSON data with a message.

Flask
from flask import Flask, [1]
app = Flask(__name__)

@app.route([2])
def api():
    return [3]({"message": "Hello API"})
Drag options to blanks, or click blank then click option'
Ajsonify
B"/api"
D"api"
Attempts:
3 left
💡 Hint
Common Mistakes
Not importing jsonify.
Using route paths without a leading slash.
Returning a dictionary directly instead of using jsonify.
5fill in blank
hard

Fill all three blanks to create a Flask route that accepts a variable part in the URL and returns a greeting.

Flask
from flask import Flask
app = Flask(__name__)

@app.route([1])
def greet_user(name):
    return f"Hello, [2]!"

if __name__ == '__main__':
    app.run([3])
Drag options to blanks, or click blank then click option'
A"/user/<name>"
Bname
Cdebug=True
D"/user/name"
Attempts:
3 left
💡 Hint
Common Mistakes
Not using angle brackets for variable parts in the route.
Mismatching variable names between route and function.
Forgetting to enable debug mode in app.run.