Google App Engine with Python

Posted by in Python

A few months ago, I wrote about my experience on a Google Code Lab I was, and I could verify that … I need to work with Google App Engine. So, today’s entrance is the starting point for GAE (although you can read the theory of GAE here and here), where I code several examples of using webapp2, how to work with jinja2 templates, add the front-end using Twitter Bootstrap, and more!

Let’s start by ….

Trying Google App Engine for Python

Before installing, it is prerequisite to have Python 2.7 installed:

Prerequisitos GAE

Pre-requisites GAE

Next is accept the terms of use, select the path where the SDK will be installed, next, next, … and you’re done!

Instalando GAE

Installing GAE

 

Let’s try Google App Engine with Python

Before to begin GAE, it could be interesting to know what libraries are included and I could use. You can discovered them here: https://developers.google.com/appengine/docs/python/tools/libraries27. As you can see, the selections is really interesting because you can use NumPy, MatPlotLib, MySQLdb, pyCripto, yaml, lxml, django, … a lot of tools to have fun!

Let’s launch GAE:

GAE

GAE

Our first try is create a new app, be going to File / Add New Application, and complete the data, as the app name, the path where it will be stored, the Python engine and the ports to use:

Nueva aplicación

New application

Now, everything is ready:

Aplicación Hola

Hello world

Let’s Run it, and now open the browser:

Aplicación Hola corriendo

Hello world app

And .. that’s all … by now!.

The app files

We haven’t done anything, and the “hello world” app is running! Let’s verify what’s on the path, the files created:

Ficheros de la aplicación

App files

Basically, there is a favicon, twoyaml files, and a python file (and compiled .pyc).


import webapp2

class MainHandler(webapp2.RequestHandler):
def get(self):
self.response.write('Hello world!')

app = webapp2.WSGIApplication([
('/', MainHandler)
], debug=True)

As you can see, the example app use the webapp2 framework, of course in Python.

NOTE: I haven’t upload any code because I think it’s not neccesary. Anyway, you can see the app running here.

Learning webApp2

Until here, it has been easy … but, let’s try a new example, modifying the code and let’s create an html variable where you can store a form (including a submit button) using HTML. When using the post method, the executed rutine will be MainHandler.post and it will show the message.

import webapp2

root = '/'
html = """
<html lang="es">
    <head><title>Probando Google App Engine for Python</title></head>
    <body>
        <h1>Probando Google App Engine for Python</h1>
        <p>Cargado %d veces</p>
        <form method="post">
            <label for="nombre">Nombre</label>
            <input name="nombre" id="nombre" type="text" value="" />
            <input type="submit" value="Enviar">
        </form>
    </body>
</html>
"""

class MainHandler(webapp2.RequestHandler):

    def post(self):
        nombre = self.request.get("nombre")
        if len(nombre) == 0:
            self.redirect(root)
        mensaje = ('Hola ... %s' % (nombre))
        self.response.out.write(mensaje)

    def get(self):
        self.response.write(html)

app = webapp2.WSGIApplication([(root, MainHandler)], debug=True)</pre>

And the result is:

Probando webApp2 en GAE

Trying webApp2 on GAE

Let’s introduce a name, .. and now you have it!

Probando webApp2 en GAE

Trying webApp2 on GAE

And .. that’s all by now. I hope you enjoy with this little example of using  webapp2 framework on GAE.

Have a nice day!!