e01.org

FastAPI

FastAPI is a web framework for building HTTP-based service APIs in Python 3.8+. It uses Pydantic and type hints to validate, serialize and deserialize data. FastAPI also automatically generates OpenAPI documentation for APIs built with it. It was first released in 2018.

FastAPI is a web framework for building HTTP-based service APIs in Python 3.8+. It uses Pydantic and type hints to validate, serialize and deserialize data. FastAPI also automatically generates OpenAPI documentation for APIs built with it. It was first released in 2018. from fastapi import FastAPI from pydantic import BaseModel app = FastAPI() class Item(BaseModel): name: str price: float is_offer: bool | None = None @app.post("/items/") def create_item(item: Item): # The 'item' object is already validated and typed return {"message": "Item received", "item_name": item.name} from fastapi import Depends, HTTPException, status from db import DbSession --- Dependency for Database Session --- def get_db(): db = DbSession() try: yield db finally: db.close() @app.post("/items/", status_code=status.HTTP_201_CREATED) def create_item(name: str, description: str, db: DbSession = Depends(get_db)): new_item = Item(name=name, description=description) db.add(new_item) db.commit() db.refresh(new_item) return {"message": "Item created successfully!", "item": new_item} @app.get("/items/{item_id}") def read_item(item_id: int, db: DbSession = Depends(get_db)): item = db.query(Item).filter(Item.id == item_id).first() if item is None: raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Item not found") return item You must have 'websockets' package installed from fastapi import WebSocket @app.websocket("/ws") async def websocket_endpoint(websocket: WebSocket): await websocket.accept() while True: data = await websocket.receive_text() await websocket.send_text(f"Message text was: {data}") import time import shutil from fastapi import BackgroundTasks, UploadFile, File from utils import generate_thumbnail @app.post("/upload-image/") async def upload_image( image: UploadFile = File(...), background_tasks: BackgroundTasks ): file_location = f"uploaded_images/{image.filename}" # Save uploaded image with open(image_path, "wb") as f: contents = await file.read() f.write(contents) # Add thumbnail generation as a background task background_tasks.add_task(generate_thumbnail, file_location, "200x200") return {"message": f"Image '{image.filename}' uploaded. Thumbnail generation started in background."} Import FastAPI class from the fastapi package from fastapi import FastAPI Create an instance of the FastAPI app app = FastAPI() Define a GET route for the root URL ("/") @app.get("/") async def read_root() -> str: # Return a plain text response return "Hello, World!" ==External links== ==References==

source: e01 Encyclopedia · licence: CC BY-SA 4.0 · page · source revision

Cite this

"FastAPI". 1770280766. e01 Encyclopedia. CC BY-SA 4.0. https://en.wikipedia.org/w/index.php?oldid=1336720302

JSON record · API collection · citation policy