Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the Flask class.
Flask
from flask import [1] app = [1](__name__)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Importing Request instead of Flask
Using render_template in place of Flask
Forgetting to import Flask
✗ Incorrect
The Flask class is imported to create the app instance.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning a function call instead of a string
Forgetting quotes around the string
Using redirect instead of returning text
✗ Incorrect
The route function returns a simple string 'Hello World!'.
3fill in blank
hardFix the error in the code to run the Flask app.
Flask
if __name__ == '__main__': app.[1](debug=True)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using start() instead of run()
Calling execute() which does not exist
Forgetting parentheses after run
✗ Incorrect
The Flask app is started using the run() method.
4fill in blank
hardFill both blanks to create a real-time message update using Flask-SocketIO.
Flask
from flask_socketio import SocketIO socketio = SocketIO(app) @socketio.[1]('[2]') def handle_message(msg): print('Message:', msg)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'emit' as decorator instead of 'on'
Using 'connect' event instead of 'send'
Confusing 'send' with 'emit' in decorator
✗ Incorrect
The decorator @socketio.on('send') listens for 'send' events.
Flask-SocketIO uses 'on' to register event handlers.
5fill in blank
hardFill all three blanks to emit a real-time event to clients.
Flask
from flask_socketio import emit @app.route('/notify') def notify(): [1]('notification', [2]={'msg': 'Update!'}, [3]=True) return 'Notified'
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using send instead of emit
Passing message as a string instead of dictionary
Forgetting broadcast=True to notify all clients
✗ Incorrect
The emit function sends an event named 'notification'.
The data argument is a dictionary with the message.
The broadcast=True option sends to all connected clients.