Minimal Flask REST API in Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] what is going on guys welcome back in today's video we're going to build a simple and minimal rest api in python using flask and for those of you who don't know what a rest api is it's basically a thing that you can send requests to http requests and you get some responses it takes some actions maybe in the background uh not the most technical not the most scientific definition here but basically you send something to it it does something and you get a response as a result that's the basic uh definition of a rest api if you break it down and we're going to build that in python uh an example for a rest api could be a to-do list for example you just say okay create this new to-do item or show me the to-do item number five show me all the to-do's delete this one and so on those would be typical uh rest api calls and today we're going to not build a to-do list we're going to build a video schedule so we're going to have a rest api where we can schedule new videos that need to be uploaded and produced and all that so we can create new videos we can delete videos we can look at the videos we can sort them and so on this is what we're going to build today you can choose whatever example you like you can also make a library management system you can uh have your to-do list whatever you want to do as an example here we're going to do a basic video schedule so we're going to do first we're going to open up cmd and install flask and for this we're going to say pip install flask once you have that you're going to also install pip install flask restful and those are the two libraries we're going to need we can start by importing them by saying from flask import flask with a capital f then from flask underscore restful import the resource and the api and we will start by creating an ordinary flask app so we're going to say app equals flask and the name is going to be video api and then the api is going to be api off app so let's start with a class for the individual video so we're going to say class video and it's going to be a resource so the class video is going to extend from resource which is part of the flask restful module and what we do here is we basically define the methods for get put post and delete and so on for the basic crud methods that we have so what we're going to do is we're going to start with a basic get and for this get we're just going to take the self object and then it has to return something now for now let's just return hello world to see how it works we're going to just say hello world is going to be the result of the get call to the video resource and in order to actually make that happen in order to actually have this resource uh being accessible we need to also add this resource to the application and for that or to the api actually and for that we need to say api dot add resource going to add the video class as a resource and what we also need to add is a path a url mapping so to say so if we go with slash it's going to be the default and if we just visit localhost in this case or vip where this app is running on we're just going to see all all that the get function returns in this case hello world so in order to get the app running we need to say if underscore underscore name equals main and then we're just going to say app.run and we can right click the file and run this and there you go our first rest api is running so we're going to open up the terminal and we're going to curl our way into the result so as you can see it's running on localhost port 5000 i'm going to use my command line here to say curl and then http localhost 5000 there you go you can see hello world is the response and we can of course also do the same thing with the browser so i can just go to firefox and say local host and then 5000 and i'm going to see hello world here as a json output so this is how we basically set up the rest api all right so let's now go ahead and create an actual dictionary full of videos so that we have a resource to return and not just a static string we're going to save videos equals this dictionary here then we have video one is going to be a dictionary itself and it has a title i don't know hello world and python for example and then we say video two also dictionary with a title in it and the title is uh why matlab is the best language ever there you go very reasonable video and what we're going to do now is we're going to not return hello world we're going to return the full videos dictionary here and if we run this you're going to see that this works because if i now curl the resource i'm going to get the dictionary with the videos and if i do this in the browser i'm also going to get the videos here as a result as a json object here and this works now what if i don't want to have all the videos i only want to have specific videos if i want to do that i want to i have to add a parameter here so i have to say for example video id or video number whatever we want to call it and this video id has to be passed to the get function and we can say here when we add the resource to the api we can say that it's going to be part of the url so we're going to say for example slash videos and then i'm going to use these angle brackets and say video id and this is automatically going to be linked to that parameter so this here is going to be linked with that and when i do a get call onto that url obviously here is going to be some identifier and not that string then i'm going to get it into that function and by doing that i can just say return videos and then video id like that and if i now restart this first of all this is not gonna work anymore as you can see 404 now if i go to videos slash test i'm going to see internal server error because there is no resource there but if i say video 1 you can see i get hello world and python video 2 why matlab is the best language ever and video 3 doesn't work anymore so this is how we can do that and of course we can also try something like if video id uh equals actually if it's empty does that work not sure if that works we can try that we can return all the videos if no idea was specified i'm not sure if that's going to work otherwise we can just use something like all slash all for example but let's just go ahead and see if that works no doesn't work but the rest does work right okay uh so let's try and say all and then we're going to rerun this and we're going to curl slash all there you go you can see that works i'm not sure why the empty string doesn't work to be honest but doesn't seem to work so we're not going to do it but with that it works we're going to do it a different way anyways so let's just forget about this part for now but this is how we can access individual resources now let's go ahead and add a little bit more action to it by also allowing for different methods like put post and delete because get is basically just for getting something from the server and if we put something we update it we change it or we create it so what we're going to do is we're going to say def put and we're going to also pass a video id here so that we know what we're creating there and in order to create a new video we need to also specify the title for example and if the video has multiple fields of course we need to specify all of these fields in other words we need to add some arguments we need to add some arguments to the uh to the request so that we know what are the parameters that we're passing to the creation process and in order to do that we need to parse these arguments and in order to do that we need to import something which is called wreck parse from flask restful import reg parse and we're going to create a parser up here so parser equals rectars.requestparser and the parser accepts an argument called title and this argument is required there you go so now this parser asks for the title and if it doesn't get it it doesn't work so it has to be there has to be a title whenever we make a request with that parser and in the put function we're just going to say the arguments passed to that call are going to be parser dot parse arguments and then we're going to say new video is going to be a dictionary with title arcs title by the way if we want to use a certain name for the passing because we're going to see in a second that we have to specify what we're passing so we have to say okay we set the title to xyz if we want to have a certain name here but then we want this name to end up in a in a different location here we can use the destination keyword here so we can say dest equals something or something else and then this is what the user is going to input and sdh is what we're going to use here in the arguments dictionary so we're not going to do that but this is how this would work and now we have this new video and all we're going to do is we're going to say video videos actually just videos video id equals new video like that and we're going to return something we are going to return the what are we going to return we're going to return video id and the new video or actually videos video id there you go and we can also specify the actual http status quo so we can go with 201 which is i think successfully created and in general also for the get function we can specify okay what is the code that we want to pass 200 is default 201 is successfully created i think 204 is successfully deleted i think i'm not right quite sure you can just google that but we can specify the status codes in this case we're going to return 201 and uh that's basically it that should be enough in order to allow for the creation of new videos so let's run this and first of all let's see if the get still works so let's clear all of that curl http localhost and then 5000 videos video one this works video two works and slash all works as well so now let's go ahead and create a new resource by saying slash video slash video three first of all let's see that this doesn't work okay and now we're going to say minus d or dash d for data and we're going to say title equals my new video for example and then we need to specify dash capital x put because get is the default and if you want to specify something you say minus x and then put and if i run this now you can see i get this as a return and if i now try to get all i can see that there is a new video so if i go to localhost slash videos slash all we can see that there is a new video in here all right now let's go ahead and also implement the delete method we're going to say def delete self video id and then we're going to say if the video id is not in videos i think that's enough if it's not in videos we're going to abort so we're going to say pass for now uh and then we're going to say otherwise or actually what's the problem here yeah okay if video id not in videos then we're going to abort and otherwise we're going to delete it so we're going to say dell videos video id and then we're going to return an empty string and 204 was successfully deleted right so that's basically what we're going to do now for the abort we need to import from flask restful also the function abort and instead of pass we're now going to say a board with the status code 404 because that is not found because we don't have this video id and we're going to say um i think message is going to be video id not found there you go and we can also add the same thing by the way to the get as well so like that if the video id cannot be found we're going to get 404 if it's all we're going to return all of them and otherwise we're just going to return the specific video that was asked for uh that should work so let's run this again and see if it works let's curl everything first video all not found oh probably we should do that first because all is obviously not part of the videos but it should still work so we need to do that first there you go so now let's also try to add a new one there you go it works and if i now try to get something that doesn't exist like that it says not found and if i try to delete something that does exist so let's go with video 2 for example we just need to say dash x delete like that then if i run all you can see that video 2 is no longer there but if i try to delete something that doesn't even exist so video 6 for example it says video video 6 not found all right that works now what i would like to add here as well is another resource which is a video schedule itself and not just an individual video and for that we're going to create a new class here and we're going to call it video schedule it's going to also be a resource and it's going to have a simple get method that basically just returns the whole object and it's also going to have a post method and this post method will create a new video but without needing a video id so we're just going to say post and it's going to create a new video and it's going to calculate the next video id that is free uh and we're going to ignore the gap so if we have one two three six seven eight we're going to go with nine and not with four so we're going to say def post self and we're still going to have to pass arguments so parser dot parse arcs and then the new video is going to be title arcs title yes there you go and then the video id is going to be a calculation it's going to be the max of the end of v dot l strip yeah max v l strip video it's a little bit more complicated let me think about this for v in videos dot keys and the result of that plus one so what we're doing here is we're taking all the individual video ids that we have so video one video two and so on we're stripping away the video so we end up with a number we convert that into an integer then we have a list of all that we get the maximum from that list and increase it by one this is going to be the next video id and what we do here is we basically say videos video id new video and then return videos video id and 201 there you go so i think that should be it of course we need to also add the resource so api dot add resource video schedule will be accessible by going to slash videos without specifying anything else and if i run this now we should be able to just do what we did before so we should be able to curl all and this works but we should now also be able to do the same thing with just videos and now if i go and say videos and data equals title or actually data is title [Music] equals newest awesome video and then dash x post then we should be able to see that a new video was created with only three that's not good so we need to change that oh obviously this is just a number so we need to also add to this video id equals f fstring video and the video id so not just the video id itself not just the number itself but also the video in front of that let's run this again let's see again what we have here let's post again let's see again and there you go video three was created now i can run this command a couple of times so it's always going to be the same title but we're going to see that if i now go to that we have a bunch of new videos four five six seven eight nine so that works now one thing that's problematic here with the current situation is that we have this local dictionary here that is initialized at start and we manipulate it we add to it we change we delete and all that but once the app is closed everything is gone and this gets loaded again the next time we start the application now usually you want to have a database or something in the back end today we're going to just use ordinary json files so what we're going to do is with every change we're going to save that result into a json file but before we do that i would like to introduce another argument we're going to have in upload date so we're going to say parser.add argument upload date and this is not going to be required the type is going to be integer because i don't want to mess around with date right now so required is going to be false but i think it's false anyway by default so i'm not sure if that's even necessary and we're going to add the upload date here we're going to use the format year month and day because that is the best for sorting so we're going to say um 2021 for example 0 9 for september and then 17 for the 17th of september 2021 that would be the number that represents the date and we're going to add here the upload date of this video is going to be uh 2021 november and 18th of november for example there you go and of course we need to change in the put method here also the upload date to args upload date and also down here we need to say upload date is arcs upload date there you go um let's see if that works let's test this before we do anything else and then we're going to talk about the json so first of all i should be able to still post without the upload date so this should still work and if i look at that oh there you go it works okay so now if i want to also add the upload date i'm going to add another dash d and then upload date is going to be 2022 0 1 0 1 for example there you go and if i now look at the results we have this here as well so this works now we're going to work with json we're going to import json the library json in order to be able to do that and we're going to create a function called write changes to file so def right changes to file and we're going to just store the videos into that file but before we do that we always want this videos object to be sorted so whenever we do something we want to sort it and then we want to save it into the file which will end up or which means that in the end we're going to have a sorted file and we're also going to have a sorted life object um and what we're going to do for that is a dictionary comprehension so we're going to say first of all global videos so that we can even access it and then we're going to say videos equals k colon v for key value for k and v in sorted videos dot items and the key the the the criteria for sorting is going to be lambda the video and of that video that we're currently looking at we're going to take video uh one for the values so not for the uh for the key but for the value and of that we're going to take the uh upload date because if we go with zero we're going to get the actual key so the video id if we go with one we get the value and the value as the dictionary itself so we need to get the upload date i'm going to sort based on that so now they're sorted and then we're just going to say with open videos.json writing mode sf f dot or actually json.dump videos into that file and every time we run that we want to also load that so one time we're going to run it with the default values here i'm going to stop that we should now have a json file here oh no we don't have it because i didn't call the function right changes to file there you go let's run this bam stop that we now have a videos json we need to reformat it to see what it's about there you go then we can delete that and we can also delete that and we're going to say with open [Music] videos.json [Music] in readingmode sf videos is going to be json.load f i think that is how it works all right so now every time we do something every time we put something or anything like that we should call write changes to file this will automatically also sort the videos object or the videos the videos dictionary and we should do that here as well so write changes to file and down here as well right changes to file all right and now we should be done let's see if it works let's go ahead and see what we have here okay we have that so now let's post something to it uh let's see if it's sorted or actually in order to see if it's sorted we should add something with a small date so let's go with 2019 and change this to something else here and then we post that and if i now query everything you're going to see that the 2019 video even though it's video four is at the first place and if i run this in the browser as well we see video four is the first one two three because the date is the important thing so the date determines the position here now if i close the server here and if i run it again we're going to see that the information will still be there there you go all the objects are still here because they're saved in a json file the json file is here and i can again reformat that and you can see that the values are now stored in the back end so that's it for today's video if you enjoyed it hope you learned something if so let me know by hitting the like button and leaving a comment in the comment section down below and of course don't forget to subscribe to this channel and hit the notification bell to not miss a single future video for free other than that thank you very much for watching see you next video and bye [Music] you
Info
Channel: NeuralNine
Views: 3,509
Rating: 4.9833331 out of 5
Keywords: python rest api, python flask, flask rest api, minimal rest api python, flask tutorial, python flask tutorial, python rest tutorial, rest api tutorial, rest tutorial, python rest api tutorial, flask rest api tutorial, flask-restful, flask restful
Id: xqJXDZvtogY
Channel Id: undefined
Length: 25min 36sec (1536 seconds)
Published: Tue Sep 21 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.