Sunday, January 15, 2012

Creating Python Google App with Aptana and Pydev

This document is focus on setup a development environment and start a web application based on Google App Engine (GAE) on Python SDK using Aptana IDE.


Google App Engine currently support create applications on three different languages: Java, GO and Python.  Many developers like to work their projects with a good text editor (Gedit, Notepad++, JEdit, TextMate, MCedit, Vim, etc.) but others go for a sophisticated IDE (Integrated Development Environment). Currently Google  gives a plugin for develop Java applications on Eclipse IDE, but not for Python and GO. Lets focus on start a whole project based on Python.

First than all let's start by setting the all environment by downloading the Python SDK from code.google.com. For Windows just install the ".msi" executable and for Linux just extract the ".zip" in your "home" folder. Don't forget to install Python on Windows.

Now let's go to get the Aptana IDE at aptana.com. For Windows just execute the installer and for Linux unpack the ".zip" file and make sure of install Sun Java JDK or JRE (Please not Open JDK). Aptana comes with PHP, HTML, CSS, Javascript, YAML, SQL and more editors. The most I like is the Django Template editor, but we'll get there. Aptana comes by default with a Git client but if you work with SVN take a look at plugin Subclipse.

Aptana is based on the Eclipse framework and therefore is possible to install many Eclipse plugins on it. The plugin that allow coding Python on Aptana (and Eclipse) is Pydev. Once you open Aptana for the first time select the Pydev perspective.



Let's specify the version of Python to Pydev in Aptana. Select "Window -> Preferences -> Pydev -> Intrepeter - Python". Click on the "Auto Config" button or select the path of your Python interpreter on the "New ..." button.


Lets start by creating a new Pydev Google App Engine Project. Select "File -> New -> Project -> Pydev -> Pydev Google App Engine Project".


Make sure to select the correct version of Python for the project and the path for the Google SDK. Look at the images below.







Now we have the development environment complete. I will assume that you have created an application on https://appengine.google.com/. For the next example I will use my own app called djangoabdel (I called like this because I like Django a lot!)


When I started reading the documentation of GAE it was evident that there is a lot of freedom for create your own framework. So I will show my way but you can innovate or use any pure Python framework (Django?). Lets take a look at this image:


Here I have:
  • main.py - This is the start point of the whole application.
  • appengine_config.py - At the top level of your project is automatically imported bygoogle.appengine.ext.webapp.util.run_wsgi_app() to add middlewear to webapp applications. This is how I specify Django version (1.0, 1.1 or 1.2). Take a look at.
  •  app.yaml - The heart of the app configuration.
  • templates - folder that will contain static files like images, css files, javascript files, etc. This is the UI of your application.
  • controllers - python package that contains all the controllers or business scripts of our application.
  • models - python package that contains all the models or  Entities of our application.
  • libs - python package that contains all the third party libraries.
I will create a post for explain more detailed the different parts.

One thing that happened to me while creating this way of development was a problem trying to import the controllers, libs, models to the main.py file. The solution is pretty simple, just add this to the __init__.py file in the package folder:


import os, sys

path = os.path.abspath(".")

sys.path.append(os.path.join(path, __name__))



Even if sometimes you have trouble trying to import a package just restart the test server. Just keep this in mind (Oh, you will!).

Now lets create a "Run" configuration so we can run our development server. Go for "Run -> Run Configurations...". Here double click on "Pydev Google App Run" and set a name to this configuration and the at "Main Module" point to "dev_appserver.py" file. This file is in the SDK installation files.


The last of the configuration is click on the "Arguments" tab and on "Program Arguments" add the path to your application folder.


Now click on "Apply" button and then on "Run" button for run the app. Now all the messages of the application will appear on the 'Console" tab. Here you can stop the development server too.


Now that the "Run" configuration is created, the "Debug" configuration is created automatically. Take a look at the short-cut buttons. 


Just remember that for debugging set your break points.


While you will be coding you'll enjoy the auto-complete behaviour of Pydev (Ctrl + Space Bar). But the most I like is the Django Template Editor for the GAE template files. When opening a html file from the "Pydev Package Explorer" do a right click "Open with ->" and select "HTML.Templates Django Editor (Aptana)". GAE takes advantage of the Django template tags and filters, so lets  do the same on this editor.

The last part is upload our app to Google so that's easy as do a right click on our project "Pydev: Google App Engine -> Upload".



Thank you for your time.

4 comments:

  1. This was the best description of setting up Aptana for App Engine development that I've found, but I have one question: I don't get a "Pydev: Google App Engine -> Upload" (or in fact any "Pydev: Google App Engine" options). Any idea what I'm missing?

    ReplyDelete
    Replies
    1. Hi Derek!
      I am glad that I have helped someone with this small tutorial. First than all the project must be created as a Pydev Google App Engine project. Take a closer look at image #4 where the project is created (File -> New -> Project -> Pydev -> Pydev Google App Engine Project). After this when the project is created just right click on the project folder in the view "Pydev Package Explorer"(penultimate image).

      Delete
  2. Hello Abdel:

    Thanks for the tutorial.
    Issuue#1:
    I was able to create a "hello webapp world" sample for Google App engine, and ran it with no errors with dev_appserver.py, but it does not stop at the breakpoint.

    I set the break point in the class method like so:

    class MainPage(webapp.RequestHandler):

    def get(self):
    self.response.headers['Content-Type'] = 'text/plain'
    self.response.out.write('Hello, webapp World !') # <<== Break point is here.


    I was able to see the response using localhost:8080 on Firefox.
    So the sample code is working, but breakpoint is not working.


    Issue#2:
    I did not see ay short cuts for displaying different views like package Explorer.

    I have the latest Aptana studio (3.4) download on for Linux.
    Please advice.
    thanks.

    ReplyDelete