-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
116 lines (95 loc) · 3.09 KB
/
server.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
# Zain Tahlilkar, July 2018
from flask import Flask, flash, redirect, render_template, request, session, abort, jsonify
app = Flask(__name__)
# Load Main Page
@app.route("/")
def load_index():
return render_template('index.html')
# Return Chart Data
@app.route("/chart")
def chart():
# test: reading month that was sent with request
month = request.args.get('month')
print("Month " + month)
#DEFINING dict for mapping months to numbers
month_dict = {'January': 0, 'February':1, 'March': 2, 'April':3, 'May': 4, 'June':5,
'July': 6, 'August':7, 'September': 8, 'October':9, 'November': 10, 'December':11}
# mask to determine which month(s) to display on chart
month_mask = [False, False, False, False, False, False, False, False, False, False, False, False]
# setting mask appropriately based on month sent with request
if (month == 'all_months'):
for i in range(len(month_mask)):
month_mask[i] = True
elif (month == 'winter_months'):
#lazy way to do it. Dec - Feb
month_mask[11] = True
month_mask[0] = True
month_mask[1] = True
elif (month == 'spring_months'):
#lazy way to do it. Mar - May
month_mask[2] = True
month_mask[3] = True
month_mask[4] = True
elif (month == 'summer_months'):
#lazy way to do it. Jun - Aug
month_mask[5] = True
month_mask[6] = True
month_mask[7] = True
elif (month == 'fall_months'):
#lazy way to do it. Sep - Nov
month_mask[8] = True
month_mask[9] = True
month_mask[10] = True
else:
month_mask[month_dict[month]] = True
# Reading csv file and cleaning up data
file = open("toronto_historical.csv")
file_contents = file.readlines()
toronto_info = file_contents[21:1980] # lines which have useful data on them
for i in range(len(toronto_info)):
toronto_info[i] = toronto_info[i].split(",")
for j in range(len(toronto_info[i])):
toronto_info[i][j] = toronto_info[i][j].strip('"')
#print(toronto_info[i])
# Creating Datasets for chart
x_labels = []
high_temps = []
low_temps = []
mean_temps= []
print (month_mask)
for i in range(len(toronto_info)):
current_month = int(toronto_info[i][2]) - 1
if month_mask[current_month] == True:
x_labels.append(toronto_info[i][0])
high_temps.append(toronto_info[i][3])
low_temps.append(toronto_info[i][5])
mean_temps.append(toronto_info[i][7])
# creating json object
return jsonify (
name = "Yearly " + month + " Temperatures in Toronto",
mean_temp = mean_temps,
high_temp = high_temps,
low_temp = low_temps,
labels = x_labels
)
@app.route("/test")
def test():
file = open("1-1938.csv")
file_contents = file.readlines()
tobermory_info = file_contents[161] #toronto
tobermory_info = tobermory_info.split(",")
for i in range(len(tobermory_info)):
tobermory_info[i] = tobermory_info[i].strip('"')
print(tobermory_info[i])
# creating json object
return jsonify (
name = tobermory_info[0],
mean_temp = [1, 2, 3, 2, 1],#float(tobermory_info[4]),
high_temp = [3, 1, 6, 2, 9],#float(tobermory_info[6]),
low_temp = [1, 2, 1, 3, 1]#float(tobermory_info[8])
)
if __name__ == "__main__":
try:
app.run(host='0.0.0.0', port=3000)
except:
print("Server Crashed :(")