Fast API Tutorial, Part 1: Introduction

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello friends and welcome to the first in what will likely be a fairly long tutorial series on uh fast api in this video we're just going to do a very basic introduction of how to get started using fast api creating a project from scratch setting up some of your first routes i'm going to show you some of one of the the nice little that's not a hidden benefit but it's something that you you wouldn't expect to get right out of the box so first things first here's the the page for fast api uh the link will be included in the description below as you can see here it calls itself a high performance easy to learn fasta code ready for production uh web framework it is based in python you have to have at least 3.6 it is a much lower level framework as opposed to something like django which is going to be more opinionated it's it's more like flask or let's say express for for javascript in that you you can kind of create your routes as you need you can create your controllers things like that as you need there's no real there's no real set way you need to do it there are conventions and things like that but there is no set way to do it it is built on top of starlet as a little skeleton ascii framework that it uses and it uses it makes heavy use of pedantic for type checking data validation you know things like that so without further ado let's get into some code i'm going to open up a powershell we'll go into youtube and we will first create our directory we don't have anything set up we're setting up our entire app from scratch right now so we need to create the directory first and then what we're going to need to do after that is we're going to need to create the virtual environment most of you in here i would imagine have python installed already i'm on version 3.10 if you don't go install the latest version of python um for those of you who already have python installed on your system and have done a python project before you know that the first thing that you should always do is set up a virtual environment now i'm going to use pip you can use pip end but you can use poetry i used all three for something simple like this i see no reason not to use a tool like pip it's it's very simple it's very straightforward after that uh this is all we're gonna need the powershell for um i'm gonna open up uh pycharm that is the the editor of choice for me um i i started using pycharm before vs code kind of kind of blew up i already paid for it so this is the one that i use apologies if you're a vs code user nothing against you i wish i could i wish i could start using it i just i know the key bindings here i'm much more comfortable with with pycharm so we're going to set up our requirements.txt file and we're going to end up installing fast api and we're going to install uvicorn which is the the ascii server that we're going to use to serve our fast api app we'll come into the terminal here we're going to do end scripts activate and we're going to pip install dash r requirements dot txt while this is installing this is for those of you who are using pycharm i'm going to set up the python interpreter it's going to point to the one depending on how you have it set up it might just automatically find it and point to it by default i like to do that just because it'll give some nice code completion that you might not otherwise get okay everything is installed now what we need to do is we need to create our main.pi file so let's go into here and we will create main.pi so now this is where the the base of your app is going to live you'll see there are going to be situations where we'll have our app in in multiple files multiple packages you know there's going to be a whole lot of stuff that's going to come into play but this is when you're starting out and building a simple fast api app it's all going to take place in here so first we're going to import fast api and we're going to instantiate our app there now we have a fast api app good for us the first thing we need to do is we need to set up a route so we're going to use the app.get decorator and we're going to declare our route it's going to be our root route we're going to return a very simple message hello world and i'm going to go back and change this because the single versus double quotes is bothering me and anytime i use black on this it ends up reformatting to double quotes so i'm just going to continue use double quotes great now we have our app set up now what do we do this is the sort of situation where if you have used django before you would run python manage.pi run server we need to come down into the terminal and we're going to instead use unicorn so here we're going to do uvicorn main colon app that's the first part of the command that we're going to work on and i'll explain what each of these things does so first things first main refers to the fact that we're calling this file here main.pi app refers to the fact that we're calling our app app if we were to rename this hello and we were to rename this world surprise surprise we would do uvicorn hello world and it would run we can see it ran great good for us i'm going to go back and i'm going to change this back though because i'm not going to build a fast api app called hello world there so we've got our uvicorn setup main app now there are two other flags that i'm going to tell you about there there are other things that you can you know pass in his flags to evil corn but the first one is going to be port equals you can you can choose whichever port you want you can choose i mean not whichever one you want but you know you can choose five thousand you can choose three thousand you can choose eight thousand one if you're already running eight thousand somewhere else whatever you want um the default port that it uses is eight thousand i'm going to stick with that but you can set that flag the other flag that we care about is the reload flag now what we want to have happen is we want we don't want to have to hit control c and then up enter every time we change something in the in the files that's where the reload flag comes in it will look for any changes that are saved in your entire app and if something updates then it will automatically restart the server which is you know it's it's i mean you would go crazy if you didn't have that so there we have our app up and running and let's go to localhost 8000 and i'm going to bring the window over here let's scrunch this up just a little bit there we have our app running yay hello world good we're good now this is this is a great start um the the other thing that i'm gonna show you though um and if we wanted to app dot post we'll do async def post return message hello from the post route there and you can see on the very bottom there that the server restarted we're good now the thing the problem that we're going to have with this is we can't actually submit a post request from the browser uh there are tools like postman uh insomnia things like that um and and honestly i mean i took a a a production ready not a production ready but it was a course from brad traversy on building um product like professional apps in express uh back end in that he sets up an entire postman uh set up for documentation for like it's it was crazy level with you that was phenomenal the course itself is phenomenal and brad travers is phenomenal but what what fast api gives us is it's a nice alternative to it it comes right out of the box and i'm going to show you exactly what that is now if we go to localhost 8000 and we type in slash and we go to docs and we hit enter here we have swagger documentation swagger docs swagger documentation for those of you who are unfamiliar with it swagger docs um it comes out of the box with fast api you can see we have our get route we have our post route so we don't have to worry about installing postman we don't have to worry about doing anything like that this gives us the ability to have this documentation right away if we came in here and did app.put async def put return message hello from the put route we refresh the one thing that we don't get is automatic reloading because this is not you know react or view or spelt we have to refresh but now we have our put route we hit try it out and we hit execute and there we go we got a message of hello from the put route this is i mean i can't i can't stress enough just just how much of a game changer this right here is i mean it's it's great to be able to write this sort of uh this sort of an app this easily but just from the perspective of having the documentation not worrying about setting up postman routes and things like that this is this is just absolutely phenomenal and we'll see in future videos that we can actually have authenticated routes in here as well there are ways to set that up there's different ways to organize this this is i mean we can see this is called root but if we call it base get route and we hit save and we refresh this automatically updates here um we can try description equals this is our first route i'm showing you all stuff that that we're going to see as we as we move forward but if we look at this we see we get documentation here we can say deprecated equals true and we refresh and automatically we get that this is a deprecated route you can still do it but it's deprecated so it yells at you there's a lot that really is phenomenal with this um but yeah so that's that's it for now um [Music] this is our basic fast api app it's it's that simple in the next video we're going to touch on path parameters you know if you want to have certain variables that go go in the path like um not here we would do id if you want to pass in an id parameter as you're you know you're updating something um this code will be in a github repo i'm going to update it with each lesson as we go along so you can follow along i think for now though that's it i will see you in the next video
Info
Channel: JVP Design
Views: 119,470
Rating: undefined out of 5
Keywords:
Id: XnYYwcOfcn8
Channel Id: undefined
Length: 13min 0sec (780 seconds)
Published: Tue May 03 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.