Created
July 4, 2017 09:39
-
-
Save saliksyed/7697b16618c44e694f1a3bb34b6951fb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2 | |
# -*- coding: utf-8 -*- | |
""" | |
Created on Thu Jun 29 11:26:56 2017 | |
@author: saliksyed | |
""" | |
import csv | |
import matplotlib.pyplot as plt | |
import datetime | |
import numpy as np | |
gdp_data = csv.reader(open("gdp.dat")) | |
gdp_by_country_code = {} | |
for row in gdp_data: | |
country = row[0] | |
country_code = row[1] | |
gdp_values = [] | |
for value in row[5:]: | |
# We need to do this because there is some missing data which is represented by "" | |
if value == "": | |
final_parsed_value = None | |
else: | |
final_parsed_value = float(value) | |
gdp_values.append(final_parsed_value) | |
country_data = {} | |
country_data["country"] = country | |
country_data["country_code"] = country_code | |
country_data["gdp_values"] = gdp_values | |
gdp_by_country_code[country_code] = country_data | |
population_data = csv.reader(open("population.dat","r")) | |
population_by_country_code = {} | |
for row in population_data: | |
country = row[0] | |
country_code = row[1] | |
population_data = [] | |
for value in row[5:]: | |
if value == "": | |
population_data.append(None) | |
else: | |
population_data.append(float(value)) | |
country_data = {} | |
country_data["country"] = country | |
country_data["country_code"] = country_code | |
country_data["population_values"] = population_data | |
population_by_country_code[country_code] = country_data | |
per_capita_gdp_by_country_code = {} | |
for country_code in population_by_country_code.keys(): | |
if country_code in population_by_country_code and country_code in gdp_by_country_code: | |
gdp = gdp_by_country_code[country_code]["gdp_values"] | |
population = population_by_country_code[country_code]["population_values"] | |
country = gdp_by_country_code[country_code]["country"] | |
gdp_per_capita = [] | |
for i in xrange(0, len(gdp)): | |
if gdp[i] and population[i]: | |
gdp_per_capita.append(gdp[i]/population[i]) | |
else: | |
gdp_per_capita.append(None) | |
country_data = {} | |
country_data["country"] = country | |
country_data["country_code"] = country_code | |
country_data["gdp_per_capita"] = gdp_per_capita | |
per_capita_gdp_by_country_code[country_code] = country_data | |
@app.route("/gdp/<country_code>") | |
def render_chart(country_code): | |
import StringIO | |
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas | |
line_data = data[country_code]["gdp_per_capita"] | |
country = data[country_code]["country"] | |
x = np.array([datetime.datetime(1960 + i, 1, 1) for i in range(len(line_data))]) | |
y = np.array(line_data) | |
plt.xlabel('Year') | |
plt.xlabel('GDP per Capita (USD$)') | |
plt.title(' GDP per Capita for ' + country) | |
plt.plot(x,y) | |
fig, ax = plt.subplots() | |
# Tweak spacing to prevent clipping of ylabel | |
fig.tight_layout() | |
canvas=FigureCanvas(fig) | |
png_output = StringIO.StringIO() | |
canvas.print_png(png_output) | |
response=make_response(png_output.getvalue()) | |
response.headers['Content-Type'] = 'image/png' | |
return response | |
if __name__ == "__main__": | |
app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment