ABC 2026
<aside> šØ DISCLAIMER: This material has been produced using various openly available online resources to enhance its quality and comprehensiveness. In creating this document, we have attempted to acknowledge the original authors and primary contributors of the sourced materials. However, due to the extensive range of resources consulted, there may be instances where some contributors should be explicitly mentioned. Should there be any concerns or complaints regarding the attribution or content of this material, please do not hesitate to contact [email protected] for resolution and clarification.
</aside>
FastAPI requires Python 3.8 or higher. To run a FastAPI application in a production environment, you need an ASGI (Asynchronous Server Gateway Interface) server. A common choice is Uvicorn, a high-performance ASGI server that enables FastAPI to handle multiple concurrent requests efficiently.
python3 -m pip install fastapi
python3 -m pip install "uvicorn[standard]"
The basic āHello worldā example (fileĀ main0.py) looks like this:
from fastapi import FastAPI
import random
app = FastAPI()
# Fake in-memory database with 5 sensors
sensors = {
1: {"name": "Temperature Sensor", "value": 22.5},
2: {"name": "Humidity Sensor", "value": 55.2},
3: {"name": "Pressure Sensor", "value": 1013.2},
4: {"name": "Light Sensor", "value": 300.0},
5: {"name": "CO2 Sensor", "value": 420.5},
}
@app.get("/")
def read_root():
return {"Hello": "World"}
@app.get("/sensors/{sensor_id}")
def read_sensor(sensor_id: int):
if sensor_id not in sensors:
return {
"sensor_id": sensor_id,
"sensor_value": -1
}
return {
"sensor_id": sensor_id,
"sensor_name": sensors[sensor_id]["name"],
"sensor_value": sensors[sensor_id]["value"]
}
Run the server with: uvicorn main0:app
About the command:
main0: the fileĀmain0.pyĀ (the Python "module").app: the object created insidemain0.pyĀ with the lineĀapp = FastAPI().
Youāll get something like:
INFO: Started server process [13742]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Uvicorn running on <http://127.0.0.1:8000> (Press CTRL+C to quit)
<aside> āš»
Open your browser atĀ http://127.0.0.1:8000/
</aside>
You will see the JSON response as:
{
Hello: "World"
}
This is clearly the basic option offered by:
@app.get("/")
def read_root():
return {"Hello": "World"}