Home
Query Access
JavaScript API
Hackathon on the Earth Policy Institute Data
hosted by SDQL.com

The Application Programming Interface for Requesting Earth Policy Institute Data as JavaScript Objects
-- by Joe Meyer

This API describes a thin wrapper on this Pythonish Query Language (PyQL) and allows users to turn PyQL queries into JavaScript objects.

To learn more about the PyQL see the manual at http://SportsDataBase.com/sdb.py/Manual/toc and join the discussion group at http://groups.google.com/group/SportsDataBase.

An http get request to the api server contains a PyQL query and returns a JavaScript file containing the query results in an object named PyQL with the following attributes:

  • pyql - this is just the PyQL query text of your request.
  • headers - a list containing the names of the data fields you requested.
  • results - an object with a key for each query group and values given by the requested fields. Note that the values will be a single items in the case of requested summatives and otherwise they will be lists of values

Learning is not by example, learning is example. -- A. Einstein

Example 1: Request and display a single value.

To request the population of China in 2007 use the PyQL:

population@region='China' and year=2007 and region type='Country'

to form this url (using, for example, javascript's built in `escape` method):

to receive this JavaScript file:

PyQL = {};
PyQL.pyql = "population@region='China' and year=2007 and region type='Country'";
PyQL.headers = ['population'];
PyQL.results = {};
PyQL.results["region='China' and year=2007 and region type='Country'"] = [
		[1329]
		];


which one could access and display with:

<script type='text/javascript' src=http://epi.sdql.com/PyQL.js?pyql=population%40region%3D%27China%27%20and%20year%3D2007%20and%20region%20type%3D%27Country%27>

<script>

population = pyql.results["region='China' and year=2007 and region type='Country'"][0];
document.write("The population of China in 2005 was " + population + " million.");

</script>

See if this example works.

This JavaScript file may seem verbose for requesting just one number. However, this general form covers multiple field requests and PyQL's powerful group-by methods.

Example 2: Request the year, population and region for all countries with population data. This would be a good example for line graphs.

To request the year, population and region for all countries with population data use the PyQL:

year,population,region@region type='Country' and population is not None

to form this url (using, for example, javascript's built in `escape` method):

to receive this JavaScript file:

PyQL = {};
PyQL.pyql = "year,population,region@region type='Country' and population is not None";
PyQL.headers = ['year', 'population', 'region'];
PyQL.results = {};
PyQL.results["region type='Country' and population is not None"] = [
		[2007,2007,2006,2006,2005,2005,2004,2004,2003,2003,2002,2002,2001,2001,2000,2000,1999,1999,1998,1998,1997,1997,1996,1996,1995,1995,1994,1994,1993,1993,1992,1992,1991,1991,1990,1990,1989,1989,1988,1988,1987,1987,1986,1986,1985,1985,1984,1984,1983,1983,1982,1982,1981,1981,1980,1980,1979,1979,1978,1978,1977,1977,1976,1976,1975,1975,1974,1974,1973,1973,1972,1972,1971,1971,1970,1970,1969,1969,1968,1968,1967,1967,1966,1966,1965,1965,1964,1964,1963,1963,1962,1962,1961,1961],
		[1329,148,144,1321,1312,141,1304,138,1295,134,1286,131,1277,128,1267,125,1257,122,1246,119,116,1235,1223,113,1211,110,1199,108,1186,105,1172,102,1158,100,1142,97,1125,95,1107,92,1089,90,1070,87,85,1053,83,1037,1022,81,79,1008,994,77,981,75,968,72,955,70,941,68,927,66,911,64,894,62,876,61,59,856,836,58,816,56,795,55,775,54,53,754,735,52,716,50,700,49,48,684,670,47,658,46],
		['China','Nigeria','Nigeria','China','China','Nigeria','China','Nigeria','China','Nigeria','China','Nigeria','China','Nigeria','China','Nigeria','China','Nigeria','China','Nigeria','Nigeria','China','China','Nigeria','China','Nigeria','China','Nigeria','China','Nigeria','China','Nigeria','China','Nigeria','China','Nigeria','China','Nigeria','China','Nigeria','China','Nigeria','China','Nigeria','Nigeria','China','Nigeria','China','China','Nigeria','Nigeria','China','China','Nigeria','China','Nigeria','China','Nigeria','China','Nigeria','China','Nigeria','China','Nigeria','China','Nigeria','China','Nigeria','China','Nigeria','Nigeria','China','China','Nigeria','China','Nigeria','China','Nigeria','China','Nigeria','Nigeria','China','China','Nigeria','China','Nigeria','China','Nigeria','Nigeria','China','China','Nigeria','China','Nigeria']
		];


which one could access and display with:

<script type='text/javascript' src=http://epi.sdql.com/PyQL.js?pyql=year%2Cpopulation%2Cregion%40region%20type%3D%27Country%27%20and%20population%20is%20not%20None>



<script>

for (key in PyQL.results)
{
  document.write("<table border=2><caption>"+key+"</caption><tr>")
  for (h = 0; h < PyQL.headers.length; h++)
  {
    document.write("<th>"+PyQL.headers[h]+"</th>");
  }
  result = PyQL.results[key];
  for (i=0; i<result[0].length; i++)
  {
    document.write("<tr>");
    for (h = 0; h < PyQL.headers.length; h++)
    {
      document.write("<td>"+result[h][i]+"</td>");
    }
    document.write("</tr>");
  }
  document.write("</tr></table>");
}



</script>

See if this example works.

Example 3: Request the year, cigarettes per person and region for all countries with available data. This would be another good example for line graphs.

To request the year, cigarettes per person and region for all countries with cigarettes per person data use the PyQL:

year,cigarettes per person,region@region type='Country' and cigarettes per person is not None

to form this url (using, for example, javascript's built in `escape` method):

to receive this JavaScript file:

PyQL = {};
PyQL.pyql = "year,cigarettes per person,region@region type='Country' and cigarettes per person is not None";
PyQL.headers = ['year', 'cigarettes per person', 'region'];
PyQL.results = {};
PyQL.results["region type='Country' and cigarettes per person is not None"] = [
		[2003,2003,2003,2003,2003,2003,2002,2002,2002,2002,2002,2002,2001,2001,2001,2001,2001,2001,2000,2000,2000,2000,2000,2000,1999,1999,1999,1999,1999,1999,1998,1998,1998,1998,1998,1998,1997,1997,1997,1997,1997,1997,1996,1996,1996,1996,1996,1996,1995,1995,1995,1995,1995,1995,1994,1994,1994,1994,1994,1994,1993,1993,1993,1993,1993,1993,1992,1992,1992,1992,1992,1992,1991,1991,1991,1991,1991,1991,1990,1990,1990,1990,1990,1990,1989,1989,1989,1989,1989,1989,1988,1988,1988,1988,1988,1988,1987,1987,1987,1987,1987,1987,1986,1986,1986,1986,1986,1986,1985,1985,1985,1985,1985,1985,1984,1984,1984,1984,1984,1984,1983,1983,1983,1983,1983,1983,1982,1982,1982,1982,1982,1982,1981,1981,1981,1981,1981,1981,1980,1980,1980,1980,1980,1980,1979,1979,1979,1979,1979,1979,1978,1978,1978,1978,1978,1978,1977,1977,1977,1977,1977,1977,1976,1976,1976,1976,1976,1976,1975,1975,1975,1975,1975,1975,1974,1974,1974,1974,1974,1974,1973,1973,1973,1973,1973,1973,1972,1972,1972,1972,1972,1972,1971,1971,1971,1971,1971,1971,1970,1970,1970,1970,1970,1970,1969,1969,1969,1969,1969,1969,1968,1968,1968,1968,1968,1968,1967,1967,1967,1967,1967,1967,1966,1966,1966,1966,1966,1966,1965,1965,1965,1965,1965,1965,1964,1964,1964,1964,1964,1964,1963,1963,1963,1963,1963,1963,1962,1962,1962,1962,1962,1962,1961,1961,1961,1961,1961,1961,1960,1960,1960,1960,1960,1960],
		[1330,2247,1545,1249,1722,1338,1321,1386,2295,1609,1737,1287,1324,2568,1803,1626,1408,1342,1334,1637,2583,1397,1830,1431,1774,1671,1422,2627,1321,1431,1758,1712,2668,1431,1341,1423,1858,1677,1421,2540,1353,1515,1920,1361,1551,1542,1482,2682,1859,1577,1942,2535,1396,1526,1385,1927,1646,1564,2573,1847,1849,1389,1633,1801,1654,2674,1689,2021,2744,1898,1387,1555,2036,2684,1378,1796,1676,1711,2165,2034,1729,1697,1440,2561,2146,2509,1941,1426,1691,1579,1410,2325,2500,1665,1627,1987,1336,2402,1695,1688,1999,2538,1710,2555,2433,1209,1983,1711,2522,2010,1750,2589,1730,1117,2571,1022,1740,2608,1766,2005,944,1951,1769,2570,1646,2621,2657,2659,1832,931,1624,1844,2828,1984,2656,2090,867,1617,2764,2188,2652,1633,771,2036,2240,2759,1701,2719,670,2002,2692,2773,1658,617,1954,2374,2696,2304,2723,642,1813,1603,2622,2872,1989,529,2390,1666,1946,545,2759,2422,1680,2647,2723,1947,489,2511,2717,1591,1900,2816,485,1422,2518,2521,2662,2435,1430,1904,2386,438,422,2593,1897,2210,2244,1400,2662,484,2162,2350,1401,1773,429,2076,2583,1415,2306,1706,2708,1974,1618,339,1262,2250,2734,1924,1577,1255,328,2211,370,1808,2722,2275,1174,1605,2705,1138,336,1790,2163,1543,1468,296,2644,1044,1675,2196,2746,1098,1561,1396,237,2199,1086,2110,1540,2705,182,1381,2719,1319,193,1420,2194,1046,2155,2657,347,1329,1235,1003],
		['China','Japan','United States','United Kingdom','Germany','France','China','France','Japan','United States','Germany','United Kingdom','China','Japan','Germany','United States','France','United Kingdom','China','United States','Japan','France','Germany','United Kingdom','Germany','United States','France','Japan','China','United Kingdom','United States','Germany','Japan','France','China','United Kingdom','United States','Germany','France','Japan','China','United Kingdom','United States','China','Germany','United Kingdom','France','Japan','Germany','United Kingdom','United States','Japan','China','France','China','United States','United Kingdom','France','Japan','Germany','United States','China','France','Germany','United Kingdom','Japan','France','United States','Japan','Germany','China','United Kingdom','United States','Japan','China','Germany','United Kingdom','France','United States','Germany','United Kingdom','France','China','Japan','United States','Japan','Germany','China','France','United Kingdom','China','United States','Japan','France','United Kingdom','Germany','China','United States','France','United Kingdom','Germany','Japan','United Kingdom','Japan','United States','China','Germany','France','United States','Germany','France','Japan','United Kingdom','China','United States','China','France','Japan','United Kingdom','Germany','China','Germany','United Kingdom','United States','France','Japan','United States','Japan','United Kingdom','China','France','Germany','United States','United Kingdom','Japan','Germany','China','France','United States','United Kingdom','Japan','France','China','Germany','United Kingdom','United States','France','Japan','China','Germany','Japan','United States','France','China','Germany','United Kingdom','United States','United Kingdom','Japan','China','Germany','France','Japan','United States','Germany','China','United Kingdom','France','Germany','China','United States','United Kingdom','France','Japan','United States','Germany','China','United Kingdom','Japan','France','Germany','United States','China','France','Japan','United Kingdom','United States','Japan','France','Germany','United Kingdom','China','China','United States','Germany','Japan','United Kingdom','France','United States','China','Japan','United Kingdom','France','Germany','China','Japan','United States','France','United Kingdom','Germany','United States','Japan','Germany','China','France','United Kingdom','United States','Japan','Germany','France','China','United Kingdom','China','Japan','United States','United Kingdom','France','Germany','United States','France','China','Japan','United Kingdom','Germany','Germany','China','United States','France','Japan','United Kingdom','United States','France','Japan','Germany','China','United Kingdom','France','United Kingdom','Japan','United States','China','Germany','United States','Germany','China','Japan','United Kingdom','France','United Kingdom','United States','China','Japan','Germany','France']
		];


which one could access and display with:

<script type='text/javascript' src=http://epi.sdql.com/PyQL.js?pyql=year%2Ccigarettes%20per%20person%2Cregion%40region%20type%3D%27Country%27%20and%20cigarettes%20per%20person%20is%20not%20None>



<script>

for (key in PyQL.results)
{
  document.write("<table border=2><caption>"+key+"</caption><tr>")
  for (h = 0; h < PyQL.headers.length; h++)
  {
    document.write("<th>"+PyQL.headers[h]+"</th>");
  }
  result = PyQL.results[key];
  for (i=0; i<result[0].length; i++)
  {
    document.write("<tr>");
    for (h = 0; h < PyQL.headers.length; h++)
    {
      document.write("<td>"+result[h][i]+"</td>");
    }
    document.write("</tr>");
  }
  document.write("</tr></table>");
}



</script>

See if this example works.