0
0
Flaskframework~10 mins

Health check endpoints in Flask - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Health check endpoints
Start Flask App
Receive HTTP GET /health
Run health check function
Check system status
If OK
If NOT OK
Send HTTP response
End request
The app listens for a GET request on /health, runs checks, then returns a status response.
Execution Sample
Flask
from flask import Flask, jsonify
app = Flask(__name__)

@app.route('/health')
def health_check():
    return jsonify({'status': 'ok'}), 200
Defines a simple health check endpoint that returns status 'ok' with HTTP 200.
Execution Table
StepActionInputCheck ResultResponse Sent
1App starts and listensN/AN/AN/A
2GET request received/healthN/AN/A
3Run health_check functionN/ASystem OKN/A
4Return JSON response{"status": "ok"}N/AHTTP 200 with JSON
5Request endsN/AN/AResponse delivered
💡 Request ends after sending HTTP 200 response with status 'ok'.
Variable Tracker
VariableStartAfter Request
appFlask instance createdListening for requests
health_check responseNone{"status": "ok"}, 200
Key Moments - 2 Insights
Why does the health check return a JSON response instead of plain text?
The execution_table row 4 shows the function returns jsonify({'status': 'ok'}), which formats the response as JSON for easy machine reading.
What happens if the system is not OK in the health check?
Though not shown in this simple example, row 3 explains the check result. If not OK, the function would return a 500 error instead of 200.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what response is sent at step 4?
AHTTP 500 Internal Server Error
BHTTP 200 with JSON {'status': 'ok'}
CHTTP 404 Not Found
DNo response sent
💡 Hint
Check the 'Response Sent' column in row 4 of the execution_table.
At which step does the app receive the GET request for /health?
AStep 2
BStep 1
CStep 3
DStep 5
💡 Hint
Look at the 'Action' and 'Input' columns in the execution_table.
If the health check function returned a 500 error, which step would change?
AStep 2 - GET request received
BStep 3 - Run health_check function
CStep 4 - Return JSON response
DStep 5 - Request ends
💡 Hint
Step 4 shows the response sent; changing status code affects this step.
Concept Snapshot
Health check endpoints in Flask:
- Use @app.route('/health') with GET method
- Define a function returning JSON status and HTTP code
- Return 200 OK if system is healthy
- Return 500 or error code if unhealthy
- Helps monitoring tools check app status easily
Full Transcript
This example shows how a Flask app creates a health check endpoint at /health. When a GET request arrives, the app runs a function that returns a JSON response with status 'ok' and HTTP 200. This lets monitoring systems know the app is running fine. The execution table traces each step from app start, request received, function run, response sent, to request end. Variables like the Flask app instance and response data change accordingly. Key moments clarify why JSON is used and what happens if the system is not OK. The quiz tests understanding of the response sent, request step, and how errors affect the flow.