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