Skip to content

starlette

That shiny little ASGI framework.

Build Status Package version


introduce

Starlette is a lightweight ASGI framework/toolkit that is ideal for building asynchronous web services in Python.

It is ready for production and provides you with the following:

  • A lightweight, low-complexity HTTP networking framework.
  • WebSocket support.
  • Background tasks in the process.
  • Startup and shutdown events. The* test client is built on httpx.
  • CORS, GZip, Static File, Streaming Response.
  • Session and Cookie support.
  • 100% test coverage.
  • Code base for 100% type annotations.
  • There is almost no hard dependence.
  • is compatible with the asyncio and trio backends.
  • Excellent overall performance against stand-alone benchmarks.

ask for

Python 3.8 and above

install

$ pip3 install starlette

You also need to install an ASGI server, such as uvicorn, daphne, or hypercorn.

$ pip3 install uvicorn

give a typical example

example.py:

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


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


app = Starlette(debug=True, routes=[
    Route('/', homepage),
])

Then run the application…

$ uvicorn example:app

For a more complete example, see here.

dependencies

Starlette requires only anyio, the following dependencies are optional:

  • httpx-Required if you want to use TestClient.
  • jinja2-Required if you want to use Jinja2Templates.
  • python-multipart-If you want to support form parsing, you need to, with request.form().
  • itsdangerous-Required for SessionMiddleware support.
  • pyyaml-Required for SchemaGenerator support.

You can install all of these using pip3 install starlette[full].

Framework tool kit

Starlette is designed to be used both as a complete framework and as an ASGI toolkit. You can use any of its components independently.

from starlette.responses import PlainTextResponse


async def app(scope, receive, send):
    assert scope['type'] == 'http'
    response = PlainTextResponse('Hello, world!')
    await response(scope, receive, send)

Run the app application in example.py:

$ uvicorn example:app
INFO: Started server process [11509]
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)

Run uvicorn with --reload to enable automatic reload when code changes.

modularization

Starlette's modular design facilitates the construction of reusable components that can be shared across any ASGI framework. This should enable an ecosystem of shared middleware and mountable applications.

Clean API separation also means that it is easier to understand each component individually.


Starlette is code under the BSD license. Elaborate design and production. — ⭐️ —