Preamble

Starlette is a lightweight ASGI (Asynchronous Server Gateway Interface) framework/toolkit, which is ideal for building async web services in Python.

Starlette is an impressive and minimalist ASGI framework designed for building efficient and high-performance web services in Python.

Main Features

ASGI Compatibility:

Asynchronous Server Gateway Interface (ASGI): Starlette is built on the ASGI specification which provides an interface between asynchronous Python web servers and web applications or frameworks. It’s designed to be fully asynchronous and capable of handling a wide range of networking interfaces like HTTP, WebSockets, and more.

Routing:

  • URL Routing: Starlette provides a dynamic and flexible routing system for matching URL paths to Python functions. This feature supports path parameters, converters, and custom routes.

Request and Response Middleware:

  • Middleware: Starlette supports middleware for request and response interception. This allows for tasks like authentication, logging, and error handling before the actual endpoint handler is invoked.

Session Management:

  • Sessions and Cookies: It includes session and cookie management systems, allowing secure and stateful interactions with clients.

WebSockets:

  • WebSocket Support: Starlette has native support for handling WebSocket connections, enabling real-time communication between the client and server.

Static Files:

  • Static Files Handling: Starlette can serve static files including HTML, CSS, JavaScript, and other assets, making it easy to serve front-end applications or static content.

Background Tasks:

  • Background Task Management: Easily manage background tasks allowing for deferred, out-of-band processing of time-consuming operations.

GraphQL:

  • GraphQL Integration: It provides integration with GraphQL, supporting advanced API development paradigms.

Testing:

  • TestClient: Starlette includes tools for testing applications, including a `TestClient` for writing unit tests and integration tests.

CORS:

  • Cross-Origin Resource Sharing (CORS): Built-in middleware supports configuring CORS policies for secure cross-origin requests.

Underlying Concepts of Starlette:

ASGI:

  • The ASGI standard allows for an advanced concurrency model and greater scalability than WSGI (Web Server Gateway Interface). Starlette fully leverages ASGI by supporting async programming, making it ideal for I/O-bound and high-concurrency applications.

Event Loop:

  • At the core of Starlette's asynchronous capabilities is the event loop provided by Python's `asyncio` library. It allows Starlette to handle many simultaneous connections efficiently by managing asynchronous tasks.

Dependency Injection:

  • Starlette’s design promotes clean architecture and separation of concerns by supporting dependency injection patterns. This aids in creating modular and testable code.

ACID Transactions:

  • Starlette can be used with databases via async ORMs that support ACID transactions, providing transactional integrity and durability.

FastAPI Integration:

  • Starlette is also foundational for FastAPI, another popular web framework for building modern APIs with Python 3.6+ based on standard Python-type hints. By understanding Starlette, one inherently grasps significant parts of FastAPI as well.

Example Code:

Here's a simple example to illustrate the usage of Starlette:

from starlette.applications import Starlette
from starlette.responses import JSONResponse
from starlette.routing import Route
import uvicorn


async def homepage(request):
    return JSONResponse({'hello': 'world'})

routes = [Route('/', homepage), ]

app = Starlette(debug=True, routes=routes)

if __name__ == "__main__":
    uvicorn.run(app, host='127.0.0.1', port=8000)

Conclusion

Starlette's minimalistic yet powerful design makes it a robust choice for creating async-based web services in Python, whether as a standalone framework or as part of FastAPI for more feature-rich applications.