0
0
Flaskframework~10 mins

Why background processing matters in Flask - Test Your Understanding

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

Complete the code to import the Flask class from the flask package.

Flask
from flask import [1]
app = [1](__name__)
Drag options to blanks, or click blank then click option'
AFlask
BRequest
Crender_template
Dredirect
Attempts:
3 left
💡 Hint
Common Mistakes
Using Request instead of Flask
Trying to import render_template here
Using redirect which is for routing
2fill in blank
medium

Complete the code to define a route that returns 'Hello World!'.

Flask
@app.route('/')
def home():
    return [1]
Drag options to blanks, or click blank then click option'
AHello World!
B'Hello World!'
Crender_template('index.html')
Dredirect('/')
Attempts:
3 left
💡 Hint
Common Mistakes
Returning unquoted text causing NameError
Using render_template without a template file
Using redirect which sends a new request
3fill in blank
hard

Fix the error in the code to run the Flask app only when 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__main__
C'__main__'
D__name__
Attempts:
3 left
💡 Hint
Common Mistakes
Using __main__ without quotes causes NameError
Using 'main' misses the underscores
Comparing to __name__ itself is always True
4fill in blank
hard

Fill both blanks to create a background task using threading to avoid blocking the main Flask app.

Flask
import threading

def background_task():
    print('Task running')

thread = threading.Thread(target=[1])
thread.[2]()
Drag options to blanks, or click blank then click option'
Abackground_task
Bstart
Crun
Djoin
Attempts:
3 left
💡 Hint
Common Mistakes
Calling the function instead of passing it as target
Using run() which runs in the current thread
Using join() which waits for thread to finish
5fill in blank
hard

Fill all three blanks to create a Flask route that starts a background task and immediately returns a response.

Flask
@app.route('/start-task')
def start_task():
    thread = threading.Thread(target=[1])
    thread.[2]()
    return [3]
Drag options to blanks, or click blank then click option'
Abackground_task
Bstart
C'Task started!'
Djoin
Attempts:
3 left
💡 Hint
Common Mistakes
Using join() which blocks the response
Calling the function instead of passing it as target
Returning without quotes causing errors