0
0
Flaskframework~10 mins

Polling as fallback 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'
Arender_template
Bjsonify
CRequest
DFlask
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 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'
Ajsonify
Brequest
Credirect
Drender_template
Attempts:
3 left
💡 Hint
Common Mistakes
Using render_template instead of jsonify
Returning a dictionary directly without jsonify
3fill in blank
hard

Fix 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'
Asleep
Bwait
Cpause
Ddelay
Attempts:
3 left
💡 Hint
Common Mistakes
Using time.wait which does not exist
Using time.pause or time.delay which are invalid
4fill in blank
hard

Fill 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'
Ajsonify
Brequest
Drender_template
Attempts:
3 left
💡 Hint
Common Mistakes
Importing request instead of jsonify
Returning render_template instead of JSON
5fill in blank
hard

Fill 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'
A/poll
Bstatus
Cdone
D/status
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong URL like '/poll'
Checking wrong JSON key or value