As a final touch we use the Google Query Language to retrieve all datasets (Dataset.gql("")) and print a list of links to visualisation pages for each of them.
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
import math
import cgi
def generate_menu(out):
out.write("<h2>Menu</h2>\n<p>You can go to:</p>")
for page in ["upload","visualise"]:
out.write("<a href='"+page+"'>"+page+"</a><br/>")
class Dataset(db.Model):
descrip = db.StringProperty(multiline=True)
content = db.StringListProperty()
date = db.DateTimeProperty(auto_now_add=True)
class MainPage(webapp.RequestHandler):
def get(self):
self.response.out.write('<h1>Welcome to the BESS cloud tutorial!</h1>')
out = self.response.out
generate_menu(out)
class UploadPage(webapp.RequestHandler):
def get(self):
out = self.response.out
out.write('<h1>Welcome to the BESS upload page!</h1>')
out.write("""<form action='/completeupload' method='post'>
<div><b>Name of dataset:</b><br/>
<input type='text' name='dataset_name' maxlength='120' size='15'></div>
<div><b>Description:</b><br/>
<textarea name='dataset_descrip' rows='3' cols='60'></textarea></div>
<div><b>Content of dataset:</b> (format one 'XX;number' per line,
with 'XX' 2 letter country code)<br/>
<textarea name='dataset_content' rows='10' cols='60'></textarea></div>
<div><input type='submit' value='Upload Dataset'></div>
</form>""")
generate_menu(out)
class CompleteUploadPage(webapp.RequestHandler):
def post(self):
dataset_name = self.request.get('dataset_name')
dataset = Dataset(key_name=dataset_name)
dataset.content = self.request.get('dataset_content').split()
dataset.descrip = self.request.get('dataset_descrip')
dataset.put()
out = self.response.out
out.write('<h1>Upload completed</h1>')
out.write("<p>The dataset <b>"+dataset_name + "</b> was uplodaded successfully.</p>")
generate_menu(out)
class VisualisePage(webapp.RequestHandler):
def get(self):
out = self.response.out
self.response.out.write('<h1>Welcome to the BESS visualisation page!</h1>')
dataset_name = self.request.get('dataset')
if dataset_name!="":
out.write("<h2>"+dataset_name+"</h2>")
dataset = Dataset.get_by_key_name(dataset_name)
out.write("<p><b>Description:</b> %s</p>"% dataset.descrip)
countries = []
values = []
for pair in dataset.content:
(country,value) = tuple(pair.split(";"))
countries += [country]
values += [math.log(float(value))]
#values += [float(value)]
maxVal = max(values)
minVal = min(values)
values = [ "%.0f"%((value-minVal)*100/(maxVal-minVal)) for value in values ] # scaling to 0-100
countryString = "|".join(countries)
valueString = ",".join(values)
mapurl =\
"https://chart.googleapis.com/chart?"\
+"cht=map:fixed=-60,-180,75,180&chs=565x300&chf=bg,s,D0E0FF"\
+"&chco=f9f9f9,2020ff,00e000,fafa00,ff0000"\
+"&chld="+countryString\
+"&chd=t:"+valueString
out.write("<img src='%s'/>"%mapurl)
out.write("<h2>Available dataset</h2>")
out.write("<table border='1px'><tr><td><b>name</b></td><td><b>size</b></td><td><b>added</b></td>")
for dataset in Dataset.gql(""):
out.write("<tr><td><a href='/visualise?dataset=%s'>%s</a></td><td>%i</td><td>%s</td></tr>" %
(cgi.escape(dataset.key().name()),
dataset.key().name(),
len(dataset.content),
dataset.date.strftime("%d %b %Y %H:%M")
))
out.write("</table>")
generate_menu(out)
application = webapp.WSGIApplication(
[('/' , MainPage ),
('/upload', UploadPage),
('/completeupload' , CompleteUploadPage ),
('/visualise' , VisualisePage )
],
debug=True)
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()
back