starlette README

The project's README from github.com/Kludex/starlette. As one markdown file: README.md.

starlette-logo

✨ The little ASGI framework that shines. ✨


Build Status Package version Supported Python Version Discord


Documentation: https://starlette.dev

Source Code: https://github.com/Kludex/starlette


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:

Installation

$ pip install starlette

You'll also want to install an ASGI server, such as uvicorn or any of the other ASGI server implementations.

$ pip install uvicorn

Example

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 main:app

Dependencies

Starlette only requires anyio, and the following are optional:

You can install all of these with pip 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 reusable 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.

— ⭐️ —

starlette (Kludex/starlette)

Searching Database…

12.7K ·GitHub
这是什么?Starlette 是一个轻量级的 ASGI 框架和工具包,用于在 Python 中构建异步 Web 服务——体积小、可用于生产环境,而且硬性依赖很少。
它是一个完整的框架,还是可以按需取用的工具包?两者都是。你既可以把它当作完整的框架使用,也可以当作 ASGI 工具包使用,它的任何组件都可以单独拿出来用。
怎么把一个基本的应用跑起来?先用 pip 安装 Starlette,然后用 uvicorn 这类 ASGI 服务器来运行你的应用(例如 uvicorn example:app),开发时可以加上 --reload 参数,这样改了代码会自动重新加载。
它会装哪些依赖?只有 anyio 是必需的;httpx、jinja2、python-multipart、itsdangerous、pyyaml 和 opentelemetry-api 都是可选的额外依赖。用 pip install starlette[full] 可以把所有依赖装齐。
它真的能用于生产环境,还是只是个极简的小玩具?它是可以用于生产环境的:测试覆盖率 100%,代码库全部带类型注解,硬性依赖很少,而且在独立基准测试中整体性能表现出色。
开箱即用都有哪些功能?支持 WebSocket、进程内后台任务、启动和关闭事件、测试客户端、CORS、GZip、静态文件、流式响应,还有会话和 Cookie 支持。
它能配合 asyncio 用吗?还是可以用 trio?都可以——Starlette 兼容 asyncio 和 trio 后端,所以你已经在用的异步技术栈可以继续保留。
它性能怎么样?README 中提到它在多项独立基准测试中整体性能表现出色,这与其轻量、低依赖的设计思路相符。
它的中间件和组件能和其他 ASGI 框架共用吗?可以——它的模块化设计促进了可复用组件在任意 ASGI 框架之间共享,而且清晰的 API 分层让每个部分都能独立理解。
What are similar projects?aiohttp, axum, actix-web, uvicorn, koa, ktor, echo, sanic
Creator
Kludex
Size
8.4 MB
Stars
12.7K
Age
8y
Commits · 90 Days