FastAPI MySQL REST API in Python | CRUD Operations | Swagger | SQLAlchemy

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello welcome back to another exciting session i'm your host mahesh karya and today we are going to create kind of restful api with the fast api so fast api is a modern framework python framework and it is built on the top of python this is better than flask and other frameworks no need to create documentation in short so we are going to use mysql as a backend so you have to have few things available before proceeding so yeah this is my to-do list so you have to have python available or version 3 or more than 3 and pip which is a package managers to install fast api virtually nv so i'm going to use virtual hp you can use the alternatives as well and this is the pip install fast api sql alchemy we are we are using sql alchemy as orm object for object relational mapping so no need to write queries directly uvicon for server pi mysql is a driver so install xampp you have to example something else which is mandatory for mysql like map if you are on mac so this is all your choice how to use you install mysql from docker or anything else you use workbench whatever it may be it's all up to you this is your choice guys uh install mysql client from vs store i'm going to tell you guys okay so you have to have vs code available after that i'm going to use i'm going to create a new folder on my desktop name it whatever it may be i'm going to name it first appear okay and this codes will be available on github as well you can grab this from link given in the description so yeah after that you can hit i'm going to use shortcuts so control plus backtick for terminal or you can open your terminal from here as well my default terminal is a bash so you can download git bash as well or wsl whatever it may be but i always prefer to use git bash over others and after that you have to have python available so check for python version python hyphen v sorry python hyphen v and not small v capital v and p hyphen capital v yeah you can also check like that python m p i p you can install packages like that so if you don't have virtually nv so you can install virtual nv and uh i already have it so i know don't need to install this again and virtual env space okay so yeah it's created now you can see the new folder it's created over here so you just need to activate this so source venv slash see our scripts slash activate if you are on windows if you are on mac or linux or unix based systems you can use bin instead of script so yeah so it's activated now i can create a new file which will be index dot pi you can create manually or you can hit the touch command or whatever it may be so yeah i have created this file you can create it manually over here as well and after that i'm going to install few packages like pip install passed api r sql alchemy and by and the last one is uh uv con for my server uvicon i guess yeah this is it so now we can write code here so from fast api import past api our app will equal to past api and simply app.gate and this for general testing only i will implement the proper flow of architecture read something okay and here we go you just need to return any json object will be a message is hello world that's it finish i guess and once i start with uicon your file name index colon your function name app that's it and have an iphone reload so no need to reload after each change if you change any file and you just need to open your uh browser and hit localhost colon 8000 you will get this you will get nothing you just need to hit slash talk so you will get this ready bit uh swagger documentation which is the coolest thing of fast api you don't need to create documentation execute this and in written you will get this hello world response the next thing you have to create the structure like config folder should be there will be container db dot something by okay then you have to have models uh in models we are going to create a mysql tables you can say uh we will have schemas or lots of guys will be confused in schemas and differentiating between models and schema so pandemic will use in schemas and models will define the tables the table structure over here so yeah next thing routes to routes between the different pages and the last thing i guess models schemas routes controllers no need to use controllers for now so config db will start from db only so yeah again from sql alchemy import create engine okay now in here engine will be equal to create engine and in here we'll pass this um we can say sql sqlite you can use sqlite as well if you don't want to use the mysql you can simply type sqlite colon triple slash on name of your db whatever it may be it will automatically generate so this is the perfect thing so this is the best way or my sequel plus any driver like by mysql we are going to install this thing slash slash your name of your uh post sorry localhost colon 3306 is the default port for python uh sorry mysql and your test tv test will be available so you need to use my uh exam before proceeding so you can grab this from here apache friends.org and you you'll get the link over here or windows mac this is available for so i already have it no need to download this thing so from here i'll get exam control panel and over there we can start apache and mysql so yeah it's just started so we can continue with this so before that i need to install pip install by mysql okay that's it finish uh now we can create connection i guess okay this is it okay so connection will be like this dot connect okay that's it finish i guess no extra work should be here and inside models you can directly create index dot pi or user dot pi uh there should be index dot pi as well so i should create index dot pi as well here will contain so from sql alchemy import or we can say meta metadata and meta will equal to metadata so yeah this is it and here we will import like models dot user and import users users will define over there so yeah this is it i guess and inside here will define from sql coming import your you need a table you need a column okay you need a meta as well so we'll import meta from somewhere else so we should define this meta property oh wow i'm not sure where should we define this but it does it should be here only oh yeah we'll see it later if something goes wrong so fun for now it's it's simply like it should be like this so uh yeah we can proceed from config dot db import your meta we can say so yeah users will equal to our table and table will contain a name of table like users and comma your mate object okay and comma column column will be containing integer integer i will be type of something but for now dj will be better and name i guess it should be like this id comma integer and comma primary key equal to true make sure t should be capital and after that comma separated other column names so you can use anything you want name email password kind of basic stuff i'll be using here so string this will be type of string you can define your length like 255 i generally use also you can define according to your choice like for gender we generally use six length we use oppo zip code video 6 but it's all up to you how you use it so name then i need email and the last one should be password so yeah this is it uh we can grab this from anywhere else so this is it i guess now now now i'm going to use index.js to import users for now i'm not sure uh huh in schemas also we'll create a user dot js so from pendant i don't think import all we need to define a base model i guess from here so class uh user you should be capital and this model it will be narrated from race model and after that you just need to define id will be type of end okay um then the next thing will be the name will be type of string email will be type of string and the password will be type of string fun for now it's fine i guess i'm not sure why we are getting this thing identic we need to install pidentic now why we are getting this i'm not sure but it's fine so in routes as well uh we need to create user dot pi and after that in here we will import from [Music] we are using fast api import uh your api router so apr router will create a new api router with name user so api router and which will be imported from index imported in index so it will be similar user dot get whatever it may be flash for now and a sync you can use async dev your name like read data okay so no need to use params here and in return will return users dot select users dot select and will be wrap you can wrap this in connection dot execute okay and we'll be importing uh connection from db config.tb so config.tb import co double and connection so we can use it over here so and users will be having users uh from uh models will get users so modern start index also you can grab users like this so users will this is it i guess this is it nothing else should be needed over here and in schema only we are getting error input base model import can only be used in typescript oh my god this is a js file i'm using it should be pi okay this that's it that's it and now we can proceed so okay it should be one two three four and five so forget all elements get with id so you can use id over here then you can grab id define it should be inside int and here we will use where in where you just need to use users dot column column is c dot id will equal to id that's it in here we'll use post method so in post method we'll define our write data write data in execute we'll simply use insert over here and in here we'll use values so in values we'll define like you can define like user.name will equal to no you can define like this name equal to user dot name email equal to user dot email and password will equal to user dot password so there is there is need to crypto use cryptography or something else to uh encrypt your password and decrypt while logging signing in or it's all up to you how you use it it's not um it should not be like this so and one more thing it should not use fetch all because it will use written statement same like select after inserting data so we'll get the new records new entries over here once you and one more thing you need to pass user here and you need to define user data type and we'll get this data type of user from the schemas okay so key mass dot index you can define as well here as well so it will be good practice to use schema so in schemas um here i just need to define index dot pi index dot pi and now from pms dot user import capital user so yeah this is it we will grab this from here it's all up to you how you use it but i always prefer to use like this so yeah so uh now we are getting this user here and now we can update the data we use for update put will pass the same thing ideal here as well and in here in past two things id will be type of integer and at the same time user will be type of user okay and which will be and in here everyone's if you want to import the data you just need to use connection dot execute and in here users dot users dot update simply and in update you need to define the values whatever the values you i don't need to define values i guess let's see i don't think value should be like this it should be inside update only but even though i'm using i'm not sure for now so yeah and we will i don't think so it's needed so dot where where condition will be similar to this only like this you can grab this from here as well and simply define update data will be no better and in here you just need to check the id should match with this id so yeah this is it and the last one is delete so you can delete the data name it delete and uh yes we'll use a con dot execute and define users dot delete okay and delete where condition will be same i guess so yeah this is it and we can check it we need to import this thing in index.phi so don't need to use this thing no need to use this thing just use no we need app and app dot include router will be used in user here we'll get it from from [Music] routes dot index like this import user so from route as well uh why i'm creating index because when you work on multiple multi-structure project you just need to define index will be having all it's all up to you by the way there is no need also but it makes sense so from a routes dot user import user so we can grab this from here so yeah from we are getting user from here and that's it for now control plus backtick and to restart you'll get few errors as well uh but no we are not getting any errors good and if i reload this thing with the docs then i'll get some error no very good if i try it out execute something will get error no okay i'm not getting error this is my previous date i have done this same table i guess previous previous time now if i hit write data in a oh yeah this is the error okay so in your schemas make sure to not use id because id we have defined as auto incremented so yeah this is it and if i reload this thing same thing and we'll check this so yeah this is the body that i should pass so if i define something the name like john what is [Music] uh and password will be like at one two three okay and if i hit accept you will get written response this is my current response id is eight okay you'll be getting one i guess after that if i want to get a specific data i'll hit uh the id over here and execute the query i'll get a single intro here okay and if i go here input so i just need to put the data try it out id will be eight and for that i'll i'll use the same format like this with preseted with something oh yeah input body i'm passing this thing preceded by added symbol so we can identify the record is updated so make sure to password should be uh you should you have to update password so in update we are getting error let's see uh in john do extra data okay it's fine so let's see what we are missing in routes user and control plus backtick to close this up and input method here we have passed id then we have passed user in update i guess we are doing something wrong okay i guess values will be there well this i'm not sure i'm not sure i told you let's see again oh my god this is the issue not this one so yeah it should not be like that you must have seen this before so yeah if i hit the backtick again and submit the button then i'll get internal server error okay to type error additional should be named got name so yeah so let's see it works with values or not values and let's see click this again and if i execute this yeah this is updated so values was there i was sure about that and the last part of this process is to delete something so i'm going to minimize everything to to delete something try this out execute without without id how it will execute right so i need to define id here in your delete so make sure to define id which will be type of end by default it is string that's why i have to define the id over here and i got error here no okay we will execute this again we'll refresh the page and we'll get this id block here so we can grab the id 8 so now the data is gone so you can see this only 697 is here so yeah this is it so thank you very much all of you guys for being with me supporting me all the time it's been pleasure to be here if you like my video please make sure to subscribe hit the like button and uh tell me something about what you want from my side i'll be creating more such videos and thank you very much bye
Info
Channel: Mahesh Kariya
Views: 64,802
Rating: undefined out of 5
Keywords: fastapi, mysql, swagger, rest api, api, crud api, fast mysql, python mysql, python rest api, python fastapi
Id: 4Zy90rd0bkU
Channel Id: undefined
Length: 28min 28sec (1708 seconds)
Published: Wed May 12 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.