Full Web Apps with FastAPI Transcripts
Chapter: Creating our first FastAPI *site*
Lecture: A basic FastAPI app

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now that we've got our Python project set up in Pycharm, let's make it a FastAPI project. The first thing we need to do is go to
0:08 our requirements here and have our requirements stated. So to get started, we need just two,
0:14 and we'll keep adding onto this list as we start to bring in things like an ORM with SQLAlchemy, as we bring in static files support with aiofiles
0:25 and so on. But we can start with FastAPI, and we can also start with you Uvicorn. So Uvicorn is the web server, FastAPI is the web framework.
0:37 PyCharm thinks this is misspelled. It is not. So we can go over here and hit Alt + Enter and say Do not tell me this is misspelled.
0:44 They're actually working on a feature, to understand these packages and not say that they're out of date or whatever,
0:49 but there we go. Now we need to install these. So we're gonna go to our terminal, make sure your virtual environment is active here.
0:56 I want to say pip install -r requirements.txt right. Looks like everything installed just fine. I'm not a fan of the red.
1:12 Let's go ahead and commit these to GitHub here and notice over here, we've got our source control thing. We can do this to commit.
1:20 It shows us Command+K on macOS is the hotkey. But I've also installed this tool called Presentation Assistant.
1:28 When I click this, Note at the bottom we got this. But also, if I just hit Command+K, this would come up. So you'll see me do a lot of things with hot
1:35 keys as I interact with PyCharm in general, in the code and so on. So if you're not sure what just happened, keep an eye on
1:40 that thing in the bottom. Now, down here. We've got these two files. These call this starter code for our project.
1:49 Here we go. Now things don't look broken with red everywhere. But how do we create a FastAPI project?
1:55 Well, there's a really simple, what I would call the PyCon talk tutorial style where you just put everything into the main file and,
2:03 hey, you have a whole app. Look how simple it is. And there's the realistic one where you break stuff into
2:08 you know, isolating the views into their own parts, the ability to test the data exchange between the views and the templates.
2:15 And you got the template folder and all that. We're going to start with the simple one,
2:18 and we're gonna throughout this entire course move towards what? Maybe we should call the real world one where you actually organize a big,
2:27 large, FastAPI web application. We're gonna start by saying import fastapi and we're also gonna need while I'm up here uvicorn to run it.
2:37 Now, if you've ever worked with Flask, working with FastAPI, is very similar. It's not the same, but it is similar to what you would do with Flask.
2:45 So we're gonna create an app. The way we do that, is we say fastapi.FastAPI
2:50 And then we're gonna need to create a function that is called when a page is requested. So we're gonna gonna call that index.
2:57 That's just like forward slash And what are we gonna do? Let's just return something really simple.
3:03 "Hello, world". It might not do exactly what you're expecting, but we'll see. Okay, and then we need to decorate this function to say
3:11 this is not a regular function, but a function on the web. We'll say app dot get. Notice all the common HTTP verbs here get put post delete so on.
3:22 And then we specify the url. You can see there's an insane number of options. We only care about specifying the path, so this will be fine.
3:30 Bring that up. And how is everything looking? It looks pretty good, but there's one more thing to do. We need to actually
3:38 run our API. So we say uvicorn.run and we just give it the app and that's it. We've built a FastAPI web API so far as you'll see.
3:49 Let's go ahead and run this. Here it is running down there like that. Hello, world. Wait a minute, Look carefully. What is all this JSON Raw data.
4:02 Why does it say JSON? It says JSON, because FastAPI is most natively and API. It's here to build APIs that exchange data.
4:13 So, for example, if we had said something different here like we had said The message is, Hello, world and we run it again and we look at the raw data,
4:26 you can see it's returning JSON here. Okay, So one of the things that we need to do for this course is
4:32 to tell FastAPI for certain parts, may be much of it, maybe just a little part. Whatever part that we want to be an HTML browser
4:41 oriented web application, we need to tell it. Don't just, don't return JSON. No, no, no. Return HTML and ideally,
4:48 use a nice structured template language like Chameleon or Jinja or something along those lines.
4:55 But you can see we've got our little FastAPI app up and running here on locahost. All we have to do import fastapi, create an instance
5:05 of it, use its HTTP verb decorators to indicate which methods are web methods and then run, that's it.


Talk Python's Mastodon Michael Kennedy's Mastodon