forked from pybites/challenges
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
41,429 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains 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
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() |
This file contains 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
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() |
Oops, something went wrong.