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,
0:02 "It looks close, but it's missing styles and images and stuff"?
0:05 Well, look down here. This is a bit of a
0:08 hint on what might be the problem.
0:10 Well, we got slash, the content, 200
0:13 ok, but static CSS themes,
0:16 404. Static image cloud, 404. Now,
0:19 some frameworks, if you just have a static folder,
0:22 it'll just start using it. FastAPI
0:24 is not like that.
0:25 You have to opt in to using it. So the way that we opt in
0:28 is we go over here, to
0:30 "api.mount" and we're gonna say where, so "/static" then how, we say
0:35 "StaticFiles", and then this class
0:39 we tell it where is the directory?
0:40 It's going to be "static". And we also have to set a name for the
0:44 mount, which will be "static".
0:46 And I think this might do it.
0:47 Let's go ahead and put this up here together.
0:49 We're gonna organize this a little bit better in a minute,
0:51 but let's just re-run this. Now,
0:53 like with Jinja, it requireds some base things,
0:56 say the Jijnja library. In order to serve static files asynchronously,
1:01 we need to add something called "aiofiles"
1:04 which is an async library for well,
1:06 you guessed it, working with files.
1:08 Let's go over to our requirements once again,
1:10 put the next thing in. No
1:12 PyCharm, It's not misspelled.
1:14 Go. Install that. Work, try again.
1:16 Okay, here we go. What has happened?
1:19 Did it work? No, because I don't have anything in there yet.
1:22 So it is having support for those files
1:25 if they were there, let me copy them over.
1:29 So now in here we have our CSS files and we have our images.
1:33 We even have a favicon
1:34 which we're not using yet, but we will. Now if I just go back and
1:37 refresh it. Yes. Look at that.
1:39 Isn't that slick? Yeah, I think it's coming along quite well.
1:43 So we're off to a good start.
1:45 I think having our static files in place is an important part for doing anything to
1:51 do with HTML. They might make sense when you're talking about some kind of
1:55 API, but you can always just do a file response there.
1:58 But in our html, when we want to say things like "/static
2:03 /css" and whatnot, we really need some way to have support for these
2:07 static files, and so we got, over here,
2:10 we've got our mount for the static directory,
2:13 serving this up with static files using aiofiles
2:16 so it's nice and quick similarly to how we did it with Jinja templates.
2:20 Well, we kind of added a whole Web framework to
2:22 FastAPI, which is cool.
2:24 And now I think we're ready to start building the API proper,
2:27 but foundation is set. We do need to do some organization on this.
2:31 Like, what I really don't want is to have the entire application just written in
2:35 this one main.py. That would be really bad idea,
2:38 but we'll start organizing these things later as we have a little bit more to work with.