Skip to content

Commit

Permalink
Add filename to code snippets in the documentation (#2062)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kludex authored Jul 30, 2023
1 parent c6eb011 commit 13588e5
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 29 deletions.
26 changes: 11 additions & 15 deletions docs/deployment.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ As a general rule, you probably want to:
Typically you'll run `uvicorn` from the command line.

```bash
$ uvicorn example:app --reload --port 5000
$ uvicorn main:app --reload --port 5000
```

The ASGI application should be specified in the form `path.to.module:instance.path`.
Expand Down Expand Up @@ -141,9 +141,7 @@ See the [settings documentation](settings.md) for more details on the supported

To run directly from within a Python program, you should use `uvicorn.run(app, **config)`. For example:

**example.py**:

```python
```py title="main.py"
import uvicorn

class App:
Expand All @@ -152,7 +150,7 @@ class App:
app = App()

if __name__ == "__main__":
uvicorn.run("example:app", host="127.0.0.1", port=5000, log_level="info")
uvicorn.run("main:app", host="127.0.0.1", port=5000, log_level="info")
```

The set of configuration options is the same as for the command line tool.
Expand Down Expand Up @@ -197,6 +195,8 @@ Gunicorn provides a different set of configuration options to Uvicorn, so some
If you need to pass uvicorn's config arguments to gunicorn workers then you'll have to subclass `UvicornWorker`:

```python
from uvicorn.workers import UvicornWorker

class MyUvicornWorker(UvicornWorker):
CONFIG_KWARGS = {"loop": "asyncio", "http": "h11", "lifespan": "off"}
```
Expand All @@ -210,14 +210,12 @@ To use `supervisor` as a process manager you should either:

A simple supervisor configuration might look something like this:

**supervisord.conf**:

```ini
```ini title="supervisord.conf"
[supervisord]

[fcgi-program:uvicorn]
socket=tcp://localhost:8000
command=venv/bin/uvicorn --fd 0 example:App
command=venv/bin/uvicorn --fd 0 main:App
numprocs=4
process_name=uvicorn-%(process_num)d
stdout_logfile=/dev/stdout
Expand All @@ -235,11 +233,9 @@ To use `circus` as a process manager, you should either:

A simple circus configuration might look something like this:

**circus.ini**:

```ini
```ini title="circus.ini"
[watcher:web]
cmd = venv/bin/uvicorn --fd $(circus.sockets.web) example:App
cmd = venv/bin/uvicorn --fd $(circus.sockets.web) main:App
use_sockets = True
numprocesses = 4

Expand Down Expand Up @@ -325,15 +321,15 @@ For local development with https, it's possible to use [mkcert][mkcert]
to generate a valid certificate and private key.

```bash
$ uvicorn example:app --port 5000 --ssl-keyfile=./key.pem --ssl-certfile=./cert.pem
$ uvicorn main:app --port 5000 --ssl-keyfile=./key.pem --ssl-certfile=./cert.pem
```

### Running gunicorn worker

It's also possible to use certificates with uvicorn's worker for gunicorn.

```bash
$ gunicorn --keyfile=./key.pem --certfile=./cert.pem -k uvicorn.workers.UvicornWorker example:app
$ gunicorn --keyfile=./key.pem --certfile=./cert.pem -k uvicorn.workers.UvicornWorker main:app
```

[nginx_websocket]: https://nginx.org/en/docs/http/websocket.html
Expand Down
28 changes: 14 additions & 14 deletions docs/index.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<p align="center">
<img width="320" height="320" src="https://raw.githubusercontent.com/tomchristie/uvicorn/master/docs/uvicorn.png" alt='uvicorn'>
<img width="320" height="320" src="../../uvicorn.png" alt='uvicorn'>
</p>

<p align="center">
Expand All @@ -13,6 +13,9 @@
<a href="https://pypi.org/project/uvicorn/">
<img src="https://badge.fury.io/py/uvicorn.svg" alt="Package version">
</a>
<a href="https://pypi.org/project/uvicorn" target="_blank">
<img src="https://img.shields.io/pypi/pyversions/uvicorn.svg?color=%2334D058" alt="Supported Python versions">
</a>
</p>

---
Expand All @@ -25,7 +28,7 @@ Until recently Python has lacked a minimal low-level server/application interfac
async frameworks. The [ASGI specification][asgi] fills this gap, and means we're now able to
start building a common set of tooling usable across all async frameworks.

Uvicorn currently supports HTTP/1.1 and WebSockets.
Uvicorn currently supports **HTTP/1.1** and **WebSockets**.

## Quickstart

Expand Down Expand Up @@ -59,9 +62,9 @@ Moreover, "optional extras" means that:
- `python-dotenv` will be installed should you want to use the `--env-file` option.
- `PyYAML` will be installed to allow you to provide a `.yaml` file to `--log-config`, if desired.

Create an application, in `example.py`:
Create an application:

```python
```python title="main.py"
async def app(scope, receive, send):
assert scope['type'] == 'http'

Expand All @@ -81,14 +84,14 @@ async def app(scope, receive, send):
Run the server:

```shell
$ uvicorn example:app
$ uvicorn main:app
```

---

## Usage

The uvicorn command line tool is the easiest way to run your application...
The uvicorn command line tool is the easiest way to run your application.

### Command line options

Expand Down Expand Up @@ -211,8 +214,7 @@ There are several ways to run uvicorn directly from your application.

If you're looking for a programmatic equivalent of the `uvicorn` command line interface, use `uvicorn.run()`:

```python
# main.py
```py title="main.py"
import uvicorn

async def app(scope, receive, send):
Expand All @@ -226,7 +228,7 @@ if __name__ == "__main__":

For more control over configuration and server lifecycle, use `uvicorn.Config` and `uvicorn.Server`:

```python
```py title="main.py"
import uvicorn

async def app(scope, receive, send):
Expand All @@ -240,7 +242,7 @@ if __name__ == "__main__":

If you'd like to run Uvicorn from an already running async environment, use `uvicorn.Server.serve()` instead:

```python
```py title="main.py"
import asyncio
import uvicorn

Expand Down Expand Up @@ -281,16 +283,14 @@ For more information, see the [deployment documentation](deployment.md).

The `--factory` flag allows loading the application from a factory function, rather than an application instance directly. The factory will be called with no arguments and should return an ASGI application.

**example.py**:

```python
```py title="main.py"
def create_app():
app = ...
return app
```

```shell
$ uvicorn --factory example:create_app
$ uvicorn --factory main:create_app
```

## The ASGI interface
Expand Down
6 changes: 6 additions & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ theme:
toggle:
icon: "material/lightbulb-outline"
name: "Switch to light mode"
features:
- content.code.copy


repo_name: encode/uvicorn
repo_url: https://github.com/encode/uvicorn
Expand All @@ -34,6 +37,9 @@ markdown_extensions:
css_class: highlight
- toc:
permalink: true
- pymdownx.inlinehilite
- pymdownx.snippets
- pymdownx.superfences

extra_javascript:
- "js/chat.js"
Expand Down

0 comments on commit 13588e5

Please sign in to comment.