4
votes

What are the options for accessing information from Google App Engine datastore in a javascript file?

BACKGROUND:

I am running an app on Google App Engine which uses the Google Chart Tools API. All the data which I would like to graph is in my Google App Engine datastore.

WHAT I'VE TRIED

I can access the data within my python script. Also, in the python script, I encode the data to JSON. My idea was to save this in a js file, which I could import into the HTML, then build the chart there. However, this does not work, because writing to local files is not supported in App Engine.

3
Is the channel api not an option/applicable? my first thought: send the data to memcache and with a channel and a python handler send the json to the client side JS...but that's just a first thought mind you. - user1888498
It seems that the channel api is an option, but it might be overkill for what I am doing. The channel api creates a persistent connection for updating information immediately, where as my application will not need to send users such updates. - Kassy Coan

3 Answers

1
votes

On App Engine you typically generate an HTML page that you serve to a client. You don't need to save your JSON to a js file, you can embed it directly as text into your HTML page that you serve to your client.

1
votes

You can use something like Jersey to create a REST api for the backend. It's a bit of work. You can also make a GWT application with the appengine project, which has really extremely easy backend hookups. Then you can interface this with your javascript application or have hooks that your javascript application can use.

1
votes

XMLHttpRequest

From the python file: Write the JSON data to a URL. In App Engine, this is done with

self.response.write(data)

From the Javascript, make the XHR request like this:

function getData() {
    var dataObject = new XMLHttpRequest();
    dataObject.open('GET', 'URL', true);
    dataObject.send();
    return dataObject;
}

This will return a XMLHttpRequest object with the data in a string in the response or responseText attribute.