Skip to content

Commit dd03d05

Browse files
authored
📝 Update install and usage with FastAPI CLI in FastAPI tutorial (#1350)
1 parent 49dd5ff commit dd03d05

File tree

1 file changed

+12
-28
lines changed

1 file changed

+12
-28
lines changed

docs/tutorial/fastapi/simple-hero-api.md

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ The first step is to install FastAPI.
88

99
FastAPI is the framework to create the **web API**.
1010

11-
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.
12-
13-
Then install FastAPI.
14-
1511
Make sure you create a [virtual environment](../../virtual-environments.md){.internal-link target=_blank}, activate it, and then install them, for example with:
1612

1713
<div class="termy">
@@ -138,60 +134,48 @@ In this simple example, we just create the new sessions manually in the **path o
138134

139135
In future examples later we will use a <a href="https://fastapi.tiangolo.com/tutorial/dependencies/" class="external-link" target="_blank">FastAPI Dependency</a> to get the **session**, being able to share it with other dependencies and being able to replace it during testing. 🤓
140136

141-
## Run the **FastAPI** Application
137+
## Run the **FastAPI** Server in Development Mode
142138

143139
Now we are ready to run the FastAPI application.
144140

145141
Put all that code in a file called `main.py`.
146142

147-
Then run it with **Uvicorn**:
143+
Then run it with the `fastapi` <abbr title="Command Line Interface">CLI</abbr>, in development mode:
148144

149145
<div class="termy">
150146

151147
```console
152-
$ uvicorn main:app
148+
$ fastapi dev main.py
153149

154150
<span style="color: green;">INFO</span>: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
155-
<span style="color: green;">INFO</span>: Started reloader process [28720]
156-
<span style="color: green;">INFO</span>: Started server process [28722]
157-
<span style="color: green;">INFO</span>: Waiting for application startup.
158-
<span style="color: green;">INFO</span>: Application startup complete.
159151
```
160152

161153
</div>
162154

163155
/// info
164156

165-
The command `uvicorn main:app` refers to:
166-
167-
* `main`: the file `main.py` (the Python "module").
168-
* `app`: the object created inside of `main.py` with the line `app = FastAPI()`.
157+
The `fastapi` command uses <a href="https://www.uvicorn.org/" class="external-link" target="_blank">Uvicorn</a> underneath.
169158

170159
///
171160

172-
### Uvicorn `--reload`
161+
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. 🤓
173162

174-
During development (and only during development), you can also add the option `--reload` to Uvicorn.
163+
## Run the **FastAPI** Server in Production Mode
175164

176-
It will restart the server every time you make a change to the code, this way you will be able to develop faster. 🤓
165+
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.
166+
167+
For production, use `fastapi run` instead of `fastapi dev`:
177168

178169
<div class="termy">
179170

180171
```console
181-
$ uvicorn main:app --reload
172+
$ fastapi run main.py
182173

183-
<span style="color: green;">INFO</span>: Will watch for changes in these directories: ['/home/user/code/sqlmodel-tutorial']
184-
<span style="color: green;">INFO</span>: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
185-
<span style="color: green;">INFO</span>: Started reloader process [28720]
186-
<span style="color: green;">INFO</span>: Started server process [28722]
187-
<span style="color: green;">INFO</span>: Waiting for application startup.
188-
<span style="color: green;">INFO</span>: Application startup complete.
174+
<span style="color: green;">INFO</span>: Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)
189175
```
190176

191177
</div>
192178

193-
Just remember to never use `--reload` in production, as it consumes much more resources than necessary, would be more error prone, etc.
194-
195179
## Check the API docs UI
196180

197181
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*:
212196

213197
## Check the Database
214198

215-
Now you can terminate that Uvicorn server by going back to the terminal and pressing <kbd>Ctrl+C</kbd>.
199+
Now you can terminate that server program by going back to the terminal and pressing <kbd>Ctrl+C</kbd>.
216200

217201
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. 🎉
218202

0 commit comments

Comments
 (0)