Skip to content

Commit 3bd79e1

Browse files
asutosh97pybites
authored andcommitted
PCC28 asutosh97 (pybites#75)
1 parent 3345cdf commit 3bd79e1

File tree

5 files changed

+41429
-0
lines changed

5 files changed

+41429
-0
lines changed

28/asutosh97/.DS_Store

6 KB
Binary file not shown.

28/asutosh97/app.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from bokeh.embed import components
2+
from bokeh.plotting import figure
3+
from flask import Flask, render_template, request
4+
import pandas as pd
5+
import asutosh
6+
7+
app = Flask(__name__)
8+
9+
10+
@app.route('/', methods=['GET','POST'])
11+
def index():
12+
13+
# Default values to take care of a GET request.
14+
first_country = "India"
15+
second_country = "Pakistan"
16+
attribute = "per_capita_income"
17+
18+
if request.method == "POST":
19+
first_country = request.form["first_country_name"]
20+
second_country = request.form["second_country_name"]
21+
attribute = request.form["attribute_name"]
22+
23+
# Create the plot
24+
plot = asutosh.create_figure(first_country, second_country, attribute)
25+
26+
# get countries available in the dataset
27+
country_names = asutosh.get_countries()
28+
29+
# attributes
30+
attribute_names = ["population", "per_capita_income", "life_expectancy"]
31+
32+
# Embed plot into HTML via Flask Render
33+
script, div = components(plot)
34+
return render_template("index.html", script = script, div = div,
35+
attribute_names = attribute_names, current_attribute_name = attribute,
36+
country_names = country_names, first_country = first_country, second_country = second_country)
37+
38+
# With debug=True, Flask server will auto-reload
39+
# when there are code changes
40+
41+
def main():
42+
app.run(port=5000, debug=True)
43+
44+
if __name__ == '__main__':
45+
main()

28/asutosh97/asutosh.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# get both the countries and attribute value from drop-down
2+
import pandas as pd
3+
from bokeh.plotting import figure, show, output_file
4+
5+
def read_data():
6+
data = pd.read_csv('datasets/gapminder.csv')
7+
data = data[(data.Year >= 1950)]
8+
return data
9+
10+
def create_figure(first_country, second_country, attribute):
11+
# read from csv
12+
data = read_data()
13+
14+
# filter datasets according to country
15+
first_country_data = data[(data.Country == first_country)]
16+
17+
second_country_data = data[(data.Country == second_country)]
18+
19+
# get required attribute out of filtered datasets of both countries and turn them to list
20+
first_country_data_attribute = list(first_country_data[attribute])
21+
second_country_data_attribute = list(second_country_data[attribute])
22+
years = list(first_country_data["Year"])
23+
24+
# create a new plot
25+
p = figure(title = (first_country + " Vs " + second_country + " in " + attribute), x_axis_label='Years', plot_width=1280, plot_height=720)
26+
27+
# plot for first-country
28+
p.line(years, first_country_data_attribute, legend = first_country, line_color="blue", line_width=3)
29+
30+
# plot for second-country
31+
p.line(years, second_country_data_attribute, legend = second_country, line_color="green", line_width=3)
32+
33+
return p
34+
35+
def get_countries():
36+
# get countries column as a list
37+
return sorted(list(set(read_data()['Country'])))
38+
39+
def main():
40+
p = create_figure("India", "Pakistan", "income")
41+
output_file('output_files/new.html')
42+
show(p)
43+
44+
if __name__ == "__main__":
45+
main()

0 commit comments

Comments
 (0)