Join Joseph Lowery for an in-depth discussion in this video Constructing the code, part of Google App Engine Essential Training.
- Okay, let's jump into the code. Now before proceeding, you'll want to have installed the App Engine SDK for Python and get the Google App Engine launcher up and running as we covered in the previous lesson. In this lesson, we're gonna build out two short files. One for the Python code and the other to configure the application in the YAML. So I'm going to my Desktop first and create a new folder. And I'm going to call this one ae, App Engine, -python.
I'm gonna use ae-python to hold the folders for all of my apps that we'll be building in this chapter. And I'll create another new folder and give it the name of my first app which I'm gonna call test-py-01. All right, let's head on over the code editor. I am using Aptana as my code editor which is great for Python development. I've already configured my Python interpreter through preferences.
And next, I'll promote my just created ae-python folder to a project for easy access. So that's on the Desktop. And then right-click on that, Promote to Project. Click Finish and there we go. And I'll select that folder and I'm ready to create my first page. So I'll right-click on ae-python and choose New From Template, Python. Let's call this one main.
Click Finish and we're ready to start coding. The first bit of code I'll insert, we'll bring in an application framework called webapp2. And that's built-in to the SDK and App Engine. webapp2 is WSGI-compliant. That's short for Web Server Gateway Interface and has all the functionality that we'll need to get up and running. So the code for that is import webapp2. Next, we'll create a class called main page that uses the request handler found in our webapp2 framework.
So, class MainPage (webapp2.RequestHandler): and then we'll define a GET Request method which will use self.response.write to output some text. So def, for define, get and this is a self. And like I said, this is self.response.write. Now, we're ready to output some text. Put that in single quotes. And I'm gonna output a little bit of HTML, just an h1 tag, Test PY 01 Lauched.
And then close out the h1 tag all within the string, of course. The final snippet of code sets up the WSGI application instance. So app = webapp2.WSGIApplication and then a open parenthesis, square bracket and a couple of arguments. But first is our app root URL. We put this on a new line here and back that up so we can see it a little bit better.
So the app root URL that I mentioned will be represented by a forward slash, followed by a comma. What happens when we encounter that forward slash, we return the MainPage class. Finally, let's go ahead and include a debugging statement just in case we run into any errors. I'll save my file and we're done with this Python page. Now let's make the second of our two files. This is a configuration file for App Engine.
And it's done in a language called YAML. So I can choose New From Template, YAML, and we'll call this app.yaml. And then once again, we'll walk through the code line by line. First, we'll name the application. application: and I'm gonna call this firstapp. If you ever worked with YAML before, you'll see that it's basically a series of name-value pairs like application: first app that use a colon as a separator and there are no quotes or quotation marks involved.
So next snippet of code is setting up the version of the app. And we're going to be version: 1. Next, we'll specify the runtime. That's the language we're going to use. And in this case, we are, of course, using python. Then we want to set the API version. api_version:, again, 1. And finally, we'll set the Boolean threadsafe equal to true. This allows the app to handle simultaneous request.
It's a key feature of App Engines Python environment. Well there's our main configuration code. Now, we just need to set up a request handler to point to our primary PHP file. So, we'll start with the key handlers. And then on the next line, insert the hyphen which in the YAML means you're going to be setting up a list of name-value pairs. And then we add one which is url: /.*. This is a regular expression, the period, the asterisk, that basically means every request at the root of the app.
And once that request is received, the next line dictates what to do. And that is to run a script called main.application. Okay, I see a couple of errors here with the spacing. Spacing is critical in the YAML. I need to make sure that my hyphen goes all the way to the left column. I do wanna also make sure that script is in its proper place which is two spaces and indented, like so. So it looks good now. Again, I'll go ahead and save this file.
And now that both files have been created and saved, we're gonna head back to the App Engine launcher. But before I do that, just a quick glance over to the Project Explorer on the left-hand side. I realized that while I've saved the files, I somehow did not save them within the folder that I had intended to save them into. So I'm gonna go ahead and move all of these files over and bring them all within test-py. And that is fine there. I've got my app.yaml and main.py files there.
It's exactly what I need. Now, let's head over to the Google App Engine launcher. And I'll first task here is to set up a project. Now, there are couple of ways to do that if you're starting from scratch, you choose File, New Application. However, since we have our files, I'll choose File, Add Existing Application. And then once that's done, I'll browse to select my file. We'll go to the Desktop, ae-python. test-py-01.
Identify that folder and click Choose. Now, you'll notice that there are both Admin Port and Port values supplied for us. You could, of course, change these but let's leave their default values and I'll click Add. And there's our new app. As you can see, the launcher reads the app.yaml file and it's got the application name firstapp. And then the Path and Ports are also shown. And if I go ahead and select that and click Run, should get a small green dot if all goes well in the left-hand column.
And that looks good, I do. Now to view the app, we'll need to switch to a browser and we could navigate to it by specifying the Port 8080 or we can also just select the app again and click Browse. And there's my output string. Now once the app has been launched, we can make any changes we like directly in the Python file. So let's head back to our main.py code. I'm going to add another line. It's gonna be another self.response.write.
So let me just copy that and paste it. Then I'll change the value. Let's make this an h2 tag. And I will change what it says to Running Google App Engine in Python. So then I'll change the closing tag to h2 as well. I'll save the page, back to the browser. And just hit Refresh. And there's my new code. Great. Well, our first Python app is officially up and running. Good going.
- Understanding the Google App Engine
- Using the Google Cloud Console
- Setting App Engine services
- Coding your app
- Working with images, style sheets, and other static files
- Incorporating HTML templates
- Uploading and deploying your app
- Implementing Google Cloud Storage
- Setting up a custom domain