Deploy your Flask app on Google Cloud Platform

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
going to be showing you how to set up a flask app and deploying it via Google Cloud uh so first step is to say you're going to need python installed you need VSS code and you need to know what a virtual environment is I wrote all that stuff in a blog post in the link below so give that a read if you don't know or Google some of those Basics um so we're going to get started first by creating a virtual environment so I'm going to just python DM uh VN and then vnn so what this is that python is making a virtual environment we're calling it environment or EnV for short all right generate an environment let's give it a second here there it goes all right and then uh the how we get into the virtual environment is that you're going to hit en andv and then hit tab it autofills those details for you in vs code then capital S C or for scripts and then act for activate it's going to be EnV bin on Mac OS all right we're in the virtual environment which is indicated by this little uh parentheses in the lower left all right first thing we're going to do is pit install flask uh if you don't know what flask app is basically it's a library made up of other libraries that let you run a web server so I'm going to show you how to do that really quick so in this little button the upper left we're going to create a file call it app. P Y and then finally we're going to come here and import flask from our library here I'm not going to import all that other stuff just going to leave that there and then we're going to initialize our app so we're calling flask right here and it's going to initialize the app so it's going to use the code from the library uh call it call it when we run the code and then we're going to do at app. route so in the server we're creating a URL route or this is a Home Route here and then we are going to then hit enter and then create a function called hello world which is going to act as like an endpoint that will return hello world when we go to that URL so if you could imagine that in this line we could do slash API version one SL hello world for instance and then your you know your address so 27.0 01 slash yeah basically this would be the URL address that you would access short little tutorial there we're just going to go back to basic home and then if we need at the end of it if underscore name do equals name true and right and then app run. ebug true so this is a simple hello world flas G right uh we are just going to make sure the code works and that python works because you always want to check that it works so we're going to do python d python app.py okay it ran we can hold down control and left click here okay we know the flask app works here now all right then we're going to pip install some basic stuff so pip install Dot EnV because we want to call uh environmental variable so on deployment you need to pass secret keys for databases or Integrations like Google sign up or apple signup so um basically the simple. EnV file is going to allow you to pass variables to your app on deployment and work with them locally so just to show you how it works we're going to do flas debug and we're going to come back to the app and we're going to import that Library so from do EnV import load. EnV all right and then below here we're going to do load. EnV um and I forgot import OS so what Os Os does here is let us access this file through the flask app all right so what's going to happen here is that we're going to get an environmental variable .a config then we're going to select debug now um what this is is that this is the object and then this is like a property of that object or a variable that is part of it need to encapsulate that in a string then we're going to call Os thenon dogit and then we're going to do flask debug so if we want to get this variable or like a secret key from the EnV file we need to get it specifically like word for word call that the specific line and then pass it to the app and now we don't need debug equals true because that's just a parameter of app when we're running the method run so what's happening is this object is being called by Script and then we're calling a function run the server and then there's these like properties inside of it that we can kind of observe all right so I'm just to show you that it works all right it runs great all right I'm going to add some other stuff we're going to do pip install flas cores and then pip install G unicorn so um later on the series we're going to use some react code and we're going to call it API um when you call it API you're going to use something called cores or cross Origins um um it's just a web standard to make sure that you're passing correct headers to the back end and the API it's just it's pretty much necessary so you can pass data back and forth between applications um without it being like kind of middleman attacked and then you have G unicorn and G unicorn is like production code for the flask app to deploy through all right we're going to go ahead and update the app with those packages so we're going to do from flask cores import cores and then we're going to also have um that's it um and then we want to encapsulate the app we can actually move that there and then call cores around the app and then so what's happening here is that we call the libraries cross Origins and now the app will accept cross origin uh API requesting and that should just make sure that works so when you deploy your API and you're deploying it for a front end you want to have cores essentially for that uh now here I'm going to pip freeze the requirements uh what that means is that my virtual environment if we um I think it's pip list if I remember yeah so um I installed all those packages and they're just like a bunch of libraries that or libraries of code for you to kind of run the server right um You need to save the requirements so on deployment the server at Google knows what to install when you do the deployment all right so what we'll need here is the AML file diml and then we're going to do runtime python 39 so python 3.9 entry point g unicorn. e. Port app apppp all right and then you're going to go back to your EnV flash file and then do Port equals 808 so that's like a deployment for so Google needs a SLE file to deploy um that I'll explain a a little bit on all right so you created a flask app and you're going to have to deploy on Google and that means you have to go to Google and create an account and then go to Google cloud and then create a new project I already created a project um create new project test project to create okay in the upper left it's going to spin here um you would just select the project like go to the project dashboard and this just kind of shows like some deployments I've already made um this information is going to just help you later on so all right that's done all right now we need the Google uh CLI so if you type into Google CLI uh you should get something like this you click here and what you'll see is you're click to the relevant you know if you're on Linux go here if you're on Up 2 go here if you're on Mac follow these instructions I'm on windows so what we're going to do is click the installer okay download the installer um here you don't need to do any of this it's just screen reader for accessibility and then if you want to help Google improve their CLI I agree to the terms you select um what you want this installed to do you click yes yep and then you'll install I'm not going to install this in front of you because I already have it but it's just going to be like a green loading bar and that's pretty much it um so once that is installed uh what it did was essentially install a version of python 3.9 that's what Google's using now we're going to go ahead and it'll ask you to do uh get Cloud a nit so if you go back to the terminal here um you want to check that we have Google cloud first of course just type in Google Cloud help there we go the help showed up so it's just giving you some basic like how the command line works just going to cancel out of that but we're going to Google AIT that means to initialize um I can't show this process but what will happen here is that it's going to ask for your email this is my email and then it's going to ask you for a password and authentication so you're going to go through uh a login process and then it's going to connect your account with the Google Cloud account and then it's going to ask you to reselect the project so I'm going to reinitialize the configuration so I'm going to click one all right um here I can log into another account by doing this by pressing two but I'm going to press my email again because I don't want to create a whole new account to show you how it works and then I'm going to select a project I've created a ton of projects going to select um actually project six here which is the python lecture series we're doing and then finally uh it's configured it's done so anytime we're deploying here I'm deploying through this specific account like if you're working for a company they'll give you an email address you'll work through that account and then you'll deploy through there and then it should be pretty straightforward all right back to the next step after you installed the CLI we are going going to then set up for deployment and what this means is that we're going to uh create a Docker file and then um from this is Google's code is that you're going to have from python. point3 and then you're going to have copy the local code so what this means is that this code in your terminal is going to be copied from the docker file and put onto the Google server then you're going to run these commands we have run pip install upgrade pip and then requirements so if you don't have this text file you get a error on deployment and then here we're going to have a run command so just came with commented code to explain things but gunicorn is a tool where when you Deploy on the cloud you're using a CPU and a CPU has cords cores so you could like limit your server to only use like one or or two cores to save resources for example and then it can kind of like increase the number of cores if it gets like more requesting users and stuff so here it's going to call G Unicorn it's going to use that Port it's going to use one worker have eight threads so like one CPU eight workers eight threads I mean um excuse me and then the app call all right and then we're going to need uh Docker uh do docker Docker ignore file what this file is is that basically we have a virtual environment and we have this EMV file and we have um we got DOT requirements so in this EnV when we run python code it compiles Python and it creates like memory saving caches just saving memory and the docker file itself so what's happening is that when you deploy we're we're literally just copying we're going to copy this director this directory right uh we don't want to copy everything like the readme file we don't want to um Docker file we don't want to copy itself and um that is how you prepare for deployment so now that you have this set up we're going to do a Google Cloud run so let me just zoom in a little bit here I'm actually just clear this all right so it's going to ask me to name the service so when you're deploying you're going to use resource from Google and then they call it a service and that's how they track what resources you're using now here it's going to ask you to select a specific region so for example if my app is deployed in the east coast and have users in Australia they will experience latency um this is a a trick you could solve with low balancing if you're like in devops and things like that so now I'm just going to select the East Coast all right it's going to say allow on you're going to say yes to this um there's a way where you can like log in the back of the API and you can just have no and set up security features just for the sake of this video and if you're doing a startup we're just going to get this started and say yes okay we had our first error but I planned this and all that happened was your Docker file was not capitalized that's it that's all that happened here um this is a very common error that people tend to get because they forget the dock file needs to be a capital D and several lowercase D we're going to do that again test 33 uh then we're going to say yes all right and then we're on our way to deployment okay perfect so it worked it said done here's the test revision um it's going to serve 100% of the traffic we're going to hold down control and then left click we're going to open that up in our browsers to the left and there it is it's live hello world so now we're going to go back to Google uh console now here uh if I'm in the correct Library uh sorry correct project project Le python lecture um here on the left you'll have this navigation you're going to go down to Cloud run and it's telling you what is running the service is running test and that our test is running and we're accepting stuff um we're going to go back and we're going to go back to the dashboard and you can see here that I made API call for deployment it's going to charge you 1 cent to do that so uh anyways we're going to go back and go back to to Cloud run we're going to select test so here we just click test and then we click delete here that's how we remove it so yeah that's it um you can also do it from the command line I have that in the blog post um so that's just how you make a flas app deployment so other than that I just want to say uh thank you for watching uh subscribe like the video also uh short advertisement here I run a Consulting service so please check out my website I have a free trial if you need help with a small business or startup or just try and get an app constructed I do ux Consulting brand Consulting and product Consulting so that's just my plug thank you for your time
Info
Channel: Leslie Wubbel
Views: 12,930
Rating: undefined out of 5
Keywords: google cloud, python, deployment, docker, dockerfile, coding, tutorial, howtodeployflaskapp
Id: 7-s5ugThckY
Channel Id: undefined
Length: 17min 51sec (1071 seconds)
Published: Mon Oct 09 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.