GCP Python

Hello and welcome to this Google Cloud platform in this Blog. I'll be showing you how to deploy a python web server to app engine in a flexible environment. So to get started, we need to go up and get to the Google Cloud platform console. Okay? With the console loaded, we're going to need to create a new project, which is where we'll deploy our code. So if you select project and create project, and a dialog window will appear, which gives us the ability to enter the project name. Now, you need to be careful to enter something unique here, because by default, the project names form the URL of the application. Okay, so that's done once that's created. The project dashboard will appear like this, and we can click in the top right to activate the goo Cloud shell from the Google Cloud shell. If you select files and launch code, editor will be able to write the python code that will form our app select file, new folder and give the folder a name. Now, this is where we'll just put all of our code. Now we're going to need three files. The first is appxaml. Now, the yeah, more file contains our configuration data. We're also going to need a requirements dot txt text txt file, where we're going to specify any additional libraries that we need to deploy our application, and then finally, mainpy, which is where we'll put the main body of our python code for the web app. 

Okay, so if we start entering, then the first thing we need to do is import the logging Library, and also from flask. We're going to want to import flask. Now, flask is just a web template doing Library written in Python, which lets us really quickly build web apps and does a lot of the heavy lifting for us. So we're going to declare a variable here called app and assign flask that and past two flasks, the name of our application there. Okay? And then we want to specify the roots for our app. And because this is a basic one, we just want to handle everything in the same way. So we're just going to deal with Slash there. Then we're going to define a function that we want to be called whenever somebody makes a web request on that path, which of course captures everything. So we're just going to have a simple function here called hello. And I'm just going to have that return. A very basic string hello YouTube, just so we know that the web server is working, and we can respond to browser requests appropriately. Okay. So, the next step then, and I'm just showing you this as a bit of a how to you don't really need to do this for the web server. But if you wanted to test it locally, this bit of code will allow you to do that. So we're just going to check that if the name of our application, so we're applications being called from is main, we're going to specify that we want our app to run on localhost, which is this IP address 127001 at one and on port 8080. And we're just going to set debugging to true as well. And this will just make it so that when you're testing your changes locally, you can deploy them to a web server and view them in a browser. Okay, so that's it. That's all we need for the python body of code for our web app. And we're just going to handle all requests it exactly the same way. So let's move on to the configuration file, then appyaml. So if you open that up, we're going to want to specify a few things here, for instance, the runtime environment, which is, of course, Python, and that we want to run in a flexible environment. And that just allows it to dynamically scale to meet our customers demands. Then obviously, we're going to want to specify an entry point. Now just trust me here, G Unicorn Dash B. This command will specify the entry point for application and deploy our web server for us. And obviously, we want to forward the port. And the entry point is, of course, our main dot Pi file there. So just finally is a bit of an aside. The runtime config here is how you specify a python version version, if you want it to by default python 2 is used, but I'm going to just specify python version 3 here. Now, that's not a strict requirement, but I just wanted to show you how to do that if you wanted to. So let's move to requirements dot txt. Now, there's a couple of terms, though, that you may or may not be familiar with. The first is flask. 

As I said, flask is just a templating library that lets us really quickly build out H web applications. And the second is G unicorn or green unicorn. Now, that is aw SGI, which is a web service Gateway interface written in Python. And basically, that just does the heavy lifting for us and automatically deploys our web app. Okay, so with those specified, we can go back to the cloud console and list everything there. So we can see the python demo files there. And I'm just going to change to that directory and then run the command to deploy the application, which is, of course, g-cloud app deploy, just approve the URL and hit enter, and it'll progress for us. Okay, so that's deployed. Now, it may take a bit longer on your machines. Obviously, it's faster when you do it for subsequent deployments. But the first time it can take a little while. So if you copy this URL here, that's where it thinks it's deployed your code to and paste that into a new browser tab. There we go. We can see that's the string. What you going to do is from the Google Cloud platform. You will have created a project if you have not already. You can call it whatever you'd like. You might also need to set up some billing for the account. But assuming you've already got all that done from your projects, dashboard, what you're going to do is you're going to click on the little icon up here to activate the cloud shell on top right corner. Basically what you're going to be doing is cloning this whole repo. So you're going to copy the URL. And then from the console, you can just type in git clone and then control V to paste the repo I've already pasted in and uploaded this repo. So I'm getting this error message, but on your console it, if this is the first time it will download the files. And then the next thing we're going to be doing is opening up the editor just to get a feel for what these directories are make. And in this git repo you're going to see like there's a bunch of folders. They're doing a bunch of different applications. The thing that we really care about is under the python docks examples or samples app engine flexible. And then there's a directory called hello world. And this hello world is running a flask app. And so if you look at the main dot, Pi file, you will see it using the flask framework that you all are used to. And then, in addition to that, you've got this appyaml file. And this is where you are specifying the config vars for Google. So if you wanted to start scaling up an instance, if you had thousands of users, this is where you do it. It's also where you're entering in your commands through G unicorn to actually get your app started. So they've already configured all of this for us. So it's making our life very simple now that we've just confirmed where the location is for this whole project. What we're going to do is we're going to open back up the terminal, and we're going to change directory into the actual location where we've got our project see about. So as you'll recall, this was under the python examples. So if we do LS, we're going to change directory to the python doc samples. And then it was under the app engine. And the subdirectory of that was just LS. 

They're flexible, flexible. And then hello world. And so once you've changed directory from the terminal to the correct location, what you will do next is run. The following, you're going to type in g-cloud app deploy and run it. And in this case, it did not ask me what my project ID was. Sometimes it will prompt you for what your project ID is. If this is a brand-new project, your project ID can be found by clicking on the name. And it is literally this name. So in my case, it's vsd flask Dash samples. You're just associating the Google cloud with this particular app that were deploying. And so you might trying to for that because of already specified that we're not going to have to be specified redoing it. And then Additionally, you will get prompted to declare the region. So because I'm in California, I'm going to choose something close to me for better ping. So we're going to go with us Central. So I'm going to type in 14 hit enter, and these things can take several minutes to complete. But basically, it's just going to want to confirm that you know what you're doing. So you going to hit yes to confirm? And this URL that it's giving you right here is going to be where you can get your app once they finished applying it. So I'm going to say yes to continue, and then let this thing proceed with setting up and deploying your application. And then while this is going, the other things we can talk about in this Blog are, you know, in terms of deploying your own application, how would you do that? You would upload your files to GitHub or whatever repository service you want to use, and then you're just going to be cloning that repository into your project for Google cloud and making sure that you've set up the demo file appropriately. So that in this case, mainpy was the python file that we're calling to start our web application. But in your case, you might be using something like run dot, Pi or application. Stop by. See, we just change that accordingly in order to get your specific flask app to work. But in this case, we're just sticking to the defaults that Google has given us and their documentation. And this is just help us confirm that everything's been set up correctly. And then we can start tinkering with stuff to get our own applications online. So I'm going to shut up now and let this thing finish, and we'll get back when it's done.