Skip to content

Commit

Permalink
PCC28 asutosh97 (pybites#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
asutosh97 authored and pybites committed Jul 25, 2017
1 parent 3345cdf commit 3bd79e1
Show file tree
Hide file tree
Showing 5 changed files with 41,429 additions and 0 deletions.
Binary file added 28/asutosh97/.DS_Store
Binary file not shown.
45 changes: 45 additions & 0 deletions 28/asutosh97/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from bokeh.embed import components
from bokeh.plotting import figure
from flask import Flask, render_template, request
import pandas as pd
import asutosh

app = Flask(__name__)


@app.route('/', methods=['GET','POST'])
def index():

# Default values to take care of a GET request.
first_country = "India"
second_country = "Pakistan"
attribute = "per_capita_income"

if request.method == "POST":
first_country = request.form["first_country_name"]
second_country = request.form["second_country_name"]
attribute = request.form["attribute_name"]

# Create the plot
plot = asutosh.create_figure(first_country, second_country, attribute)

# get countries available in the dataset
country_names = asutosh.get_countries()

# attributes
attribute_names = ["population", "per_capita_income", "life_expectancy"]

# Embed plot into HTML via Flask Render
script, div = components(plot)
return render_template("index.html", script = script, div = div,
attribute_names = attribute_names, current_attribute_name = attribute,
country_names = country_names, first_country = first_country, second_country = second_country)

# With debug=True, Flask server will auto-reload
# when there are code changes

def main():
app.run(port=5000, debug=True)

if __name__ == '__main__':
main()
45 changes: 45 additions & 0 deletions 28/asutosh97/asutosh.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# get both the countries and attribute value from drop-down
import pandas as pd
from bokeh.plotting import figure, show, output_file

def read_data():
data = pd.read_csv('datasets/gapminder.csv')
data = data[(data.Year >= 1950)]
return data

def create_figure(first_country, second_country, attribute):
# read from csv
data = read_data()

# filter datasets according to country
first_country_data = data[(data.Country == first_country)]

second_country_data = data[(data.Country == second_country)]

# get required attribute out of filtered datasets of both countries and turn them to list
first_country_data_attribute = list(first_country_data[attribute])
second_country_data_attribute = list(second_country_data[attribute])
years = list(first_country_data["Year"])

# create a new plot
p = figure(title = (first_country + " Vs " + second_country + " in " + attribute), x_axis_label='Years', plot_width=1280, plot_height=720)

# plot for first-country
p.line(years, first_country_data_attribute, legend = first_country, line_color="blue", line_width=3)

# plot for second-country
p.line(years, second_country_data_attribute, legend = second_country, line_color="green", line_width=3)

return p

def get_countries():
# get countries column as a list
return sorted(list(set(read_data()['Country'])))

def main():
p = create_figure("India", "Pakistan", "income")
output_file('output_files/new.html')
show(p)

if __name__ == "__main__":
main()
Loading

0 comments on commit 3bd79e1

Please sign in to comment.