its other functionality.
```python
+from contextlib import asynccontextmanager
+
from starlette.applications import Starlette
from starlette.responses import PlainTextResponse
from starlette.routing import Route, Mount, WebSocketRoute
await websocket.send_text('Hello, websocket!')
await websocket.close()
-def startup():
- print('Ready to go')
+@asyncontextmanager
+async def lifespan(app):
+ print('Startup')
+ yield
+ print('Shutdown')
routes = [
Mount('/static', StaticFiles(directory="static")),
]
-app = Starlette(debug=True, routes=routes, on_startup=[startup])
+app = Starlette(debug=True, routes=routes, lifespan=lifespan)
```
### Instantiating the application
----
-hide: navigation
----
-
<p align="center">
<img width="400px" src="/img/starlette.svg#only-light" alt="starlette"/>
<img width="400px" src="/img/starlette_dark.svg#only-dark" alt="starlette"/>
## Installation
```shell
-$ pip install starlette
+pip install starlette
```
You'll also want to install an ASGI server, such as [uvicorn](https://www.uvicorn.org/), [daphne](https://github.com/django/daphne/), or [hypercorn](https://hypercorn.readthedocs.io/en/latest/).
```shell
-$ pip install uvicorn
+pip install uvicorn
```
## Example
-```python title="example.py"
+```python title="main.py"
from starlette.applications import Starlette
from starlette.responses import JSONResponse
from starlette.routing import Route
Then run the application...
```shell
-$ uvicorn example:app
+uvicorn main:app
```
-For a more complete example, [see here](https://github.com/encode/starlette-example).
-
## Dependencies
Starlette only requires `anyio`, and the following dependencies are optional:
Starlette is designed to be used either as a complete framework, or as
an ASGI toolkit. You can use any of its components independently.
-```python
+```python title="main.py"
from starlette.responses import PlainTextResponse
await response(scope, receive, send)
```
-Run the `app` application in `example.py`:
+Run the `app` application in `main.py`:
```shell
-$ uvicorn example:app
+$ uvicorn main:app
INFO: Started server process [11509]
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
```
repo: fontawesome/brands/github
features:
- content.code.copy
- - navigation.tabs
- toc.follow
repo_name: encode/starlette
edit_uri: edit/master/docs/
nav:
- - Home: "index.md"
+ - Introduction: "index.md"
- Features:
- Applications: "applications.md"
- Requests: "requests.md"