The little ASGI framework that shines. 🌟
Go to file
Michał Górny 94225d0901
Include all files in sdist archives (#2149)
Remove the restriction on files inclduded in sdist archives in order
to include documentation sources and tests there.  This is the default
hatchling behavior and it is helpful to packagers (such as Linux
distributions or Conda) as it permits using sdist archives to do
packaging (instead of GitHub archives that are not guaranteed
to be reproducible).  Most of the ordinary users will not be affected
since starlette is a pure Python package and therefore pip will prefer
wheels to install it everywhere.
2023-05-24 03:27:22 -06:00
.github Update pull_request_template.md (#2122) 2023-04-18 14:43:01 +02:00
docs Version 0.27.0 (#2147) 2023-05-16 17:56:45 +07:00
scripts Run black before ruff (#2143) 2023-05-04 21:05:27 +02:00
starlette Version 0.27.0 (#2147) 2023-05-16 17:56:45 +07:00
tests Merge pull request from GHSA-v5gw-mw7f-84px 2023-05-16 17:33:57 +07:00
.gitignore Update CI scripts to match httpcore (#1043) 2020-09-06 12:08:07 +02:00
LICENSE.md Update link to https (#356) 2019-01-25 23:12:32 +00:00
README.md Update link to jinja2 (#2043) 2023-02-20 11:14:47 +00:00
mkdocs.yml Replace reference from Events to Lifespan on the mkdocs.yml (#2072) 2023-03-10 00:20:06 +01:00
pyproject.toml Include all files in sdist archives (#2149) 2023-05-24 03:27:22 -06:00
requirements.txt Bump ruff from 0.0.260 to 0.0.263 (#2136) 2023-05-02 00:13:14 -06:00

README.md

starlette

The little ASGI framework that shines.

Build Status Package version


Documentation: https://www.starlette.io/


Starlette

Starlette is a lightweight ASGI framework/toolkit, which is ideal for building async web services in Python.

It is production-ready, and gives you the following:

  • A lightweight, low-complexity HTTP web framework.
  • WebSocket support.
  • In-process background tasks.
  • Startup and shutdown events.
  • Test client built on httpx.
  • CORS, GZip, Static Files, Streaming responses.
  • Session and Cookie support.
  • 100% test coverage.
  • 100% type annotated codebase.
  • Few hard dependencies.
  • Compatible with asyncio and trio backends.
  • Great overall performance against independent benchmarks.

Requirements

Python 3.7+ (For Python 3.6 support, install version 0.19.1)

Installation

$ pip3 install starlette

You'll also want to install an ASGI server, such as uvicorn, daphne, or hypercorn.

$ pip3 install uvicorn

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'})

routes = [
    Route("/", endpoint=homepage)
]

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

Then run the application using Uvicorn:

$ uvicorn example:app

For a more complete example, see encode/starlette-example.

Dependencies

Starlette only requires anyio, and the following are optional:

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

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

Framework or Toolkit

Starlette is designed to be used either as a complete framework, or 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 auto-reloading on code changes.

Modularity

The modularity that Starlette is designed on promotes building re-usable components that can be shared between any ASGI framework. This should enable an ecosystem of shared middleware and mountable applications.

The clean API separation also means it's easier to understand each component in isolation.


Starlette is BSD licensed code.
Designed & crafted with care.