Update FastAPI guide for virtual projects and use `uv init` to create the `pyproject.toml` (#6752)

Follows https://github.com/astral-sh/uv/pull/6751

Matches https://github.com/astral-sh/uv-fastapi-example/pull/3
This commit is contained in:
Zanie Blue 2024-08-28 10:33:46 -05:00 committed by GitHub
parent 50c4341cc6
commit 37e29f1109
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 17 additions and 5 deletions

View File

@ -27,8 +27,20 @@ follows:
│ └── admin.py │ └── admin.py
``` ```
To migrate this project to uv, add a `pyproject.toml` file to the root directory of the project, as To migrate this project to uv, add a `pyproject.toml` file to the root directory of the project with
a sibling to the `app` directory. `uv init`:
```console
$ uv init
$ uv add fastapi --extra standard
```
!!! tip
If you have an existing `pyproject.toml`, `uv init` cannot be used — but you may not need to
make any changes to the file.
You should now have the following structure:
```plaintext ```plaintext
. .
@ -46,11 +58,11 @@ a sibling to the `app` directory.
│ └── admin.py │ └── admin.py
``` ```
The contents of the `pyproject.toml` file should look something like this: And the contents of the `pyproject.toml` file should look something like this:
```toml title="pyproject.toml" ```toml title="pyproject.toml"
[project] [project]
name = "app" name = "uv-fastapi-example"
version = "0.1.0" version = "0.1.0"
description = "FastAPI project" description = "FastAPI project"
readme = "README.md" readme = "README.md"
@ -63,7 +75,7 @@ dependencies = [
From there, you can run the FastAPI application with: From there, you can run the FastAPI application with:
```console ```console
$ uv run fastapi dev app/main.py $ uv run fastapi dev
``` ```
`uv run` will automatically resolve and lock the project dependencies (i.e., create a `uv.lock` `uv run` will automatically resolve and lock the project dependencies (i.e., create a `uv.lock`