FastAPI Tutorial | FastAPI vs Flask

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Have you ever wondered when you place an order on Amazon.com what happens behind the scene? What you see on the website, Amazon website is basically called the UI code when you click on place order button it sends an http request to a back-end and in the back end there is a back-end server running, a web server running which serves those requests. Now that web server can be returned in a javascript framework called node.js or if you're using python you can use a framework called flask. Nowadays a new web from framework has come up which is called fast API and this video is a beginner video. For fast API in this video we will install fast API and we'll play with it a little bit to see what benefit it has to offer over flask. Let's install fast API on your computer. First I'm assuming you have Python installed which comes with pip so you will run pip install fast api and it is as simple as that. You also need to install something called uvicon so after fast API is installed you will run pip install uvicon. This is the server that you'll be using to run your fast API server so pip install first api and pip install uvicon. These are the two commands that you run to complete your installation. Now I will write my first fla first api server. I have opened pyjam you can use any editor sublime notepad plus plus vs code, any editor that you're comfortable with. and here I'm going to go into zen mode. I will be meditating, all right? I will install the fast API first by doing this and then I will create a fast API instance so you're just creating an instance of this class you can call it app or anything that you feel comfortable with then you will write your first endpoint. So I will explain what that endpoint means. Let's say I'm writing a simple function called hello and that hello endpoint will return me some fixed string. So I'm writing a python function it's an async function okay and I will say return for example welcome so my code is done so you see like only five lines of code and I have written my first endpoint. Now to run the server you will use UV con command you will say uecon and then the name of the file the name of this file, by the way, let me show you is main dot pi. Okay. So that's the file so I will use ubicon main. That's a file name colon app so you are not using main.pi you're just saying main and then colon app whatever is a variable name here and you will do hyphen hi for reload. I will explain what that reload option is but when you run it it says application startup complete and your server is available at this port. This is the host and port, okay? So we'll go to now browser and try to run this link. I'm using chrome here and when I run this link I will say hello see I get this thing back if so hello is essentially my entry point, okay? When you are working on less Amazon placing an order you know you might be having URLs like post order so post order is a typical name of the end point which will post an order you know or place an order so these are all called endpoint it could be post order it could be get, order it can be anything it's up to you it's not like fixed list of strings you can define it to be anything so here I'm using hello and hello is returning me welcome I can change my string and say welcome to fast API tutorial and I have to just save. I don't need to stop my server and rerun it again, see? I did not stop and rerun it and it automatically reloaded my new code and that that was the purpose of this option so now when I go back to my browser and refresh it you see you can see welcome to fast API tutorial. So my core changes I'm doing are dynamically getting reflected into my execution now let's make it more interesting. Let's have it such that it takes a parameter where I can say okay the world and it will say welcome to first API tutorial how do you do that well here you can supply that parameter so here I'm saying name okay and I'm using this bracket this is more like a Python format string kind of syntax where you are supplying name. Here and you can use the same word as a variable name here and now that name that someone is supplying you in a URL is available here in this variable and you can just say this I will use Python's format string and again, you don't need to reload the server it's automatically loading those changes on its own. Now when I refresh it it's all right so let me refresh it so it says see the world you can give any name like say tom and that this string will be passed into name variable and it will come back in the message. Here if we are using get method in http rest protocol there are a couple of other methods as well. So let's go over them one by one get is usually used to create data for example you are looking for iPhone case on amazon website when you make that query the website is making a get request to get you all the iPhone covers there could be another request called post which is used to create data. So when you place an order on Amazon website let's say you are issuing a post query so this get post etc these are the rest API protocol endpoints the third one is put which will be used to update data so an existing order you want to update it and the fourth one delete obviously if you want to delete any data like deleting an order you will use this endpoint. Now there are other endpoints as well but these four are the popular ones. Now while working on this tutorial let's say you got hungry and you went to grubhub to order some food and let's say you're looking for Indian food and this is showing you all the Indian restaurant and it might show you all the Indian kind of recipes. So let's say you are a backend engineer working in Grubhub and you want to write an endpoint where you can say something like get item so let me just show you here you can maybe say let's say get items and Indian and it will return you the indian food items which are available. Okay so I'm going to just change this entry point and I will say get items and this is the name of my cuisine and that comes here as a variable. This function name can be anything you it doesn't have to be get items. And let's say you know I'm retrieving my item records from a database but I don't have a database here so I will just use a simple dictionary where I'm saying okay indian cuisine means these many recipes okay I love samosa by the way American means these these recipes and so on and here you can just use food items dot get cuisine. So if someone is applying Indian cuisine it will return this one so let's let's try this out really quickly. We are very hungry! Okay this is hello hello doesn't work I will say get items indian get items Indian is not working actually it was reloading I did not wait enough. So when I refresh this I'm getting see Indian item samosa and dosa. All right. Let's try some Italian food ah Ravioli pizza. Okay how about Mexican well. We don't have Mexican so then this get will return null but this is not ideal. Actually you want to give user a message that in my website the only supported cuisines are Indian, American, and Italian. Now let's talk about the benefit of fast API over flask. So the first benefit is that in flask if you have to do this kind of data validation then you will do something like if cuisine in rf cuisine not in let's say Indian so you will have to write a code like this where I have just said food items dot keys which will be Indian, American and italian and you will say if cuisine not in this then return this message that only valid supported cuisines are Indian you know American and Italian now see this is a very simple function you might have a big function with a lot of validation and when you are using framework like flask you have to do all this validation yourself which is not ideal. What if the framework itself gives you some validation. So that's what fast fast API can do it for you it can give this validation for free and I will explain how. So I'm just going to remove this code here and I have imported nm in python and NM is used if you have fixed category of things and I'm going to create a class which will have all three cuisines are specified as inum. So see these are my available cuisines and now here you can just use python's type hint and say available cuisines so you're saying that cuisine has to be from these are these available cuisine now this is a type in python is not a statically typed language. So if you run the code just like that it's not going to do anything but fast API understands now that the user or the backend engineer is expecting cuisine to be one of these three and if you don't supply the cuisine which is you know part of this list let's see what happens. So here once again if I supply Indian things are going to just work as fine but when I say Mexican it will give me an error that it is not valid. The valid values are these three so see you don't have to write that validation code here and this is benefit number one that fast API offers inbuilt data validation it is a huge benefit it can make your code really compact because you don't have to do all this validation yourself. It can also reduce bugs so you see you see a very nice error here. I quickly wrote a second function. Let's say on your Grubhub website you have some coupon codes you know and one two three are coupon code IDs and these are the relevant amount in percentages of for those coupon codes and you can have another endpoint called get coupon and code and now code you expect it to be only integer so using type in again you are saying it has to be integer. Okay and let's see if it is not integer so I go back here. I will say get coupon and when I say 1 you know I get 10 percent when I say 2 I get 20 percent but if I do abc it will again tell me that value is not valid integer value is not a valid integer. It has to be integer so again the data validation in fast API is super awesome. The second big benefit of fast API is the enable documentation so you can do slash docs and it will generate the documentation for you. See I didn't have to write anything this could be very useful for front-end engineer who is using your back-end. Bcause that way they will know what you are expecting in your API. For example, in get cuisine you are expecting only Indian, American, Chinese. So if you click on this try it out button see it is showing you that these are the three varied cuisines and you can also send a test request so if I say Indian and if I execute you know see I'm getting very delicious samosa and dosa bag which I can eat and increase my programming productivity you know American execute hot dog, apple pie. So this is giving you a nice test pad to test out your APIs you can also use a different kind of documentation so fast API. Again, if you do slash docs you get this documentation if you do re doc. You know redoc that's another way of generating documentation. So see here it's some generates like response and response samples as well. So you can just explore it. It's pretty useful this is the official website of fast api and if you click on tutorials they have amazing quality tutorials. I have not seen tutorials of such a great quality. Very simple easy to understand on any other technology frameworks. Okay so I highly recommend you go through all these tutorials because I just covered the basics of fast api. I compared it with flask but there are so many things you know so many options available. For example- form data. Form data is a usual thing when you're doing UI coding in your javascript and you can directly import that form class here in the past api back-end and you can do various things with it. You know you get some ready-made functionality so explore these options and you're going to absolutely love the documentation of this portal. While coding we covered the two big benefits of fast API which was inbuilt data validation and the second was inbuilt documentation support. There are few other benefits as well for example the benefit number three is that the fast API as the name suggests is actually very fast the way it is return it gives you the best performance so your server will run almost at the speed of you know node.js server. So the performance runtime performance is beautiful so that's benefit number three. Benefit number four is the code that you're writing is very compact and as a developer, it will take you very less time to write fast API code so it's a compact code. The code development is very fast and there are very few bugs so just to quickly summarize four benefits- inbuilt data validation and build documentation support. The runtime performance is pretty good. It is very fast and the development time is also very less and there are less bugs. I hope you like this video. If you did, please give it a thumbs up and if you have any question post in the comment box below.
Info
Channel: codebasics
Views: 11,845
Rating: undefined out of 5
Keywords: yt:cc=on, fastapi python tutorial, fastapi vs flask, fastapi explained, fastapi example, fastapi example python, fastapi, fastapi tutorial, fastapi introduction, Flask vs FastAPI
Id: Wr1JjhTt1Xg
Channel Id: undefined
Length: 16min 13sec (973 seconds)
Published: Wed Aug 18 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.