FastAPI [ Python Web Framework ] Crash Course 2021 For Beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to the fast api crash course for beginners i wanted to do this one for a long time but i didn't have the chance to do it earlier but it's better later than never i decided to follow a different approach in this course which is the practical approach meaning that instead of me showing you a few slides talk a little bit and then show you some features by typing some code i decided instead to build a crud router application and i'm sure you will learn a lot because we'll create different routes using different http request methods to create read update and delete to-do's so we will type some code together step-by-step and we'll see what the code builds actually but still i will show you a few slides then we'll jump directly to vs code or any text editor of your choice and by the way i have a video on fast api but it's an 8 minutes video just scratching the surface i will leave the link to that video in the description section below alright so what is fast api fast api is a modern and fast web framework for building apis and it works from python 3.6 and above and it's based on standard python type hints also fast api supports concurrency and core routines but i'm not going to talk about concurrency and parallelism in fast api in this video if you'd like to see what is concurrency and how it's working in fast api please let me know in the comment section below also fast api is one of the fastest python frameworks available and with speed comes very high performance fast api is on par with node.js and go which means that fast api is on the same level as node.js and go language so fast api by experience is faster than express js and sjs express and nest are just back and frameworks for node.js and node.js is just a run time for javascript to work on the backend so node.js is just javascript code that we write on the backend or on the server side okay and it's based on v8 engine the same engine on which runs google chrome so let me show you very quickly this is the documentation for go language okay so go is an open source programming language it's a back-end language of course and it's easy to build simple reliable and efficient software so this is go express is just a web framework for node.js so to compare fast api it should be compared with another framework in this case express and i told you by experience fast api is faster than express i really don't know about nest i heard some colleagues of mine saying that fast api is faster than sjs as well but if this is the case it wouldn't be surprise for me fast api also is designed to be easy to use and learn which means less time reading documentation and more time to produce and also it minimizes the code duplication which results in fewer bugs it's also robust framework with automatic interactive documentation and we'll talk about the interactive documentation in the next slide so let's check out some of the features of fast api first it has open api which makes the creation of apis very easy including declarations of path operations parameters body requests etc also it has automatic data model documentation with json schema as open api itself is based on json schema also fast api can help you test your requests and see the responses using the interactive api documentation and i will show you that in the browser when we will create our crowd router application and the last feature on the list is no new syntax to learn just standard modern python so if you feel comfortable writing python code then you will feel very comfortable writing your web applications in fast api so let me talk a little bit about concurrency ascii uv corn what's all that fast api uses ascii which stands for synchronous server gateway interface if you have seen my videos in flask and django we said that django starting from django 3.0 uses the ascii server so ascii is a server or to be more specific ascii or whiskey there are just the interface between the server and your web application so it's kind of the connection between both actually so django 3.0 uses ascii server flask uses whiskey server and fast api uses ascii server and we need to install that server or that implementation for the server and this is where uv corn comes to play so we install uv corn via pip install uv core and what it is is just the server implementation and it's not only limited to uv corn there are others like daphne or hypercorn but the most popular and the most compatible with fast api is uv core by far it's the most famous one and ascii helps an ecosystem of python web frameworks that are highly competitive against node and go so you will need to understand that fast api as a web framework uses ascii server which is in our case uv core and uv corn which is our server uses something called starlet and start is just a lightweight ascii framework or tool kit if you remember in flask crash course we said that in flask we have a whiskey server and this whiskey server uses a toolkit called work zwig so starlit is very similar to workswig it's just a tool that helps the server building high performance services and in our fast api case it's a sync io services okay so hope all that makes sense again just to recap very quickly fast api which is our framework uses the ascii server and we can choose from uv corn daphne or hypercorn but we very often use uv corn and uv corn itself it needs a toolkit or a framework and this framework is starlet so starlit helps uv corn building high performance sync io services all right so this is in a nutshell the whole story so let me talk a little bit about the fast api creator sebastian ramirez he's the creator of fast api and typer and typer was intended to be the fast api of clies or command line interfaces and i will show you that in a second on the github page he's also a developer and explosion company he works a lot with artificial intelligence deep learning machine learning full stack distributed systems he works with sql nosql and a whole bunch of other texts all right so this is sebastian and let me show you his um github page real quick so you can see here's his commits he's the creator of fast api and typer he's a great guy and thank you sebastian for creating fast api it's an awesome web framework really i like now to work with the farm stack which is fast api react and mongodb by the way and let me show you the fast api page so in the video that i've created two months ago we had 22 000 stars so two months later it's 26 600 stars all right so it's growing real fast and it has 208 contributors so i trust that anything that you need to know in fast api you will find it either on the github page or on the official website fastapi.thiangolo.com and documentation is amazing by the way it's perfect this is the concurrency and async away code i love this page i've read it all it's beautiful if you're interested in concurrency and async await code i advise you to read this page okay so let's get back to fast api page so you can see here some opinions of different companies for example netflix okay netflix is pleased to announce the open source release of our crisis management orchestration framework dispatch built with fast api so it's really growing in reputation and i suspect that fast api will have great future in the world of python web frameworks alright so before we get started let's go ahead and install fast api uv corn and starlit on three different planes like that or just all at once on one line pip install fast api uv corn and start it together because we're going to need them in our crud application project so yeah let's go ahead and build our quad router alright so let's go ahead and create a folder so i will create it on the desktop so go to desktop make dur and we'll call the folder fast api crud crash okay so let's access that fast api crud crash and let's open it with visual studio code alright so let me actually show you the structure of the project so this is the main structure of our project so inside your main folder so i'm going to create a folder now called fast api inside that folder i will have main.pi file and a folder called app inside app i will have two files init.pi and api.pi or actually let's call it app.pi alright so make sure you have that structure in order to start working with me okay so we don't need that anymore the first thing that i want to do is i want to go to the main.py file so in the main.pi file we want to define an entry point for running our app which will be running directly in the fast api site and we're not going to create a front-end site so no react or view or angular anything of that just will create the back-end today and we'll display it through the fast api interactive documentation so the first thing that you will need to do is you will need to import the server and do you remember what's our server called exactly uv corn so we'll need to import uv corn then what i want to do below here i will say if name equal to main in this case what i want to do i want the server to run and here i want the app dot so i want to access app folder and then i want to access the app.pi file okay so app.app and to be run as the main app okay next what i want is the host so we will say zero zero zero zero okay and by the way you can wipe it off totally and it will default to the local host 127.001 all right so it doesn't make a difference also our port is eight thousand the default port for fast api is eight thousand and i want to set reload to true which is very analogistic to debug mode in flask you set the debug mode in flask to true in the development okay you set it to false in production when you will deploy your app all right so i don't know why i wrote it like that so here what we did actually is we have told main.pi to run the ask uv current server on port 8000 and to set reload to true okay and you want to make sure that reload is set to false if you will deploy your application never forget that and what i like to do also is i like to determine this to 127.0.0.1 or as i told you you can wipe it off totally so it's up to you let's actually delete it just to have your code all right and now i want to create a base route in app.pi so let's go to app.pi and i want to import from fast api fast api class and then i will create an app object and it's an instance of the main class it inherits everything from the main class and then i want to create a router decorator it's very similar to flask and also in django we had the path route so you can notice that these frameworks have the same logic but the syntax is different so we'll say at app dot get and we want to get just an empty route or a slash and here we'll create tags and this will be very obvious to you when we'll open the docs or the documentation or the interactive documentation in fast api and this is just the category under which this get request will be classified it's nothing more than that and then let's create our function so i will call it root and this points to addict or a dictionary and it will return just we'll say ping pong like that and let's actually add a sync here a sync code if you don't want to include it don't include it but i think it's a good practice at least to get used to type the word async or the sync syntax so when you create a fast api path operation you can return any data from it in our case we have a dict or dictionary but you can return anything a list a database model anything that you want and by default fast api would automatically convert that return value to json so behind the scenes it would put that json compatible data in our case the dictionary inside of a json response and this would be used to send the response to the client so we don't have to import any external package fast api will take care of that alright so let's go ahead and try that out actually our minimal app so let's enter the main directory fast api and python main.pi so we have here the server running on the localhost and listening on port 8000 let's click on that open your default browser and we'll get ping pong okay so if you'll go to the url and type docs slash docs and hit enter you will have here this is open api okay this is the main interactive documentation provided by fast api to make your life easier so if you'll click on get you'll have here everything that you will need to do interactively speaking so try it out then execute and you'll find the ping pong this is the response that we get to our get request okay and if you will take that curl link you copy it and if you're working on mac os or linux you have curl pre-installed in your system and if you don't have that if you have windows i mean um i think it's a good idea to install get bash so in git bash you will have curl installed so let's paste that curl link and hit enter and you will get the response ping pong all right but even if you don't have that fast api really makes it easy for you to see what's going on and if you'll have an error you will find here the type of error that um that was reported okay so this is simply it and i think it's pretty cool from fast api to do something like that okay let's close the browser and let's get back to our code let's shut the server all right so we said that our crud application will be divided into four sections create read update and delete so these are the main four functions in all the websites that you can create something on them so you can create a blog post create a to-do app like we're going to do now anything basically that requires that you create something and you can update it you can delete it and you can get it so let's type here a comment minimal app get request okay so this has a root tag we are going now to create four routes so we want get post put and delete and this get request pertains to read to do post is create to do put will be to update a to do and delete is just delete to do okay simply so let's start by get so get is very easy it's very similar to the main get that we have created here so let's have a route right and when you will type slash to do you should be able to get the list of the to do's and we will put that in a tag and we'll call it just to do's okay then we want to create our function and i will call it get to do all right and also it will return a dictionary i don't know why i did that okay and it will return the data and the data here could be from your database so if you're working with a nosql database like for example or an sql dbms like for example mysql or postgresql sql alchemy any dbms you can use it easily here but just for the sake of simplicity in this crash course i'm going to create a dictionary of different to-do's so i will call it todos and i will put it just down so to this is a list inside that list let's paste two of these to do's okay so these are dummy data just for this demonstration all right so why it's red oh sorry for that we want to return this dictionary which will be parsed automatically to json data okay so the list of to-do's that will be displayed is that list here okay so pretty simple and pretty logical so let's actually go ahead and try that out so if you will if you're using visual studio code and if you will hover over to do's and you hit control you'll find that this is the to-do that we have created below but again this could be substituted with your database all right so let's open our integrated terminal let's enter fast api and run our main.pi file so now if we'll do like that slash to do we will have our data so id1 with activity id2 with activity but if we'll go now to the docs we will have a different you see this is the tags that we have where is it that we have created you see root and to dose okay so in the get route for get to do's if you will try it out execute you will get both activities or both to-do's okay and again if you will take that curl link and we'll try it on git bash and we'll paste it we will get our response with the status code 200 which means that everything is ok no internal error on the server and it responded perfectly okay all right so far so good so we have created our first route or our first letter in the crowd which is read now let's go ahead and create our post route just close that okay so post route is pretty simple again we will need a route at app.post in this case and it's directed towards the same route to do on the same tag category which is to-do's and we'll create our asynchronous function and we'll call it add to do and the to do here is going to be a dictionary okay so this is the argument or the input that we want to do in the form of dictionary and um it's pointing to dict and then we want to append in the to-do's list down here whatever to do that you will enter so we have a parameter to do to dict and in the argument that you will enter it should be in the form of a dictionary so the to-do's list will append to that whatever to do that you will enter and what we want to return is just a dictionary and we'll say here data so this is what happens after you enter your to-do you get a message in the form of dictionary or on the browser will be in the form of json telling you that a to-do has been added successfully something like that to do has been added so i had to do all right so this is post so let's go ahead and try that out all right let's go to docs okay perfect so we have our second route and notice it has a different color get with blue and post was green so let's go ahead and try to post um had to do so try it out so let me actually cheat and take one from here all right copy that okay and i'll say that this is activity number three say play football with the dudes all right oops so we'll execute and we have response 200 everything went okay data to do has been added all right and if we will get up and get the list of to-do's again you will find that our third activity has been added to the to-do's list all right so perfect um let's actually close that close that well i don't know why i did that because i will close the whole browser alright so so far we have created the get request or the read to do's and create to do's with the post request now the third request that we have is put or update to do and this is pretty simple what we'll need to do is we'll create as always a route with the put request to do like we have done here and here but we will add this time the individual id number that we want because logically if you want to update you will not update everything at once but you will choose only one to do activity to update it and to do that we will need an id number and it will go under the same tag name to do's all right and let's create our function we'll call it update to do and we'll have two parameters an id and buddy so the id it should be an integer all right and the body must be a dictionary okay so these are our parameters and id and body and this points to addict as always and we want to loop over the to-do's list so for each to-do item in the to-do's list i want to check out if that id is equal to the id that you have entered then the item or the activity must be equal to the activity in the body where you're going to update your activities in order to do that let's go ahead and have our for loop for to do in to do's and here we're going to have a condition so we want to check if the to do with that id number is equal to whatever id is going to be entered all right but something is missing we need to parse that into an integer to have the same data type in this case i want to say to do with a not item activity should be equal to the activity in the body all right by the way i wanted to ask you guys what is the text editor that you are using right now please drop a comment in the section below and let me know so if this is the case we want to return [Music] a json data saying that to do with id has been updated and we can do that in a better way by using the f string and we can add whatever the id number that has been entered so or has been updated should i say and with id number and here just we will plug the id number all right otherwise i won't say that this id number was not found so um also data also let's use f string to do with this id number whatever id is was not found all right perfect so let's go ahead actually and try that out so you see how fast api is really fast in implementing and showing and making changes this is what makes fast api stands out so let's go to to do not to do but to docs and take a look it's really cool get with the blue color pose with a green color and put with orange color it's neat it's clean i like it really so we have our get request our post request and our put request now let's go ahead and try put so let's check out first our um yes of of course it's there's no persistent data because we don't have database so the third activity that we have created was wiped off so actually let me try post again so so i'll try that again three and let's type something like the same playing football with the dudes all right and let's copy it just in case and let's execute and we have response body successful response which is a to do has been added okay perfect now i want to modify that first let's check it in in our to-do's so execute another get and there it is all right so now let's try to modify it so the id let's enter a number it should be an integer which is number three and we'll paste that and instead of playing football with the dudes i want to say playing super mario and let's make execute and we have here data to do with id3 has been updated perfect now let's go ahead and create our last route our delete route and this is very easy and very similar to our put route so let's go ahead and create another the creator so app dot delete and the route will be to do and it goes to an individual id and also it's classified under the to-do's tag and let's create our function a sync def and we will call it delete to do the only input that i want is the id number because logically you want to delete a to do with an id number so id must be an integer otherwise you will have an error and will also loop over the to-do's list so for to do introduce we want to check if the to do with that id is equal to is the same like in post so id and also we want to wrap that in int method in that case i want to remove that specific to do item so to do is dot remove method and we will pass inside that to do that to do with that specific id all right and we will return also a message saying data has been deleted or to do with id number and whatever the number of id is has been deleted else if there is an error we can return something like all right this to do with this id number uh wasn't found all right and that's it this is our crud router application of course we can use a front-end framework like react or angular i personally love to work with react more as this channel provides only python and python frameworks databases related to python and so on i've never presented any front-end framework or javascript i only have two projects in javascript not more but if you would like me to recreate that application using react please let me know in the comment section below all right but for the moment let's go ahead and run the server to test our last http method delete so fast api okay our default page ping pong and let's go to docs and look at that how clean and nice and neat is that get post put delete with different colors under a tag name and if we have different methods under different category it will be under a different tag name all right so let's now test them all let's first get everything so this is the read in the crud execute we have our two activities that we have written in our script now let's go ahead and create a third one so try it out and here let's actually cheat a little bit and go to execute and here click that and we'll go here in the request body and we'll paste that 3 activity let's change it let's say get some sleep okay sleep is very important as well and if we'll execute it and if we'll go up to get to dues we'll find that get some sleep has been added to my to-do list now let's try to put or update so let's update the third one so you need to click on try try it out and let's update the third one and here so here should be three as well the same id in the request body should be the same id that we have entered and let's actually instead of get some sleep let's say play i don't know mortal kombat execute and we have to do with id3 has been updated and if we'll go to our uh to-do lists if we'll execute to get the to-do's we'll find that play mortal kombat okay id number three has been updated all right let's try the last route or the last http request delete let's click on delete and try it out and let's delete the third to do execute and that's it to do with id3 has been deleted so if we will go to the get to do's and i'll click on execute you'll find that it has been removed from the to-do's list all right so this was the fast api crash course i hope you liked it if you have any questions please let me know in the comment section below i love to answer all your questions also if you have any suggestions any types of videos that you would like to see in the channel also please let me know all right so thank you very much guys for watching and i will see you in the next courses take it easy
Info
Channel: Bek Brace
Views: 9,840
Rating: 4.9534883 out of 5
Keywords:
Id: 62pP9pfzNRs
Channel Id: undefined
Length: 39min 32sec (2372 seconds)
Published: Sun Jan 31 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.