“An API is just a URL that returns data, not HTML.”
Example:
/predict → returns result
No page. No UI. Only data.
pip install flask“Flask is lightweight. Perfect for ML.”
Tell them to create:
ml_api/
│── app.py
“For APIs, we don’t need templates.”
from flask import Flask, jsonify
app = Flask(__name__)
@app.route("/api/hello")
def hello_api():
return jsonify({
"message": "Hello from Flask API"
})
if __name__ == "__main__":
app.run(debug=True)Say while explaining:
-
jsonify→ converts Python dict to JSON -
Browser shows JSON, not HTML
Run:
python app.pyOpen browser:
http://127.0.0.1:5000/api/hello
“If this works,
you have officially built an API.”
Let that moment land.
“ML models need input.
APIs receive input as JSON.”
from flask import request
@app.route("/api/square", methods=["POST"])
def square_number():
data = request.get_json()
number = data["number"]
result = number * number
return jsonify({
"input": number,
"output": result
})Say slowly:
-
POST→ sending data -
request.get_json()→ read input -
Return JSON result
“APIs are tested using tools, not browser typing.”
Example JSON:
{
"number": 5
}Response:
{
"input": 5,
"output": 25
}“This is how frontend talks to backend.”
Now relate it to ML, verbally:
“Replace this logic with your ML model prediction.”
Example explanation:
prediction = model.predict(input_data)“API does not care how complex your model is.
It only sends input and receives output.”
This clicks instantly.
@app.route("/api/predict", methods=["POST"])
def predict():
data = request.get_json()
marks = data["marks"]
if marks >= 50:
result = "Pass"
else:
result = "Fail"
return jsonify({
"marks": marks,
"prediction": result
})“Today this is logic.
Tomorrow this will be your ML model.”
“UI is optional.
API is mandatory.”
Explain flow:
HTML / React / Mobile App
↓
Flask API
↓
ML Model
↓
JSON Response
Don’t build UI yet.