0
0
Flaskframework~10 mins

Health check endpoints 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 create a basic Flask app with a health check endpoint at '/health'.

Flask
from flask import Flask
app = Flask(__name__)

@app.route('/health')
def health_check():
    return [1]
Drag options to blanks, or click blank then click option'
A"OK"
B200
Capp.run()
Drequest
Attempts:
3 left
💡 Hint
Common Mistakes
Returning a status code number instead of a string
Calling app.run() inside the route function
2fill in blank
medium

Complete the code to run the Flask app only when the script is executed directly.

Flask
if __name__ == [1]:
    app.run(debug=True)
Drag options to blanks, or click blank then click option'
A__name__
B"main"
Capp
D"__main__"
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'main' without underscores
Using __name__ without quotes
3fill in blank
hard

Fix the error in the health check route to return a JSON response with status code 200.

Flask
from flask import jsonify

@app.route('/health')
def health_check():
    return jsonify(status='OK'), [1]
Drag options to blanks, or click blank then click option'
A200
B"OK"
C500
D404
Attempts:
3 left
💡 Hint
Common Mistakes
Using string 'OK' instead of numeric status code
Using error codes like 404 or 500
4fill in blank
hard

Fill both blanks to create a health check endpoint that returns JSON with a message and status code 200.

Flask
from flask import Flask, jsonify
app = Flask(__name__)

@app.route([1])
def health():
    return jsonify([2]='Healthy'), 200
Drag options to blanks, or click blank then click option'
A"/health"
B"/status"
Cmessage
Dstatus
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong route path
Using 'status' as JSON key instead of 'message'
5fill in blank
hard

Fill all three blanks to create a Flask app with a health check endpoint that returns JSON with 'status', 'uptime', and HTTP 200.

Flask
from flask import Flask, jsonify
import time

app = Flask(__name__)
start_time = time.time()

@app.route([1])
def health_check():
    uptime = time.time() - [2]
    return jsonify([3]='OK', uptime=uptime), 200
Drag options to blanks, or click blank then click option'
A"/health"
Bstart_time
Cstatus
D"/status"
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong route path
Using wrong variable for uptime calculation
Using incorrect JSON key