Modern APIs with FastAPI and Python Transcripts
Chapter: Building a realistic API
Lecture: Static files

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Recall when we rendered our HTML and I said, "It looks close, but it's missing styles and images and stuff"? Well, look down here. This is a bit of a
0:09 hint on what might be the problem. Well, we got slash, the content, 200 ok, but static CSS themes, 404. Static image cloud, 404. Now,
0:20 some frameworks, if you just have a static folder, it'll just start using it. FastAPI is not like that.
0:26 You have to opt in to using it. So the way that we opt in is we go over here, to "api.mount" and we're gonna say where, so "/static" then how, we say
0:36 "StaticFiles", and then this class we tell it where is the directory? It's going to be "static". And we also have to set a name for the
0:45 mount, which will be "static". And I think this might do it. Let's go ahead and put this up here together.
0:50 We're gonna organize this a little bit better in a minute, but let's just re-run this. Now, like with Jinja, it requireds some base things,
0:57 say the Jijnja library. In order to serve static files asynchronously, we need to add something called "aiofiles" which is an async library for well,
1:07 you guessed it, working with files. Let's go over to our requirements once again, put the next thing in. No PyCharm, It's not misspelled.
1:15 Go. Install that. Work, try again. Okay, here we go. What has happened? Did it work? No, because I don't have anything in there yet.
1:23 So it is having support for those files if they were there, let me copy them over. So now in here we have our CSS files and we have our images.
1:34 We even have a favicon which we're not using yet, but we will. Now if I just go back and refresh it. Yes. Look at that.
1:40 Isn't that slick? Yeah, I think it's coming along quite well. So we're off to a good start.
1:46 I think having our static files in place is an important part for doing anything to
1:52 do with HTML. They might make sense when you're talking about some kind of API, but you can always just do a file response there.
1:59 But in our HTML, when we want to say things like "/static /css" and whatnot, we really need some way to have support for these
2:08 static files, and so we got, over here, we've got our mount for the static directory, serving this up with static files using aiofiles
2:17 so it's nice and quick similarly to how we did it with Jinja templates. Well, we kind of added a whole Web framework to FastAPI, which is cool.
2:25 And now I think we're ready to start building the API proper, but foundation is set. We do need to do some organization on this.
2:32 Like, what I really don't want is to have the entire application just written in this one That would be really bad idea,
2:39 but we'll start organizing these things later as we have a little bit more to work with.

Talk Python's Mastodon Michael Kennedy's Mastodon