0
0
Flaskframework~10 mins

Server-Sent Events alternative 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 import the Flask class.

Flask
from flask import [1]
Drag options to blanks, or click blank then click option'
AFlask
Brender_template
CSession
DRequest
Attempts:
3 left
💡 Hint
Common Mistakes
Importing Request instead of Flask
Using lowercase flask instead of Flask
2fill in blank
medium

Complete the code to create a Flask app instance.

Flask
app = [1](__name__)
Drag options to blanks, or click blank then click option'
Asession
Brequest
CFlask
Drender_template
Attempts:
3 left
💡 Hint
Common Mistakes
Using request or session instead of Flask
Forgetting to pass __name__
3fill in blank
hard

Fix 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'
Amethod
Bmethods
Croute
Dendpoint
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'method' instead of 'methods'
Using 'route' or 'endpoint' which are incorrect here
4fill in blank
hard

Fill 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'
A'data: Hello World!'
B'event: message'
C1
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Yielding without 'data:' prefix
Using too long or zero sleep time
5fill in blank
hard

Fill 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'
Aevent_stream
B'text/event-stream'
C'application/json'
D'text/plain'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong mimetype like 'application/json' or 'text/plain'
Calling the generator without parentheses