0
0
Flaskframework~10 mins

Context lifecycle execution in Flask - 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 app instance.

Flask
from flask import Flask
app = Flask([1])
Drag options to blanks, or click blank then click option'
Aapp
B__name__
CFlask
DNone
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string like 'app' instead of __name__
Passing None which causes errors
2fill in blank
medium

Complete the code to define a route for the home page.

Flask
@app.route([1])
def home():
    return "Hello, Flask!"
Drag options to blanks, or click blank then click option'
A"/home"
B"home"
C"/"
D"index"
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'home' without a slash
Using '/home' which is a different URL
3fill in blank
hard

Fix the error in the code to run the Flask app only when the script is executed directly.

Flask
if [1] == '__main__':
    app.run()
Drag options to blanks, or click blank then click option'
A__name__
Bapp.name
Cmain
Dapp.__main__
Attempts:
3 left
💡 Hint
Common Mistakes
Using app.name which is incorrect
Using 'main' without underscores
4fill in blank
hard

Fill both blanks to access the request method inside a route function.

Flask
from flask import [1]

@app.route('/submit', methods=['POST'])
def submit():
    if [2].method == 'POST':
        return 'Form submitted!'
Drag options to blanks, or click blank then click option'
Arequest
BResponse
DFlask
Attempts:
3 left
💡 Hint
Common Mistakes
Importing Response instead of request
Using Flask instead of request
5fill in blank
hard

Fill all three blanks to create a context manager that pushes and pops the app context.

Flask
with app.[1]() as [2]:
    print([3].app.name)
Drag options to blanks, or click blank then click option'
Aapp_context
Bctx
Drequest
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'request' instead of 'ctx' for the context variable
Not using app_context() method