From: Sebastián Ramírez Date: Sun, 27 Apr 2025 13:19:59 +0000 (+0200) Subject: 📝 Update install and usage with FastAPI CLI in FastAPI tutorial (#1350) X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=dd03d05889c9dc10521c0632a6e2c5c5e6ca8a56;p=thirdparty%2Ffastapi%2Fsqlmodel.git 📝 Update install and usage with FastAPI CLI in FastAPI tutorial (#1350) --- diff --git a/docs/tutorial/fastapi/simple-hero-api.md b/docs/tutorial/fastapi/simple-hero-api.md index c6f27f4b..163fa281 100644 --- a/docs/tutorial/fastapi/simple-hero-api.md +++ b/docs/tutorial/fastapi/simple-hero-api.md @@ -8,10 +8,6 @@ The first step is to install FastAPI. FastAPI is the framework to create the **web API**. -But we also need another type of program to run it, it is called a "**server**". We will use **Uvicorn** for that. And we will install Uvicorn with its *standard* dependencies. - -Then install FastAPI. - Make sure you create a [virtual environment](../../virtual-environments.md){.internal-link target=_blank}, activate it, and then install them, for example with:
@@ -138,60 +134,48 @@ In this simple example, we just create the new sessions manually in the **path o In future examples later we will use a FastAPI Dependency to get the **session**, being able to share it with other dependencies and being able to replace it during testing. 🤓 -## Run the **FastAPI** Application +## Run the **FastAPI** Server in Development Mode Now we are ready to run the FastAPI application. Put all that code in a file called `main.py`. -Then run it with **Uvicorn**: +Then run it with the `fastapi` CLI, in development mode:
```console -$ uvicorn main:app +$ fastapi dev main.py INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit) -INFO: Started reloader process [28720] -INFO: Started server process [28722] -INFO: Waiting for application startup. -INFO: Application startup complete. ```
/// info -The command `uvicorn main:app` refers to: - -* `main`: the file `main.py` (the Python "module"). -* `app`: the object created inside of `main.py` with the line `app = FastAPI()`. +The `fastapi` command uses Uvicorn underneath. /// -### Uvicorn `--reload` +When you use `fastapi dev` it starts Uvicorn with the option to reload automatically every time you make a change to the code, this way you will be able to develop faster. 🤓 -During development (and only during development), you can also add the option `--reload` to Uvicorn. +## Run the **FastAPI** Server in Production Mode -It will restart the server every time you make a change to the code, this way you will be able to develop faster. 🤓 +The development mode should not be used in production, as it includes automatic reload by default it consumes much more resources than necessary, and it would be more error prone, etc. + +For production, use `fastapi run` instead of `fastapi dev`:
```console -$ uvicorn main:app --reload +$ fastapi run main.py -INFO: Will watch for changes in these directories: ['/home/user/code/sqlmodel-tutorial'] -INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit) -INFO: Started reloader process [28720] -INFO: Started server process [28722] -INFO: Waiting for application startup. -INFO: Application startup complete. +INFO: Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit) ```
-Just remember to never use `--reload` in production, as it consumes much more resources than necessary, would be more error prone, etc. - ## Check the API docs UI Now you can go to that URL in your browser `http://127.0.0.1:8000`. We didn't create a *path operation* for the root path `/`, so that URL alone will only show a "Not Found" error... that "Not Found" error is produced by your FastAPI application. @@ -212,7 +196,7 @@ And then you can get them back with the **Read Heroes** *path operation*: ## Check the Database -Now you can terminate that Uvicorn server by going back to the terminal and pressing Ctrl+C. +Now you can terminate that server program by going back to the terminal and pressing Ctrl+C. And then, you can open **DB Browser for SQLite** and check the database, to explore the data and confirm that it indeed saved the heroes. 🎉