def rendermap(): message = '' result = '' leaderitems = {} cc = '' logging.info("POST request path /rendermap") if request.method == 'POST': cc = request.form.get('countryCode') if cc == '': message = "Please enter a 2 letter ISO country code" cc = cc.lower() ccupper = cc.upper() logging.info("ISO country code entered:" + cc) #Load the country json with open('data/leaders.json') as leaders_json_file: leadersdata = json.load(leaders_json_file) #for w in leadersdata: # print("%s: %d" % (w, leadersdata[w])) if ccupper not in leadersdata: message = "Wrong country code. Click Home link and enter a valid 2-letter ISO code!" else: leaderitems[cc] = float(leadersdata[ccupper]) result = cc + ":" + str(leadersdata[ccupper]) with open('data/neighbours.json') as neighbours_json_file: nd = json.load(neighbours_json_file) #for n in nd: # print("%s: %s" % (n, str(nd[n]))) narr = nd[cc] for neighbour in narr: nupper = neighbour.upper() if nupper in leadersdata: leaderitems[neighbour] = float(leadersdata[nupper]) result += "," + str(neighbour) + ":" + str( leadersdata[nupper]) else: message += "Data not available for neighbouring country ISO Code:" + str( neighbour) + "\n" #Create world map with result wm_style = RotateStyle('#34126000') wm = World() wm.force_uri_protocol = 'http' wm.title = "Women Political leaders (%)" wm.add('', leaderitems) wm.render_to_file('templates/lmap.svg') svg = render_template('lmap.svg') print("Result " + result) print("Message " + message) print(len(leaderitems)) #svgf = open("templates/lmap.svg", "r").read() #svg_io = StringIO() #svg_io.write(svgf) #svg_io.seek(0) #return send_file(svg_io, mimetype='image/svg+xml') img = './static/lmap.svg' return render_template('leadersmap.html', message=message, img=img)
def GET(self): user_data = web.input() prd = user_data.product pg = user_data.data from pygal.maps.world import World wm = World(height=400) wm.force_uri_protocol = 'http' wm.title = "產品 " + prd + " 的 " + pg + " 在全球即時分佈的狀態" d = getlist(prd, pg) i = 0 for item in d: x = {} x[incl.countrycode[item[0]]] = item[1] wm.add(str(i + 1) + ". " + item[0], x) i = i + 1 #wm.render_to_file("static/map.svg") wm.disable_xml_declaration = True return '<html>\n <head>\n <title>全球即時分佈圖</title>\n<meta http-equiv="content-type" content="text/html;charset=utf-8"><meta http-equiv="refresh" content="30" />\n<script type="text/javascript" src="http://kozea.github.com/pygal.js/latest/pygal-tooltips.min.js"></script>\n</head>\n<body>\n' + wm.render( is_unicode=True) + '\n </body>\n</html>'
from pygal.maps.world import World wm = World() wm.force_uri_protocol = 'http' wm.title = 'Populations of Countries in North America' wm.add('North America', {'ca': 34126000, 'us': 309349000, 'mx': 113423000}) wm.render_to_file('na_populations.svg')
# json.load() converts the data into a format Python can work with pop_data = json.load(f) # build a dictionary of population data cc_populations = {} for pop_dict in pop_data: if pop_dict['Year'] == '2010': country_name = pop_dict['Country Name'] population = int(float(pop_dict['Value'])) code = get_country_code(country_name) if code: cc_populations[code] = population # group the countries into 3 population levels cc_pops_1, cc_pops_2, cc_pops_3 = {}, {}, {} for cc, pop in cc_populations.items(): if pop < 10000000: cc_pops_1[cc] = pop elif pop < 10000000: cc_pops_2[cc] = pop else: cc_pops_3[cc] = pop wm = World() wm.force_uri_protocol = 'http' wm.title = 'World Population in 2010, by Country' wm.add('0-10,', cc_pops_1) wm.add('10m-1bn', cc_pops_2) wm.add('>1bn', cc_pops_3) wm.render_to_file('world_population.svg')
from pygal.maps.world import World wm = World() wm.force_uri_protocol = "http" wm.title = "Populations of Countries in North America" wm.add("North America", {'ca': 34126000, 'us': 309349000, 'mx': 113423000}) wm.render_to_file('na_populations.svg')