Showing posts with label google app engine. Show all posts
Showing posts with label google app engine. Show all posts

Wednesday, April 29, 2009

Google App Engine & Eclipse

I am going to show you how to run your Google App (Python) in eclipse without any special plugins!

All you need to do is ensure that you have downloaded and installed both the Google App Engine and Python.

The trick to getting Google apps to run within eclipse is to run your app as an 'External Tool'.

From the Eclipse file menu, click on
Run -> External Tools -> External Tools Configuration ...

In the resulting window, choose to create a new program:



Give it a name (I called it 'App Engine') in the Name field. In the Location field, enter the exact path to your Python executable. For the Working Directory, enter the exact path to the Google App Engine.

The Arguments field is the most interesting one! Here we enter:

dev_appserver.py -d ${resource_loc}

The dev_appserver.py is the Python script that Google tells you to run when you want to test your app.

The -d just tells the App Engine to output debug statements. Feel free to use any command line arguments that the App Engine allows!

Finally, the ${resource_loc} tells Eclipse to use the full path of any item currently selected in the Navigator.


The final thing to do is to click on the Common tab and to Select "Display in favorites menu". This will provide you with a shortcut so that running your Google App requires the least amount of clicks!


Set up is complete!

Now choose the root folder of your Google App in Eclipse and click on 'App Engine' (or the name that you named your External Tool).



As you can see, the App Engine is running!


Some Notes:
  • If the App engine looks like it has hung (froze), click in the Eclipse console and hit the return key on your keyboard. What may have happened is that the App Engine has prompted you to check for updates and Eclipse didn't show you the message.

  • If you get an Eclipse error, "Problem Occurred", make sure that the root folder for your app is selected in Eclipse (i.e. click on it!).
If you have any other things to add, please post your comments!

Wednesday, August 27, 2008

Freebase & Google App Engine - Write Queries

If you would like to use Freebase with your application that is hosted with Google App Engine, then you may have run into some difficulties trying to get write queries to work!

Now that I have been able to get writes to work, I am going to write down what it is that I did so that other people don't run into the same problems that I did!

Basically, to get writes to work, I created a proxy between my app engine page and freebase. In other words, I had a form that allows you to modify an entity in freebase and upon submit, the mql write statement was created and sent to the app engine. The app engine then forwarded this request to Freebase and then waited for the response. The response was then forwarded back to the to the client page.

To get this to work, you need to first obtain the cookies that are set when you authenticate with Freebase. To do this, we have a simple python subroutine:

# domain is either sandbox.freebase.com or www.freebase.com
# user is the username
# pass is the password
def login(domain, user, pass):
result = urlfetch.fetch(
url='http://'
+ domain
+ '/api/account/login?username='
+ user
+ '&password='
+ pass
+ '&rememberme=true',
payload={},
method=urlfetch.POST,
headers={
'X-Metaweb-Request':'x-metaweb-request'
}
)

return result.headers['set-cookie']

This subroutine basically calls the login service for Freebase and returns any cookies that their server says to save.

The next thing that you need to do is to forward your write mql to Freebase. This can be done in a similar manner:

# domain is either sandbox.freebase.com or www.freebase.com
# user is the username performing the write
# pass is the password for the user performing the write
# mql is the write query wrapped in the query envelope
def write_to_freebase(domain, user, pass, mql):
form_fields = {
"queries":mql,
}
form_data = urllib.urlencode(form_fields)
cookies = login(domain, user, pass)
result = urlfetch.fetch(
url='http://' + domain+ '/api/service/mqlwrite',
payload=form_data,
method=urlfetch.POST,
headers={
'Content-Type': 'application/x-www-form-urlencoded',
'X-Metaweb-Request':'x-metaweb-request',
'Cookie' : cookies
}
)
return result.content;

This method simply calls the mqlwrite service and forwards the JSON object to you.

The imports required are:


from google.appengine.api import urlfetch
import urllib

That is all that is required in order to get writes to work with google app engine!

Please let me know if I missed anything so that I can update this post!

Thanks and good luck!