Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the Flask class.
Flask
from flask import [1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Importing Request instead of Flask
Using lowercase flask instead of Flask
✗ Incorrect
The Flask class is imported from the flask module to create the app.
2fill in blank
mediumComplete the code to create a Flask app instance.
Flask
app = [1](__name__) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using request or session instead of Flask
Forgetting to pass __name__
✗ Incorrect
Creating the Flask app requires calling Flask with __name__.
3fill in blank
hardFix the error in the route decorator to define a streaming endpoint.
Flask
@app.route('/stream', [1]=['GET'])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'method' instead of 'methods'
Using 'route' or 'endpoint' which are incorrect here
✗ Incorrect
The correct keyword argument to specify HTTP methods is 'methods'.
4fill in blank
hardFill both blanks to create a generator function that yields messages for streaming.
Flask
def event_stream(): while True: yield [1] + '\n\n' time.sleep([2])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Yielding without 'data:' prefix
Using too long or zero sleep time
✗ Incorrect
The generator yields a string starting with 'data:' followed by two newlines, then sleeps 1 second.
5fill in blank
hardFill all three blanks to return a streaming response with correct mimetype.
Flask
from flask import Response @app.route('/stream') def stream(): return Response([1](), mimetype=[2]) # Use [3] mimetype for Server-Sent Events
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong mimetype like 'application/json' or 'text/plain'
Calling the generator without parentheses
✗ Incorrect
The Response uses the event_stream generator and 'text/event-stream' mimetype for SSE.