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 define a route that returns a JSON response.
Flask
@app.route('/status') def status(): return [1]({'status': 'ok'})
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using render_template instead of jsonify
Returning a dictionary directly without jsonify
✗ Incorrect
The jsonify function converts a Python dictionary to a JSON response.
3fill in blank
hardFix the error in the polling loop to wait 2 seconds between requests.
Flask
import requests import time while True: response = requests.get('/status') if response.json().get('status') == 'ok': break time.[1](2)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using time.wait which does not exist
Using time.pause or time.delay which are invalid
✗ Incorrect
The time.sleep(seconds) function pauses the program for the given seconds.
4fill in blank
hardFill both blanks to create a polling endpoint that returns JSON with a message.
Flask
from flask import Flask, [1] app = Flask(__name__) @app.route('/poll') def poll(): return [2]({'message': 'Polling response'})
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Importing request instead of jsonify
Returning render_template instead of JSON
✗ Incorrect
jsonify is imported and used to return JSON responses in Flask.
5fill in blank
hardFill all three blanks to create a polling loop that stops when server returns 'done'.
Flask
import requests import time while True: response = requests.get('[1]') if response.json().get('[2]') == '[3]': break time.sleep(1)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong URL like '/poll'
Checking wrong JSON key or value
✗ Incorrect
The polling URL is '/status', checking the 'status' key for the value 'done' to stop.