Modern APIs with FastAPI and Python Transcripts
Chapter: Building our first API
Lecture: A home page, of sorts
Login or purchase this course to watch this video and the rest of the course contents.
0:00 There is one more thing I think we should check out and fix before we move on from this simple calculator API. Recall when we run it,
0:09 we can go over and we can go toe whatever this URL port is here, "/api/calculate" and pass X and Y and that works great.
0:17 But if we just go to the server, something bad happens. We get a "404 not found" right? See this? And if we actually pulled up the dev tools,
0:26 you would see the response code, it's 404 not found. That is not ideal for just the home page of the domain name of your API.
0:34 So let's take the smallest measures here, and we'll take bigger measures later to improve upon this.
0:39 So let's just do a little bit of tiny work here so that we can have something. So let's go
0:44 API, and we're gonna do a get to forward slash. Whatever we write next is what's gonna happen when you just request the server by itself.
0:51 So I'm gonna create something called "index" and I'm gonna just paste some text here. I'm gonna have a body which has some HTML, the body,
0:59 then the head, and then a div, and maybe it even needs a "<HTML> </HTML>" to be proper, even though it would have worked before.
1:08 And we can just use what we knew before, we can just return a "fastapi.responses.htmlresponse()" and we want to set the content
1:17 is this body. Alright, that's it. And with we had a little continuation character there. Now, now that's it. Nothing else got messed up.
1:26 Okay, good. So if we just rerun this and we click here now, Yes! Welcome to the super fancy API. And now if we click on that,
1:35 it takes us over to our actual API. Because it just seemed wrong that you opened up the server, and it just says "crash, this is not working".
1:43 So here's a little tiny bit of work that we can do, stuff we've already discussed of just passing back some alternative response besides JSON and,
1:52 you know, hooking this. Actually, this is the gateway to having a whole bunch of pages and stuff on our site
1:57 if we wanted, we're going to talk more about using things like Jinja templates, and maybe even better templates, later.
2:03 But for now, this will at least let our site work in a way so it doesn't look broken, and then we can go on and work with our API
2:11 here. So that's a quick introduction to FastAPI. You can see it's
2:15 super clean, super easy, and there's tons of neat stuff that we haven't even gotten to yet.