Pages

Topics

Tools

↑↓navigate↵openescclose

Python guides

Every published Python guide, newest first, with practical examples you can copy into your own project.

3,965 guides across 14 topics

Topics

Latest Python guides

Illustration of a Flask route receiving an HTTP request and returning a JSON response, showing the request-response cycle in a Python web API.
Python

Python Flask Routes: Handling Requests and JSON Responses

Build Flask routes that handle HTTP requests and return JSON, with request parsing, status codes, error handling, validation, and production tips.

FlaskJSONREST APIHTTP Requests
Diagram showing FastAPI generating a customized OpenAPI schema and Swagger UI documentation interface.
Python

Python FastAPI OpenAPI and Swagger Customization

python fastapi openapi and swagger customization: Learn how to customize the OpenAPI schema and Swagger UI in FastAPI: metadata, schema overrides, docs endpoints, and...

FastAPIOpenAPISwagger UIAPI Documentation
A stylized test tube with a FastAPI logo and a pytest checkmark, symbolizing API testing.
Python

Python FastAPI Testing with pytest

python fastapi testing with pytest: Learn how to test FastAPI applications with pytest: TestClient, dependency overrides, async tests, and database isolation.

FastAPIpytestTestClientasync testing
Illustration of a FastAPI pagination endpoint showing a list of items with offset and cursor navigation arrows.
Python

Python FastAPI Pagination: Offset vs Cursor

Learn how to implement offset- and cursor-based pagination in Python FastAPI, including response models, validation, and performance tradeoffs.

FastAPIPaginationSQLAlchemyREST API
Illustration of FastAPI lifespan managing startup and shutdown events with an async context manager.
Python

Python FastAPI Lifespan: Startup and Shutdown

Use FastAPI's lifespan async context manager to initialize resources on startup and clean them up on shutdown. Includes resource ordering, error handling, testing, and production cleanup.

FastAPIlifespanstartupshutdown
Diagram of FastAPI request flowing through an async SQLAlchemy session to a database with a non-blocking event loop.
Python

Python FastAPI Async SQLAlchemy Database Setup

Set up asynchronous SQLAlchemy in FastAPI: configure an async engine and session factory, use dependency injection to manage request-scoped sessions, and avoid blocking the event loop.

FastAPISQLAlchemyAsyncDatabase
Diagram showing a FastAPI application split into modular routers and project folders
Python

Python FastAPI Routers and Project Structure

Learn how to organize a FastAPI application with routers, structure files for maintainability, and handle dependencies without common pitfalls.

FastAPIAPIRouterProject OrganizationPython Web Development
Diagram comparing a one-way streaming HTTP response with a bidirectional persistent WebSocket connection in FastAPI
Python

FastAPI WebSocket vs Streaming Responses

Compare FastAPI WebSockets and streaming responses: how each works, when to choose one over the other, and how to implement both with code examples.

FastAPIWebSocketStreamingResponseAsync Python
Illustration of a FastAPI request triggering a background task that runs asynchronously after the response.
Python

Python FastAPI Background Tasks: Built-in vs Celery

Compare FastAPI's built-in BackgroundTasks with Celery for background work. See code examples, execution-model details, retry behavior, and production tradeoffs.

FastAPIBackground TasksCeleryAsync
Illustration of a FastAPI exception handler intercepting a custom error and converting it into a structured HTTP response
Python

Python FastAPI Exception Handlers and Custom Errors

How to define custom exception classes, register exception handlers, and return structured JSON error responses in FastAPI.

FastAPIException HandlingCustom ExceptionsHTTP Responses