Bird
Raised Fist0
Flaskframework~10 mins

Polling as fallback in Flask - Interactive Code Practice

Choose your learning style10 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

Practice

(1/5)
1. What is the main reason to use polling as a fallback in Flask applications?
easy
A. To regularly ask the server for updates when real-time methods fail
B. To reduce the number of server requests to zero
C. To make the server push updates automatically without client requests
D. To avoid any communication between client and server

Solution

  1. Step 1: Understand polling purpose

    Polling sends repeated requests to the server to check for updates.
  2. Step 2: Identify fallback use

    Polling is used when real-time communication methods like WebSockets are unavailable or fail.
  3. Final Answer:

    To regularly ask the server for updates when real-time methods fail -> Option A
  4. Quick Check:

    Polling = fallback for updates [OK]
Hint: Polling means repeated requests when real-time fails [OK]
Common Mistakes:
  • Thinking polling reduces server requests
  • Confusing polling with server push
  • Believing polling stops communication
2. Which Flask code snippet correctly implements a simple polling endpoint that returns JSON data?
easy
A. from flask import Flask app = Flask(__name__) @app.route('/poll') def poll(): return {'status': 'ok', 'time': datetime.now()}
B. from flask import Flask app = Flask(__name__) @app.route('/poll') def poll(): return 'status: ok'
C. from flask import Flask, json app = Flask(__name__) @app.route('/poll') def poll(): return json.dumps({'status': 'ok'})
D. from flask import Flask, jsonify app = Flask(__name__) @app.route('/poll') def poll(): return jsonify({'status': 'ok'})

Solution

  1. Step 1: Check Flask imports and response format

    Using jsonify is the correct way to return JSON responses in Flask.
  2. Step 2: Verify route and function syntax

    from flask import Flask, jsonify app = Flask(__name__) @app.route('/poll') def poll(): return jsonify({'status': 'ok'}) uses @app.route and returns jsonify({'status': 'ok'}) correctly.
  3. Final Answer:

    Correct Flask polling endpoint returning JSON with jsonify -> Option D
  4. Quick Check:

    jsonify for JSON response = from flask import Flask, jsonify app = Flask(__name__) @app.route('/poll') def poll(): return jsonify({'status': 'ok'}) [OK]
Hint: Use jsonify to return JSON responses in Flask [OK]
Common Mistakes:
  • Returning plain string instead of JSON
  • Using json.dumps without Response object
  • Returning dict directly without jsonify
3. Given this Flask polling endpoint code, what will the client receive when requesting '/poll'?
from flask import Flask, jsonify
app = Flask(__name__)

@app.route('/poll')
def poll():
    data = {'message': 'Hello', 'count': 5}
    return jsonify(data)
medium
A. Hello 5
B. {"message": "Hello", "count": 5}
C. {message: Hello, count: 5}
D. Error: jsonify not defined

Solution

  1. Step 1: Understand jsonify output

    jsonify converts Python dict to JSON string with double quotes and correct syntax.
  2. Step 2: Check returned data format

    The returned JSON string will be {"message": "Hello", "count": 5} as a JSON response.
  3. Final Answer:

    {"message": "Hello", "count": 5} -> Option B
  4. Quick Check:

    jsonify returns JSON string = {"message": "Hello", "count": 5} [OK]
Hint: jsonify returns JSON string with double quotes [OK]
Common Mistakes:
  • Expecting plain text instead of JSON
  • Confusing Python dict syntax with JSON
  • Assuming jsonify is undefined
4. Identify the error in this Flask polling endpoint code:
from flask import Flask, jsonify
app = Flask(__name__)

@app.route('/poll')
def poll():
    data = {'status': 'ok'}
medium
A. Function poll() is missing a return statement
B. Missing @app.route decorator
C. Flask app is not created properly
D. Returning a dict directly without using jsonify causes an error

Solution

  1. Step 1: Check for return statement

    The poll() function defines data but does not return it.
  2. Step 2: Verify function completeness

    Missing return statement means the endpoint returns None by default, causing unexpected empty response.
  3. Final Answer:

    Function poll() is missing a return statement -> Option A
  4. Quick Check:

    Missing return in view function [OK]
Hint: Always include return statement in Flask route handlers [OK]
Common Mistakes:
  • Missing return statement in route function
  • Forgetting route decorator
  • Not creating Flask app instance
5. You want to implement polling as a fallback in a Flask app that first tries WebSocket for real-time updates. Which approach correctly combines both methods?
hard
A. Use WebSocket and polling simultaneously without fallback logic
B. Only use polling and ignore WebSocket support
C. Use WebSocket for updates; if unsupported, client polls '/poll' endpoint every 5 seconds
D. Use polling only on the server side without client requests

Solution

  1. Step 1: Understand fallback strategy

    Fallback means trying WebSocket first, then polling if WebSocket fails or is unsupported.
  2. Step 2: Identify correct client-server interaction

    Client tries WebSocket; if it fails, client sends repeated requests to '/poll' endpoint every few seconds.
  3. Final Answer:

    Use WebSocket for updates; if unsupported, client polls '/poll' endpoint every 5 seconds -> Option C
  4. Quick Check:

    Fallback = WebSocket then polling [OK]
Hint: Try WebSocket first, then poll if needed [OK]
Common Mistakes:
  • Ignoring fallback and using only polling
  • Using polling without client requests
  • Running both methods without fallback logic
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard