Full Web Apps with FastAPI Transcripts
Chapter: Course conclusion and review
Lecture: Our first site, a minimal FastAPI website
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
The first major thing that we covered was, how do we get started with FastAPI? Let's build the absolute most minimalistic web application,
0:08
not just API endpoint, but web application with FastAPI. And the fact that it fits in that square
0:13
grey box that we're about to light up, that tells you, it's not too complicated, is it? So we're gonna need two things to get started.
0:20
We've gotta import fastapi so we can use it. And then, in order to run the application created with FastAPI, we need a web server.
0:27
So we're gonna use uvicorn, which is a fantastic, production grade, ASGI, Asynchronous Web Service
0:33
Gateway Interface Server. We start by creating an app instance from the FastAPI, our FastAPI instance. I'm calling it app here,
0:43
sometimes it's called api, sometimes it's called app. Doesn't really matter as long as
0:46
you're consistent. Then we define a function that's gonna be called for a given URL when we request it with a web browser. So we say app.get("/").
0:55
That means if we do a basic normal HTTP request against the, just the server. This is what's going to run, not a POST or other HTTP verbs, just a GET
1:05
standard web request. And then, in here, we're going to generate a message very, very simple in this case, we just have a header that says "Hello
1:12
web!". Now remember, if we return just the string by itself, return "<h1>..." and so on, FastAPI
1:19
is an API framework first, with this ability to do HTML second. So if we did it that way, we would get a JSONResponse, which has a string "Hello,
1:30
web!". That's not at all what we want. We wanna have a web page, not an API response,
1:36
right? So we're gonna use one of the specialized responses, it really comes from Starlette, but we'll find that under fastapi.responses.htmlResponse.
1:45
Doing it this way, we tell the web browser what you're getting back is a web page, not some JSON data exchange. This is gonna be our home page here.
1:56
Not very impressive as you saw it when we got started, but we built on it throughout the course, and then finally, in order to run our application,
2:03
unlike Flask, you can't say app.run(). Instead, you say uvicorn.run(), and then, if you'd like, you can specify the host and the port.
2:12
The port defaults to 8000. The host, I believe, defaults to 0.0.0.0 or listen on the Internet. Not a big fan of that,
2:20
so I'll just listen on localhost here.