Challenge - 5 Problems
Sensor API Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this Flask API endpoint?
Consider this Python Flask code running on a Raspberry Pi that reads a temperature sensor and serves the data as JSON. What will be the output when a client sends a GET request to
/temperature?Raspberry Pi
from flask import Flask, jsonify app = Flask(__name__) def read_temperature(): return 23.5 @app.route('/temperature') def temperature(): temp = read_temperature() return jsonify({'temperature_celsius': temp}) if __name__ == '__main__': app.run()
Attempts:
2 left
💡 Hint
Look at the dictionary key used in jsonify.
✗ Incorrect
The Flask endpoint returns a JSON response with key 'temperature_celsius' and value 23.5. So the output JSON string is {"temperature_celsius": 23.5}.
🧠 Conceptual
intermediate1:00remaining
Which HTTP method should be used to request sensor data from a JSON API?
You have a Raspberry Pi serving sensor data as a JSON API. Which HTTP method is the correct and standard way to request this data?
Attempts:
2 left
💡 Hint
Think about which method is used to retrieve data without changing it.
✗ Incorrect
GET is the standard HTTP method used to retrieve data from a server without modifying it.
🔧 Debug
advanced2:00remaining
What error does this Raspberry Pi Flask code raise?
This code tries to serve sensor data as JSON but has a bug. What error will it raise when running?
Raspberry Pi
from flask import Flask, jsonify app = Flask(__name__) def read_humidity(): return 55 @app.route('/humidity') def humidity(): value = read_humidity() return jsonify({'humidity': value}) if __name__ == '__main__': app.run()
Attempts:
2 left
💡 Hint
Check the syntax of the argument passed to jsonify.
✗ Incorrect
The code uses jsonify('humidity': value) which is invalid syntax. The correct syntax is jsonify({'humidity': value}). This causes a SyntaxError.
🚀 Application
advanced2:30remaining
How to add a timestamp to sensor JSON response?
You want to serve sensor data with a timestamp in the JSON response on your Raspberry Pi Flask API. Which code snippet correctly adds the current time in ISO format under key 'timestamp'?
Raspberry Pi
from flask import Flask, jsonify from datetime import datetime app = Flask(__name__) def read_light(): return 300 @app.route('/light') def light(): value = read_light() # Add timestamp here return jsonify({})
Attempts:
2 left
💡 Hint
Use datetime.now() to get current time, then convert to ISO string.
✗ Incorrect
datetime.now() returns current datetime object. Calling isoformat() on it returns a string in ISO format. This is the correct way to add a timestamp string.
❓ Predict Output
expert3:00remaining
What is the output of this asynchronous sensor data API code?
This Raspberry Pi code uses async Flask to serve sensor data. What is the JSON output when calling
/sensor?Raspberry Pi
import asyncio from flask import Flask, jsonify app = Flask(__name__) async def read_sensor(): await asyncio.sleep(0.1) return {'temperature': 22.1, 'humidity': 48} @app.route('/sensor') async def sensor(): data = await read_sensor() return jsonify(data) if __name__ == '__main__': app.run(debug=False)
Attempts:
2 left
💡 Hint
Check the awaited function and returned dictionary keys and values.
✗ Incorrect
The async function returns a dictionary with float and int values. jsonify converts it to JSON with numeric values, so output is {"temperature": 22.1, "humidity": 48}.