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:
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:
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:
Now, everything is ready:
Let’s Run it, and now open the browser:
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:
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.
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:
Let’s introduce a name, .. and now you have it!
And .. that’s all by now. I hope you enjoy with this little example of using webapp2 framework on GAE.
Have a nice day!!