AJAX with Google App Engine
June 21th, 2009 - Programming
In this tutorial, I'll be showing you how to make a functional AJAX application on Google App Engine. There are two ways to do this, the first being to use a framework such as JQuery, the second being to write the javascript by hand. I will be showing you how to do both, and in such a way that a beginner can make sense of it easily, while more advanced users of App Engine can also benefit.
So I'm going to split the tutorial up into two parts, and each part will have a separate example for you to work with. Here are the bookmarks to each section, and you can decided which way you want to do this. Personally, I find JQuery easier, but it will require that you learn some new things if you are only familiar with Javascript.
JQuery for AJAXAJAX with regular Javascript
AJAX with JQuery and App Engine
For the JQuery part, take a look at this message generator (which has actually become quite popular). This is what we will be making if you are following along, so make sure that you take a look at it before we get started.
I am going to assume that you have already set up an App Engine Application, if not, you can check out this tutorial on Getting Started With Web Application Development, which explains exactly how you can do this. Then we have to create the page on which the AJAX will function, I have called this "magic.html", you call it whatever you want. So let's set up the basic format (I'll try to use exactly what I have made for the message generator application, which actually validates on w3 validator). The first part before the line break just includes the obvious html set-up, like the title of the page, and description, and the second section is the style. This is up to you to decided what is appropriate for your application.
-
Okay, so this is what the page looks like. You'll notice that I haven't closed the page, because we still have to add the JQuery. Let me explain what's going on here: the "messages div" is where the messages are going to pop up, and it seperated into "magicresponse", which is the actual message, and "credit", which is the div for the author's name. The "add" div is there for the form to add. You'll see that this isn't really a form as such, because we are going to be using AJAX to execute all of our submit functionality.
First we need to load the JQuery into the page:
Now let's use it for something:
Okay, now we've got all the code that will go into our html page. Save this page, with all the code as "magic.html" Now I'm going to write some Python:
" return new_text class Magician(BaseRequestHandler): def get(self): response = "welcome to the magic box app!" values = {"response": response} self.generate("magic.html", values) class Magic (BaseRequestHandler): def post(self): text = self.request.get("text") self.response.out.write(self.wizard(text)) application = webapp.WSGIApplication([('/index', Magician), ('/magic', Magic)]) def main(): run_wsgi_app(application) if __name__ == "__main__": main()
Well, if you run this code on your App Engine url or localhost, you should have a pretty neat AJAX application! I hope this tutorial proves useful for those who are in the intermediary stages of using Google App Engine - it is not another 'Get Started with App Engine' post, because there are already a million of these online for beginners.
Sincerely,
PyThoughts


6 Comments