William Li: Can we eat to starve cancer?

Tuesday, May 18, 2010

Excellent talk about cancer-fighting foods. See also Food Prints notes for Tomatoes and Spinach.

Food and Addiction

Thursday, May 6, 2010

This week NPR had also an interesting story - Soda In America: Taxes And A Debate Over Health. Check out also this funny cartoon that I saw in Google Reader.

Web Services

Tuesday, May 4, 2010

There has been multiple requests to add web services to Food Prints, that's why I created RESTful Web Services to allow developers access to nutrition data stored in Google App Engine. The URL for this is http://food-prints.appspot.com/ws. You'll need to add query string (q) to this to get the results. For instance http://food-prints.appspot.com/ws?q=cheese returns this JSON result:
{
   "Cheese, brie":334.0,
   "Cheese, camembert":300.0,
   "Cheese, brick":371.0,
   "Cheese, cheddar":403.0,
   "Cheese, blue":353.0,
   "Cheese, colby":394.0,
   "Cheese, cottage, creamed, large or small curd":98.0,
   "Cheese, caraway":376.0,
   "Cheese, cheshire":387.0,
   "Cheese, cottage, creamed, with fruit":97.0
}
The strings here are first 10 food names found in the database, and values are calories per 100g of serving. You can get the next 10 results, if any, by passing offset parameter. For instance, http://food-prints.appspot.com/ws?q=cheese&offset=10 returns next 10 results. You can also get serving size for a given food in grams by sending w (for weight) parameter with the exact name of a food. For instance, http://food-prints.appspot.com/ws?w=Cheese, brie returns:
{
   "cubic inch":17.0,
   "cup: sliced":144.0,
   "cup: melted":240.0,
   "oz":28.35,
   "package (4.5 oz)":128.0
}
If you are calling this from javascript, you can also pass callback parameter that will be called when you get the data. I had to use JSONP for cross-site Callbacks to avoid same origin problem with XMLHttpRequest.
This is how I did this in server-side Python code:
def generate(self, object=None):
    callback = self.request.get('callback')
    if callback:
        self.response.out.write(callback + "("+ simplejson.dumps(object)+");")
    else:
        self.response.out.write(simplejson.dumps(object))
The following example shows a complete code that you can copy/paste inside html body tag.
<script type="text/javascript">
function handler(jsonData) { 
 var tableStr = "<table><thead><tr><th>Name</th><th>Quantity</th></tr></thead>";
 for (var food in jsonData) {
  tableStr +="<tr><td><a href='http://food-prints.appspot.com/facts/"+food+"'>"+food+"</a></td><td>"+jsonData[food]+"</td></tr>"; 
 }
 tableStr += "</table>";
 document.getElementById("result").innerHTML = tableStr;
}
var wsURL="http://food-prints.appspot.com/ws?callback=handler&q=";
var script = document.createElement("script");        
script.setAttribute("src",wsURL+"cheese");
document.body.appendChild(script);
function find(){
 document.getElementById("result").innerHTML = "Searching...";
 var script = document.createElement("script");        
 script.setAttribute("src",wsURL+escape(document.getElementById('query').value));
 document.body.appendChild(script);
}
</script>
<input type="text" id="query" value="cheese"/>
<input type="button" onclick="find()" value="Search" />
<div id="result"></div>
Here is how it renders




If you use this in your own app, please consider adding a link back to http://food-prints.appspot.com to acknowledge the source. Here is a sample image you can use:


Latest Feeds from Food Politics

Food Prints Blog Copyright © 2009 Designed by Ipietoon Blogger Template for Bie Blogger Template Vector by DaPino