xxxxxxxxxx
<project-name>/src/<project-module-name>/main.py
from fastapi import FastAPI
import uvicorn
app = FastAPI()
@app.get("/")
def read_root():
return {"Hello": "World"}
def start():
uvicorn.run("src.<project-module-name>.main:app", host="127.0.0.1", port=8000)
def dev():
uvicorn.run("src.<project-module-name>.main:app", host="127.0.0.1", port=8000, reload=True)
[tool.poetry]
name = "<project-name>"
version = "0.1.0"
description = ""
authors = ["FirstName <email@gmail.com>"]
readme = "README.md"
packages = [{ include = "<project-module-name>", from = "src" }]
[tool.poetry.dependencies]
python = "^3.12"
fastapi = "^0.109.0"
uvicorn = { extras = ["standard"], version = "^0.25.0" }
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
# script
[tool.poetry.scripts]
dev = "src.<project-module-name>.main:dev"
start = "src.<project-module-name>.main:app"
xxxxxxxxxx
pip install fastapi
pip install uvicorn # ASGI server
pip install starlette # lightweight ASGI framework/toolkit
pip install pydantic # Data validation and type annotations
# OR
pip install fastapi uvicorn starlette pydantic
xxxxxxxxxx
import uvicorn
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
async def root():
return {"message": "Hello World"}
if __name__ == '__main__':
uvicorn.run(app, port=8080, host="0.0.0.0")
xxxxxxxxxx
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
async def root():
return {"message": "Hello World"}
xxxxxxxxxx
from fastapi import FastAPI
from fastapi.testclient import TestClient
app = FastAPI()
@app.get("/")
async def read_main():
return {"msg": "Hello World"}
client = TestClient(app)
def test_read_main():
response = client.get("/")
assert response.status_code == 200
assert response.json() == {"msg": "Hello World"}
xxxxxxxxxx
from typing import Union
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
async def read_root():
return {"Hello": "World"}
@app.get("/items/{item_id}")
async def read_item(item_id: int, q: Union[str, None] = None):
return {"item_id": item_id, "q": q}
xxxxxxxxxx
from fastapi import FastAPI
import uvicorn
from sklearn.datasets import load_iris
from sklearn.naive_bayes import GaussianNB
from pydantic import BaseModel
# Creating FastAPI instance
app = FastAPI()
# Creating class to define the request body
# and the type hints of each attribute
class request_body(BaseModel):
sepal_length : float
sepal_width : float
petal_length : float
petal_width : float
# Loading Iris Dataset
iris = load_iris()
# Getting our Features and Targets
X = iris.data
Y = iris.target
# Creating and Fitting our Model
clf = GaussianNB()
clf.fit(X,Y)
# Creating an Endpoint to receive the data
# to make prediction on.
@app.post('/predict')
def predict(data : request_body):
# Making the data in a form suitable for prediction
test_data = [[
data.sepal_length,
data.sepal_width,
data.petal_length,
data.petal_width
]]
# Predicting the Class
class_idx = clf.predict(test_data)[0]
# Return the Result
return { 'class' : iris.target_names[class_idx]}