Why You NEED To Learn FastAPI | Hands On Project

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up everybody it's Travis here from Travis dot media when you build modern applications you almost all the time want to use an API in this video I'm going to talk about why and I'm going to walk you through a simple project with one of my favorite API Frameworks let's get started and actually before we get started earlier this week I launched my coding Community it's a community for junior developers and aspiring developers to come together and reach their goals in a community not in isolation so it's a place for learning it's a place for growth encouragement and accountability currently we have about 26 members and it's been a great couple of days if that's something that would interest you check out the video I'll put a link above where I do a walk through and then come join us we'd love to have you there so think about a.net app or a laravel app or something like that you have models which talk to your database you have controllers which control the logic and then you have these views and you build your web app and it gets bigger and your views get bigger and your logic gets bigger and it's all maintained in this one growing large code base and if you decide later hey we want a mobile app version of this then you have to go and build that separately then if you want a desktop app you're going to build that separately if you want like an iot device you'll build that separately as well now you got four code bases to maintain that's not good in a lot of this duplication can be remedied by creating an API first so with the API you have models that will talk to the database it will relate with your data you have controllers that's going to handle the logic and then you have routes in this entire backend sits somewhere and you have routes to interact with it and once that's in place you can create your web application with react or something like that all you have to do is make calls to these routes so I want to log in that's a login route log out route register routes create post route update delete post routes so once that API is in place you can build out your web application it's just a front end you just take react and you make calls to your API and then if you want to mobile app up you don't have to build this entirely separate thing you just take react native or flutter or something like that and make calls to that same API you want a desktop app same thing make calls to the API stripe integration API where I'm going with this is now everything is an API if you put that in place you can connect anything to it I've heard people compare it to like a USB port you can put a pin drive there you can charge your phone there all things speak that language and can interact back and forth with your backend and your database so given this fact you need to make sure you're familiar with apis what is an API how to build an API things like routes and models and Basics like that and that's regardless if you're back end or front-end if you're front end you still got to talk to the API you got to know something about it so today we're going to take a look at one of my favorite apis it's a python API called Fast API and it has so many neat and modern features that make it stand out is a way better alternative than using something like flask as an API and I'm going to give you seven reasons why fast API is a better alternative and one that you should learn in 2023 2024 whatever whenever you're watching this and I'm not going to go and give you seven bullet points real quick why fast API is better I'm actually going to build a simple to do app so that I can show off these seven key differences or seven key advantages so I don't want you sitting here watching me pull up your computer pull up the S code encode along with me it's going to be fun I guarantee you'll get something out of it so let's get started first let's create the python project so I'm going to CD into my desktop and I'm going to make a directory called Fast API project CD into that and I'm going to create my virtual environment so python m v e and V this is just a virtual environment it's a way to get all of the things you need in a kind of sealed up packaged environment and I'll open that in vs code and then I want to activate this environment so Source venv slash bin slash activate to activate it and we're ready to go and the first thing we want to do is to install the package so let's go to tutorial Dash user guide and see if it tells us so right here install fast API so let's do this pip install fast API install that and while that's installing let's grab the next package which is uvicorn which is a server that's going to run our API they're installing just the standard dependencies so let's highlight that and run it awesome and let's get some kind of sample here to get our API running so to do that let's go over to First Steps and copy this code and we'll talk about it so I'll create a file called main.pi and paste this in and here's what's going on we're importing fast API we're instantiating a fast API class and saving that into the app variable and right below it you'll notice this is just plain Python and that's fast API Advantage number one they've made it Easy by keeping the syntax just plain python you can see here if you know python you can make sense of it so here we have a path decorator that says hey whatever method is below it is in charge of handling any requests going to slash which is going to be the root of our program and the method is called root and it returns some Json of message hello world and that leads us to fast API Advantage number two which is async is built in with fast API we use asgi which handles requests asynchronously whereas flask uses wsgi which handles requests synchronously so that's one big benefit it's async out the box so async function there it is so we have a route and we have a route that does something or produces some kind of data so let's save that and figure out how to run the server so that's right below it you just copy this command it explains right here what it does if you don't know but I'm going to open this and paste in this command and what it's doing is running a server for us this first argument here is going to be the name of the file which is main so we don't have to change that and then the instantiation of the class which is saved in a variable called app so we don't have to change that either so if you save this in like api.pi it would be API colon app or if you change this variable to something else you would replace the app but we seem to be good to go let's hit return to run that server and that's served here on Port 8000 localhost so we can open Postman or whatever you want to use do a get request to localhost 8000 and you should see Hello World basic enough so what we're going to do here to demonstrate fast apis just build a simple to-do app so let's go ahead and create some comments for all of this so here we're going to get all to do's next we're going to get single to do then create a to-do update to do and delete a to do and we're going to keep this video kind of short so I'm not going to set up a database we're just going to do to Do's equals list an empty list and we'll just push everything there and not persist it to a database for the sake of not having a really long video and if you do want to build out this app further there is a second and a third part to this the second part is setting up a SQL database so that you can persist the fast API data to SQL and then the third video will be building out a react app just a simple react to do app to talk to the API and pull data and post data and update data those two videos will be in the Travis media community so consider joining us there will be links below to those so let's go ahead and fill in these comments so get all to Do's let's just copy this here put it there and figure out how to make this work so let's change this path to to do's and we'll change the method to get to do's and then we want to return all of the to Do's so that's pretty easy given that we have a list we'll just put to do's and we'll return the actual list of to-do's so let's save that and try it out so let's do slash to Do's send and we have our empty list great next let's skip down to create a to-do let's figure out how to do that so let's copy this and paste it we'll keep the path the same but we'll change the git to post and we'll want to pass in a to-do a to-do item now the thing about apis is you don't want sporadic and random data being sent to it you want to have data validation so if I have a to-do I want to Define what parameters that to do accepts and then not accept anything but that so that our data is clean and we have a defined object and that leads us to fast API Advantage number three in that data validation is built in with pedantic so if we go back to the documentation and we go to request body which is where we should be going for anything post you'll see here that to declare a request body you use pedantic models with all their power and benefits so you'll see here that we are creating an item and that item is defined here with these values so it has a name a description a price and a tax so when you create an item if you're going to this route and you're creating an item putting an item in the database you got to follow these rules or it won't work and that's what you want so let's copy this and let's create a file called models models.pi paste that there and instead we're going to do a to do item a to-do class and we're just going to have two values we're going to have ID which is going to be an INT and then we're going to have an item a to-do item which is going to be a string and that's it we're going to keep it simple so every to do you create or get or whatever it's going to follow this data model and fast API has validation built in to confirm that so let's jump back over here and up at the top let's from Models import to do but down here let's change this to create to Do's we can keep the path the same and what we'll do is we'll pass in the to-do item of type to do we brought that in we brought in that model now we're saying this to do that we're posting here has to follow that data model and that leads us to fast API Advantage number four and it's that fast API is typed if this to do was an integer I can say int if it's supposed to be a string I can put string I can declare types in Python with fast API I love it so this is going to be a type of to do so what are we going to do well simple we're going to do to Do's dot append so we're going to add it to this list that's how we're going to create remember we're not persisting to do is dot append to do and then if that's successful we'll just say return message and the message will be to do has been added let's try that out so let's go back here let's get to Do's we don't have any let's switch this to post put some information in the body which I already have so ID is one and item is to edit a blog post let's send this to do has been added great now let's get and make sure our to-do is there and there it is post finished so we've appended that to our list now let's jump up to the single to do so I'm going to copy this paste it because I don't like typing so much and here we need to pass in an ID for the to do item we want to get so if we come over here and go to path parameters and you'll see here we can pass in a dynamic item id this can be anything and then that will because it's in this decorator correlate to this item ID that we're passing into the function and if you scroll down a little further you'll see that we can also type that so let's go back and we're going to add here to do ID so we can add that to the to the path then down here we're going to pass in to do ID and we're going to make sure it's an INT if I didn't do that python would guess that it was a string and it would mess me up because when I want to compare the string to the actual ID it's not going to match so we need to declare that an integer so type your parameters so here I'm going to make it easy and just do a loop four to do and to do's if and here We're looping through this and here to do is our individual item so if to do dot ID is equal to this to do ID that we're passing in if that's the case we want to retrieve it return to do and the actual to do item and then down here if nothing is found and fast API has some good built-in error codes but we're just going to do message and here we'll say no to Do's found so let's try that out open up Postman and let's do a get should be nothing there because when we refresh the server we lose that temp data so let's go ahead and post this again so post it then we can get and see if it's there yes now let's try to get individually to do with an ID of one so hit send and there it is and you're probably like what is the point of this we're just saving it to a list and you're right but what you can do is you can get it all functional and then you can go in and swap it out for your database you can set up your database and then where it's like to do dot a PIN to append it to the list there you just do database.write or whatever it is you just write it to the database so you can do this and then add the database functionality next so we have all to Do's single to do and we can create a to do let's do delete next because delete's always the easiest so I'm going to grab this and paste it here and I'll call this delete and actually before we do that we didn't change the name of this function instead of get to Do's let's do get to do and then I'm just going to copy this and we'll work on the delete which should be easy so this needs to be changed to delete to do's and then we'll pass in the ID for the delete as well and here we'll change this to delete to do keep the ID and then we'll Loop through just like we did here and if the ID matches we'll do a to Do's dot remove to do so we're removing that from the list and then we can return a message that says to do item or to do has been deleted so let's save that and try it out again let's check we should have nothing now let's post this item and we can get to make sure it persisted yes and now let's delete it so we're going to delete with the to do with an ID of one send the to do has been deleted let's make sure our list is empty and it is so delete's working too now let's do update let's finish with update update's always a little bit more complicated but here's the thing fast API is just python so let's try to do this without even looking at the documentation so let's grab this again and there's a number of ways you can update data you can do a put where you replace the whole object we only have two Fields so I'm just going to replace the two Fields you can also do a patch where it's going to update selectively but here we'll just do app.put and here we are going to update based on the ID and let's change this to update to do and here's the thing we are passing in the ID that's going to help us identify the to-do that we're updating but we also need to pass in the to do data so let's put a comma here and passing a to-do of type to do and here again we'll just use a loop it's other ways you could do this we'll Loop through them and if the IDS match then we'll update the two Fields so to do dot ID equals and this is not comparing this is changing the values so one equal sign to do dot ID equals to do ID and the to do dot item which is the other value in our data model needs to equal the to do and we're probably going to have a little contradiction here because we have to do here and to do here so let's change this to to do object and here we can say to do item equals to do object dot item so this to do We're looping through this is our individual to Do's the item value is going to be replaced by this to do object item value which we're passing in which is going to be the new object hope that makes sense and then we'll return the new to do so return to do and to do and then here we can put note to Do's found to update let's try this all out so let's get it we have nothing let's post this and then let's do a get again we do have that item now let's change this to instead of edit a blog post let's do delete a blog post let's see if that works so here we're going to do a put then we're going to pass in the ID and we're going to send this data and it's going to overwrite our ID and overwrite our item now with the database you don't really have to overwrite the ID you don't have to mess with the ID because the database is going to increment that in whatever way you set it up to but anyway this should change the item to delete a blog post so let's send it and it returned the new to do now let's do a git and make sure that it updated send and there it is delete a blog post now I got ahead of myself there are actually two more Fast Track advantages one is that errors are in Json whereas with flask they would show up in the HTML and then Fast Track Advantage number seven is that there is Authentication built in so check this out it supports HTTP basic auth oauth tokens or jot tokens in header API keys this is by default and actually I can't even count the last one was supposed to be six so fast track API Advantage number seven is that if you open this in a browser so I'm at localhost 8000. if you do a slash docs you get a Swagger UI so if you have stakeholders that want to see how this works or you have front-end developers that need access to the data or for any other reasons you need a user interface to represent your API you get Swagger built in so if I go to post I can click try it out I can set an ID of one and an item of mow the lawn execute and I can see I have a 200 response my response body is to do has been added then I can run up here to this get route try it out execute and see that I have my to do so I can demo my API to someone use it to develop or whatever I need I have Swagger built in with fast API then of course I have my get put in delete routes as well if you implement authentication there will be a button up here where you can authenticate where you have to authenticate in order to use these routes and so forth and if your flavor is redock which is another graphical API tool just type in redoc instead of Docs and you get that built in as well wonderful now that's a very simple API if you go back to the documentation I pretty much just explained a lot of this user guide there are other things like cookie parameters header parameters nested models there's more things you could do form data and you can go through these and see examples as your API grows but there's also a section called Advanced user guide so you can click on this and you can see there's just so many things this API can do it's very modern it's very fast it's python it's typed it's async there's authentication built in there's great documentation here's your relational databases your nosql databases it's all here so apis are super duper important and hopefully you've seen here how fun it could be to build one so I hope you enjoyed it and I hope you continue to build it think about some project you can start and go through this documentation and learn apis that's all I want you to do if you found this video helpful give it a thumbs up consider subscribing to the channel and I'll see you in the next video
Info
Channel: Travis Media
Views: 139,899
Rating: undefined out of 5
Keywords: fastapi, fastapi tutorial, python api, python api tutorial, python api project, python api fastapi, python async, typed python, python asgi, python fastapi, create an api with python, python web app, fastapi python, python web development, fastapi tutorial for beginners, fast api tutorial
Id: cbASjoZZGIw
Channel Id: undefined
Length: 21min 15sec (1275 seconds)
Published: Sun Aug 06 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.