-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
155 lines (124 loc) · 6.01 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
from flask import Flask, render_template, request
import json_to_database
app = Flask(__name__)
json_to_database.start_database() # Create table - fetch dataset - parse data and insert to table
country_names = json_to_database.get_country_names() # Get all country name in data - for the dropdown menu
global_cases_deaths = json_to_database.select_global_cases_and_deaths() # Get total world cases and deaths
@app.route('/')
@app.route('/home') # Gets called when home page is requested
def home():
result = json_to_database.select_all_countries() # Get total cases and deaths per country
return render_template(
'home.html',
rlt=result,
cntr_nms=country_names,
gl_cs_dths=global_cases_deaths
)
@app.route('/result', methods=['POST']) # Called when form is submitted
def result():
country = request.form['country'] # Get the selected country from the form
date_from = request.form['date1'] # Get the given 'from date' from the form
date_to = request.form['date2'] # Get the given 'to date' from the form
if date_from > date_to: # If user gives a from date that's after to date
# swap dates
temp = date_from
date_from = date_to
date_to = temp
if date_from and date_to: # If both (from and to) dates were given from the user
# Get per day cases and deaths for the 'country' between 'date_from' and 'date_to' dates
result = json_to_database.select_a_country_between_dates(country, date_from, date_to, )
else: # User selected only a country
# Get per day cases and deaths for the 'country' for all available dates
result = json_to_database.select_a_country(country)
result2 = json_to_database.select_cases_and_deaths_per_country(country) # Get total cases and deaths for 'country'
return render_template(
'home.html',
rlt4=result,
rlt2=result2,
cntr_nms=country_names,
gl_cs_dths=global_cases_deaths,
d_from=date_from,
d_to=date_to,
)
@app.route('/result2', methods=['POST']) # Called when form is submitted
def result2():
n = request.form['topNCases'] # Get the selected number of countries from the form
result3 = json_to_database.select_the_n_most_countries_cases(n) # Get the 'n' countries with most cases
return render_template(
'home.html',
rlt3=result3,
cntr_nms=country_names,
gl_cs_dths=global_cases_deaths,
number=n
)
@app.route('/result3', methods=['POST']) # Called when form is submitted
def result3():
n = request.form['topNDeaths'] # Get the selected number of countries from the form
result3 = json_to_database.select_the_n_most_countries_deaths(n) # Get the 'n' countries with most deaths
return render_template(
'home.html',
rlt3=result3,
cntr_nms=country_names,
gl_cs_dths=global_cases_deaths,
number=n
)
@app.route('/result4') # Called when form is submitted
def result4():
# Get total cases and deaths per continent
continent_cases_deaths = json_to_database.select_continent_cases_and_deaths()
return render_template(
'home.html',
cntr_nms=country_names,
gl_cs_dths=global_cases_deaths,
cnt_cs_dths=continent_cases_deaths
)
@app.route('/result5', methods=['POST']) # Called when form is submitted
def result5():
# Get the spread percentage per country
spread_percentages = json_to_database.get_spread_percentage()
return render_template(
'home.html',
cntr_nms=country_names,
gl_cs_dths=global_cases_deaths,
rlt5=spread_percentages
)
@app.route('/result6', methods=['POST']) # Called when form is submitted
def result6():
country1 = request.form['country1'] # Get the first selected country from the form
country2 = request.form['country2'] # Get the second selected country from the form
date_from = request.form['date1'] # Get the given 'from date' from the form
date_to = request.form['date2'] # Get the given 'to date' from the form
if date_from > date_to: # If user gives a from date that's after to date
# swap dates
temp = date_from
date_from = date_to
date_to = temp
if date_from and date_to: # If both (from and to) dates were given from the user
# Get per day cases and deaths for the 'country1' and 'country2' between 'date_from' and 'date_to' dates
result = json_to_database.select_a_country_between_dates(country1, date_from, date_to, )
result1 = json_to_database.select_a_country_between_dates(country2, date_from, date_to, )
else: # User selected only the two countries
# Get per day cases and deaths for the 'country1' and 'country2' for all available dates
result = json_to_database.select_a_country(country1)
result1 = json_to_database.select_a_country(country2)
# Concatenate the 2 lists of tuples to one list of tuples
result_list = []
for t in result: # for each tuple in 'result' list
for t1 in result1: # for each tuple in 'result1' list
if t[0] == t1[0]: # if fist items of tuples 't' and 't1' are equal - if dates are equal
result_list.append(t + t1) # add to 'result_list' list the concatenation of the two tuples
result2 = json_to_database.select_cases_and_deaths_per_country(country1) # Get total cases and deaths for 'country1'
result3 = json_to_database.select_cases_and_deaths_per_country(country2) # Get total cases and deaths for 'country2'
return render_template(
'home.html',
rlt_con=result_list,
rlt2=result2,
rlt3=result3,
cntr_nms=country_names,
gl_cs_dths=global_cases_deaths,
d_from=date_from,
d_to=date_to,
)
if __name__ == '__main__':
app.run()
# app.run(debug=True) # run in debug mode to auto-restart server on code changes