Showing posts with label appengine. Show all posts
Showing posts with label appengine. Show all posts
Thursday, October 8, 2009
Add Google App engine Support in Netbeans
Add Google App engine Support in Netbeans is very easy. To install app engine plugin follow the below steps.
1. Go to Tools - Plugins and select Settings tab.
2. Click Add button and type 'App Engine' as name of Update center and following URL into URL text field. Click to OK button.
For NetBeans 6.7.1 use http://kenai.com/projects/nbappengine/downloads/download/1.0_NetBeans671/updates.xml
For NetBeans 6.8 use http://kenai.com/projects/nbappengine/downloads/download/Latest_NetBeans68/updates.xml
1. Switch to Available Plugins tab and choose Google App Engine modules (server, configuration, deployment, editor hints)
2. Click to Install button
After that follow below links steps
http://kenai.com/projects/nbappengine/pages/Home
Wednesday, December 17, 2008
Templates in google appengine
Externalizing html from core business logic is very good technique to create code simple and understandable. Embedded HTML in code is difficult to maintain. To Avoid embedded HTML in code python provides us different templating systems which are EZT, Cheetah, ClearSilver, Quixote, and Django , which are supported by google appengine. We use Django template engine in our example.
Create Model class with name message.py
create main.py
list.html
create app.yaml
What the above code does
Create Model class with name message.py
from google.appengine.ext import db class Message(db.Model): content = db.TextProperty(required=True) who = db.StringProperty() when = db.DateTimeProperty(auto_now_add=True) |
create main.py
| class MainHandler(webapp.RequestHandler): def get(self): messages = db.GqlQuery('SELECT * FROM Message ' 'ORDER BY when DESC').fetch(100) values = {'messages': messages} self.response.out.write( template.render('list.html', values)) def main(): application = webapp.WSGIApplication([('/', MainHandler)], debug=True) wsgiref.handlers.CGIHandler().run(application) if __name__ == '__main__': main() |
list.html
| <head><title>..............</title> <style> .even { background-color:#DDDDDD } .odd { background-color:#FFFFFF } </style> </head> <body> <table width="100%" cellspacing="0" cellpadding="0" > {% for message in messages %} <tr class="{% cycle even,odd %}" > <td colspan="3"> <table width='100%' border=1> <tbody> <tr class="{% cycle even,odd %}"> <td width="20%" valign="top">Nick</td> <td align="right" width="20%" valign="top">{{message.when}}</td> </tr> <tr height="40"> <td width="20%" valign="top">{{message.who}} </td> <td width="80%" valign="top">{{message.content}}</td> </tr> </tbody> </table> </td> </tr> <tr height="5px"><td colspan='3' align='center'> </td></tr> {% endfor %} </table> </body> </html> |
create app.yaml
| application: template version: 1 runtime: python api_version: 1 - url: /.* script: main.py |
What the above code does
- In meesage.py file we define a model Message with three properties: content,who and when which plays a role of domain object and a db.This is part of App Engine not a python and It uses App Engine big table infrastructure which takes care of all of the distribution, replication and load balancing.
for detail refrence of entities and model visit http://code.google.com/appengine/docs/datastore/ - In main.py we define MainHandler class which serve as request handler of all get requests.We use GQL in our code for getting all message model objects. After fetching all results we assign these meesage to values and reponse back to list.html page. self.response.out.write use for writing reponse and template.render method use for rendring list template.
- After that we create html file with the name of list.html .In this html file we use Django script for manupolation Message objects. {% for message in messages %} is just foreach like code structure and "{% cycle even,odd %}" uses for applying style class alternatively each time. you can use any number of values, separated by commas for alternatively.
Using google webapp framework in google appengine
Google includes python based web application framework with app engine.This framework pretty similar with MVC pattern.A
our app.yaml
http://code.google.com/appengine/docs/webapp/
What the above code does
webapp application has three parts:- one or more
RequestHandlerclasses that process requests and build responses - a
WSGIApplicationinstance that routes incoming requests to handlers based on the URL - a main routine that runs the
WSGIApplicationusing a CGI adaptor.
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 World from the webapp framework!')
application = webapp.WSGIApplication(
[('/', MainPage)],
debug=True)
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main() |
| application: webapptest version: 1 runtime: python api_version: 1 handlers: - url: / script: webapp.py |
What the above code does
- The
webappmodule is in thegoogle.appengine.ext - This code defines one request handler,
MainPage, mapped to the root URL (/).We can map diffrent urls with diffrent handlers - When
webappreceives an HTTP GET request to the URL/, it instantiates theMainPageclass and calls the instance'sgetmethod. Inside the method, information about the request is available usingself.request. Typically, the method sets properties onself.responseto prepare the response.
learning google appengine in 5 minutes
Creating an App Engine application is easy, and only takes a few minutes.
Creating a Simple Request Handler
Creating the Configuration File
Testing the Application

Reference
http://code.google.com/appengine/docs/gettingstarted/
First you need to download the following packages
Creating a Simple Request Handler
Create a directory named helloworld. All files for this application reside in this directory.Inside the helloworld directory, create a file named helloworld.py, and give it the following contents:
print 'Content-Type: text/plain' print '' print 'Hello, world!' |
This Python script responds to a request with an HTTP header that describes the
content, a blank line, and the message Hello, world!.
Creating the Configuration File
An App Engine application has a configuration file called app.yaml. Among other things, this file describes which handler scripts should be used for which URLs.
Inside the helloworld directory, create a file named app.yaml with the following contents:
application: helloworld version: 1 runtime: python api_version: 1 handlers: - url: /.* script: helloworld.py |
Testing the Application
| google_appengine/dev_appserver.py helloworld Type the following URL and enjoy! |

Reference
http://code.google.com/appengine/docs/gettingstarted/
Subscribe to:
Posts (Atom)