Google App Engine Python Tutorial using Eclipse Plugin

In this tutorial, we will go thru the process of setting up Eclipse to create Python Web Project using Google App Engine SDK (GAE) then run it locally and also deploy it production basically to your Google App Engine account.

1) Google App Engine Registration

If you have already registered yourself with GAE then skip this step otherwise just click on the link https://appengine.google.com/ and register yourself for GAE.

You can create up to 10 applications. Click on the Create Application button to create a new application. The application Id that you choose have to available as it creates a sub-domain for the appspot.com with that name so that you can access your application later using http://{your_app_Id}.appspot.com

Google App Engine Create Application

2) Setup Eclipse for Python programming

Depending on what your current setup is you may have to download one or more of the following, Python Interpreter, Eclipse, Java and PyDev plugin for Python Eclipse development. If you already have this setup done just skip over to the next step otherwise follow this link: Python beginner Tutorial using Eclipse PyDev Plugin

Important Note: The above tutorial downloads the Python 3.2 version, in your case download the Python 2.7 version as GAE currently doesn't support version 3.2

Google App Engine Python 2.7 Eclipse Setup

3) Download and Install Google App Engine SDK for Python

In case of Windows you can use the native installer file GoogleAppEngine-1.7.2.msi. Here is the link https://developers.google.com/appengine/downloads#Google_App_Engine_SDK_for_Python

Google App Engine SDK for Python

4) Hello World Project using Eclipse

Create a new project in Eclipse. File -> New -> Project. Search for PyDev and Select PyDev Google App Engine Project

Eclipse Google App Engine Project
Set the project name to HelloWorld and select the option "Create 'src' folder and add it to PYTHONPATH?". Click Next...

Eclipse Google App Engine Hello World Project
Click on the Browse Button and point it to where Google App Engine is installed on your computer.

Eclipse Google App Engine Intall Directory
Specify the same Application Id that you created in your Step 1. You can change the Application Id of the Project later in the config file. Also choose the "Hello Webapp World" template to generate our sample project.

Eclipse Google App Engine Application Id
The project generates 2 files. app.yaml is the Python configuration file for running and deploying your application. For detailed information click here Python Application Configuration.
application: my-sample-code
version: 1
runtime: python
api_version: 1

handlers:
- url: /.*
  script: helloworld.py
helloworld.py is our Web Application. It just prints hello world on the web page.
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app


class MainPage(webapp.RequestHandler):
    
    
    def get(self):
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.out.write('Hello, webapp World!')


application = webapp.WSGIApplication([('/', MainPage)], debug=True)


def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()

5) Run our Hello World Application Locally

Right click on the helloworld.py file -> Run As –> Run Configuration. Create a new PyDev Google App Run launch configuration and name it Run Python Hello World. Also specify the Main Module as dev_appserver.py
It is available under the GAE installed directory, you have to type the path for some reason the Browse functionality doesn't work.

Google App Engine Eclipse Run configuration
In Arguments tab for the Program arguments specify ${project_loc}/src. Click Run.

Google App Engine Eclipse Program Arguments
The application will deploy to port 8080 on the localhost. You can access it by typing http://localhost:8080/ on the browser.

Google App Engine Python Hello World webapp
Tip: To change the port to say 8081, you have to specify that in your program arguments.
--port=8081 ${project_loc}/src

6) Modify our Hello World Application

The default template created uses webapp. We are going to changes that to use webapp2. webapp2 is part of the Python 2.7 runtime since App Engine SDK 1.6.0. webapp2 is a simple. It follows the simplicity of webapp, but improves it in some ways: it adds better URI routing and exception handling, a full featured response object and a more flexible dispatching mechanism.
Copy the following source into your helloworld.py
import webapp2

class MainPage(webapp2.RequestHandler):
    def get(self):
        myPage = """
          <html>
            <body>
              <form action="/hello" method="post">
                <div>
                    <h2>Sample Hello World Application using GAE and Python</h2><br/>
                    <input type="text" name="yourname" size="30" maxlength="30"></input>
                </div>
                <div>
                    <input type="submit" value="Submit My Form">
                </div>
              </form>
            </body>
          </html>
          """
        self.response.out.write(myPage)


class Hello(webapp2.RequestHandler):
    def post(self):
        yourname = self.request.get('yourname')
        self.response.out.write("""
            <html>
            <body>
                <div>
        """)        
        self.response.out.write("<h2>Hello World - " + yourname + "</h2>")
        self.response.out.write("""
                </div>
            </body>
          </html>
        """)
        
app = webapp2.WSGIApplication([('/', MainPage),
                              ('/hello', Hello)],
                              debug=True)
Update you application configuration file app.yaml with the following code
application: my-sample-code
version: 1
runtime: python27
api_version: 1
threadsafe: yes

handlers:
- url: /.*
  script: helloworld.app
  
libraries:
- name: webapp2
  version: "2.5.1"  
There is no need to Run anything again. Just go the browser and hit refresh and you should see the changes to your application.

Google App Engine Python Hello World webapp2
Google App Engine Python Hello World webapp2 response

7) Deploy your application to Google App Engine

Right click on the helloworld.py file -> Run As –> Run Configuration. Create a new PyDev Google App Run launch configuration and name it Deploy Python Hello World. Also specify the Main Module as appcfg.py

Google App Engine Eclipse Deploy configuration
In Arguments tab for the Program arguments specify update ${project_loc}/src. Click Run.

Google App Engine Eclipse Program Arguments
During the process it will ask you for your credentials to Google App Engine. Keep an eye on the console and enter your Email Address and password.
06:02 PM Host: appengine.google.com
06:02 PM Application: my-sample-code; version: 1
06:02 PM 
Starting update of app: my-sample-code, version: 1
06:02 PM Getting current resource limits.
Email: {enter email here}
Password for {email}@gmail.com: {enter password here}
06:02 PM Scanning files on local disk.
06:02 PM Cloning 2 application files.
06:02 PM Uploading 2 files and blobs.
06:02 PM Uploaded 2 files and blobs
06:02 PM Compilation starting.
06:02 PM Compilation completed.
06:02 PM Starting deployment.
06:03 PM Checking if deployment succeeded.
06:03 PM Will check again in 1 seconds.
06:03 PM Checking if deployment succeeded.
06:03 PM Will check again in 2 seconds.
06:03 PM Checking if deployment succeeded.
06:03 PM Will check again in 4 seconds.
06:03 PM Checking if deployment succeeded.
06:03 PM Deployment successful.
06:03 PM Checking if updated app version is serving.
06:03 PM Will check again in 1 seconds.
06:03 PM Checking if updated app version is serving.
06:03 PM Will check again in 2 seconds.
06:03 PM Checking if updated app version is serving.
06:03 PM Will check again in 4 seconds.
06:03 PM Checking if updated app version is serving.
06:03 PM Completed update of app: my-sample-code, version: 1
Your application is now deployed to http://{your_app_Id}.appspot.com

Google App Engine Python Webapp Deployed

Done

References

No comments:

Post a Comment

NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.