MongoDB with Async Python Transcripts
Chapter: FastAPI Example
Lecture: FastAPI Skeleton App
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
We're going to build a pretty simple but feature-rich FastAPI site here.
0:07
And what I mean by that is we're not going to skimp on having things like actually returning
0:12
a little HTML to help users of the API know what's going on, or properly factoring our
0:18
API so it's not just one ginormous single Python file, as well as generating API documentation with OpenAPI.
0:29
Now to get started, what we're going to do is take a copy of the old code.
0:33
For the most part, all the things we did previously, we would just kind of start over.
0:37
But like I said at the opening, we're now building on our previous work, we're now bringing
0:42
like everything we did in that CLI app, we're bringing it over here and just using it in the web instead of in a Tui.
0:49
So we're going to take this code, everything but the CLI itself, and we're going to just copy that over.
0:56
Now remember, when you have multiple files like this, and you've got basically multiple
1:02
projects or sub projects loaded in PyCharm, you need to come here right click, mark directory as unmarked as sources root. See there's a hotkey.
1:12
It's typically how I do it. Close that off over here and mark directory as sources root.
1:18
So when this service over here, says things like from models dot package, it knows, look
1:28
at the source route, go to that models that module, that package, okay.
1:33
So we're using the latest one, don't forget to do that, or it might still run, but just use the old models, which would be very confusing.
1:41
Alright, so we can close this off. we didn't have, we didn't copy over the CLI because typically in FastAPI, this is called
1:49
main and let me just set it to run real quick. So when I hit the Ctrl R hotkey, it runs the
1:56
correct thing you can see up here at the top, set to go. Okay, so how do we work with fast
2:03
API? Well, let's try this. Errors, because we haven't installed it yet. So one more round of setting up the requirements.
2:11
I'll put this up here, maybe we could alphabetize this a bit. Here we go.
2:17
Again, remember you don't really have to mess with this file unless you just want to use
2:20
this style because we're going to generate the requirements.txt file, then pip install-r requirements.
2:31
Here you can see now we have FastAPI and Starlet, the latest releases. So curious what has changed, we can come over here, see the diff.
2:45
So we got HTTP core starlet because of any IO FastAPI directly. Identic is also now requiring FastAPI or FastAPI is now requiring pedantic and so on.
2:57
Right. So that's how that's changed. But this is all good. In the examples, you'll see app equals FastAPI, not FastAPI.
3:08
this, you can see there's a couple, just a couple of arguments that go in here, you can really, really configure FastAPI.
3:18
When I build an API, I kind of like to call it API instead of AMP. But you know, you you do you whichever you prefer.
3:26
So what we need in order to do this, let's just get the Hello World FastAPI in there real quick.
3:31
So we're going to create a function, it could be async, or not FastAPI will go either way.
3:37
We'll call this hello world, whatever the name is, it doesn't matter. And we'll return a dictionary of messages, greetings to the world. Right.
3:50
And in order to make this an API, if you haven't done this before, you have to say this has
3:55
a get or a post, whatever, and you give it the URL, we could just say slash or slash API. For bonus points, you could spell greetings, right?
4:05
Now if we run this, it's not going to do anything because it's defined the API, but we haven't started the server.
4:14
So we need the section down here where we have, I guess we can have our main, sure why not, define the main function up here.
4:23
We need to say server run. And that brings us back to one more round through here we have uvicorn is a really good
4:32
asynchronous capable server that we're going to use. Might use something like g unicorn with UVicorn workers in production if we were putting
4:40
this out there. But just run through these real quick. Now you can see UVicorn is running so we can say UVicorn, import that dot run.
4:50
And we give it the API. Clean it up, get rid of the squigglies for the PEP 8 violations. Now let's run it. Hey, look, our server is running.
5:01
Let's click on it. Message, greetings, hello to the world. And let's put this in something like Firefox and see if it'll
5:09
treat it like JSON. In fact, it sure does. We have pretty printed JSON. Whenever you're working with direct JSON responses,
5:17
I find that Firefox is better than any of the Chromium-based browsers like Vivaldi or Chrome itself. You can see we're missing a favicon.
5:27
It's fine. We don't need a favicon. We're getting /ok, working, /working, okay, you know, 200. So that is a standard FastAPI app.
5:40
And what we're going to do is we're going to define three or four APIs that have to
5:45
do with our PyPI data, like API/stats, API/packages/recent, and so on. So we're going to take this model and expand it out to talk to our database.
5:57
But if you're brand new to FastAPI, here's what you need to do. If you're not, awesome, I'm sure you already love FastAPI.
6:04
It's quite a popular framework. And here's the skeleton we're going to use to build out that API.