You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/tutorial/fastapi/simple-hero-api.md
+12-28Lines changed: 12 additions & 28 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,10 +8,6 @@ The first step is to install FastAPI.
8
8
9
9
FastAPI is the framework to create the **web API**.
10
10
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
-
15
11
Make sure you create a [virtual environment](../../virtual-environments.md){.internal-link target=_blank}, activate it, and then install them, for example with:
16
12
17
13
<divclass="termy">
@@ -138,60 +134,48 @@ In this simple example, we just create the new sessions manually in the **path o
138
134
139
135
In future examples later we will use a <ahref="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. 🤓
140
136
141
-
## Run the **FastAPI**Application
137
+
## Run the **FastAPI**Server in Development Mode
142
138
143
139
Now we are ready to run the FastAPI application.
144
140
145
141
Put all that code in a file called `main.py`.
146
142
147
-
Then run it with **Uvicorn**:
143
+
Then run it with the `fastapi` <abbrtitle="Command Line Interface">CLI</abbr>, in development mode:
148
144
149
145
<divclass="termy">
150
146
151
147
```console
152
-
$ uvicorn main:app
148
+
$ fastapi dev main.py
153
149
154
150
<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.
*`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 <ahref="https://www.uvicorn.org/"class="external-link"target="_blank">Uvicorn</a> underneath.
169
158
170
159
///
171
160
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. 🤓
173
162
174
-
During development (and only during development), you can also add the option `--reload` to Uvicorn.
163
+
## Run the **FastAPI** Server in Production Mode
175
164
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`:
177
168
178
169
<divclass="termy">
179
170
180
171
```console
181
-
$ uvicorn main:app --reload
172
+
$ fastapi run main.py
182
173
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.
<span style="color: green;">INFO</span>: Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)
189
175
```
190
176
191
177
</div>
192
178
193
-
Just remember to never use `--reload` in production, as it consumes much more resources than necessary, would be more error prone, etc.
194
-
195
179
## Check the API docs UI
196
180
197
181
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*:
212
196
213
197
## Check the Database
214
198
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>.
216
200
217
201
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. 🎉
0 commit comments