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