diff --git a/docs/guides/integration/fastapi.md b/docs/guides/integration/fastapi.md index 4fd923e51f19..c2d6aef3e5ca 100644 --- a/docs/guides/integration/fastapi.md +++ b/docs/guides/integration/fastapi.md @@ -8,32 +8,36 @@ environments, running FastAPI applications, and more. You can view the source code for this guide in the [uv-fastapi-example](https://github.com/astral-sh/uv-fastapi-example) repository. +## Migrating an existing FastAPI project + As an example, consider the sample application defined in the [FastAPI documentation](https://fastapi.tiangolo.com/tutorial/bigger-applications/), structured as follows: ```plaintext -. -├── app -│ ├── __init__.py -│ ├── main.py -│ ├── dependencies.py -│ └── routers -│ │ ├── __init__.py -│ │ ├── items.py -│ │ └── users.py -│ └── internal -│ ├── __init__.py -│ └── admin.py +project +└── app + ├── __init__.py + ├── main.py + ├── dependencies.py + ├── routers + │ ├── __init__.py + │ ├── items.py + │ └── users.py + └── internal + ├── __init__.py + └── admin.py ``` -To use uv with this project, add a `pyproject.toml` file to the root directory of the project with -`uv init`: +To use uv with this application, inside the `project` directory run: ```console -$ uv init +$ uv init --app ``` +This creates an [Application project](../../concepts/projects.md#applications) with a +`pyproject.toml` file. + Then, add a dependency on FastAPI: ```console @@ -43,19 +47,19 @@ $ uv add fastapi --extra standard You should now have the following structure: ```plaintext -. +project ├── pyproject.toml -├── app -│ ├── __init__.py -│ ├── main.py -│ ├── dependencies.py -│ └── routers -│ │ ├── __init__.py -│ │ ├── items.py -│ │ └── users.py -│ └── internal -│ ├── __init__.py -│ └── admin.py +└── app + ├── __init__.py + ├── main.py + ├── dependencies.py + ├── routers + │ ├── __init__.py + │ ├── items.py + │ └── users.py + └── internal + ├── __init__.py + └── admin.py ``` And the contents of the `pyproject.toml` file should look something like this: @@ -82,6 +86,8 @@ $ uv run fastapi dev alongside the `pyproject.toml`), create a virtual environment, and run the command in that environment. +Test the app by opening http://127.0.0.1:8000/?token=jessica in a web browser. + ## Deployment To deploy the FastAPI application with Docker, you can use the following `Dockerfile`: @@ -103,4 +109,21 @@ RUN uv sync --frozen --no-cache CMD ["/app/.venv/bin/fastapi", "run", "app/main.py", "--port", "80", "--host", "0.0.0.0"] ``` -For more on using uv with Docker, see the [Docker guide](./docker.md). +Build the Docker image with: + +```console +$ docker build -t fastapi-app . +``` + +Run the Docker container locally with: + +```console +$ docker run -p 8000:80 fastapi-app +``` + +Navigate to http://127.0.0.1:8000/?token=jessica in your browser to verify that the app is running +correctly. + +!!! tip + + For more on using uv with Docker, see the [Docker guide](./docker.md).