-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
122 lines (98 loc) · 5.12 KB
/
app.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
import flask
from flask import Flask, render_template, request, redirect, url_for, jsonify
from flask_mysqldb import MySQL
import pickle
import base64
from training import prediction
import requests
app = flask.Flask(__name__)
data = [{'name':'Delhi', "sel": "selected"}, {'name':'Mumbai', "sel": ""}, {'name':'Kolkata', "sel": ""}, {'name':'Bangalore', "sel": ""}, {'name':'Chennai', "sel": ""}]
# data = [{'name':'India', "sel": ""}]
months = [{"name":"May", "sel": ""}, {"name":"June", "sel": ""}, {"name":"July", "sel": "selected"}]
cities = [{'name':'Delhi', "sel": "selected"}, {'name':'Mumbai', "sel": ""}, {'name':'Kolkata', "sel": ""}, {'name':'Bangalore', "sel": ""}, {'name':'Chennai', "sel": ""}, {'name':'New York', "sel": ""}, {'name':'Los Angeles', "sel": ""}, {'name':'London', "sel": ""}, {'name':'Paris', "sel": ""}, {'name':'Sydney', "sel": ""}, {'name':'Beijing', "sel": ""}]
model = pickle.load(open("model.pickle", 'rb'))
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = 'bhavya'
app.config['MYSQL_DB'] = 'floodcare'
mysql = MySQL(app)
@app.route("/")
@app.route('/index.html')
def index() -> str:
cur = mysql.connection.cursor()
"""Base page."""
return flask.render_template("index.html")
@app.route('/register', methods=['POST'])
def register():
if request.method == 'POST':
name = request.form.get('name')
email = request.form.get('email')
subject = request.form.get('subject')
cursor = mysql.connection.cursor()
query = "INSERT INTO volunteer (name, email, subject) VALUES (%s, %s, %s)"
cursor.execute(query, (name, email, subject))
mysql.connection.commit()
# Set a variable to indicate registration success
registration_success = True
return flask.render_template("index.html", registration_success=registration_success)
@app.route('/plots.html')
def plots():
return render_template('plots.html')
@app.route('/heatmaps.html')
def heatmaps():
return render_template('heatmaps.html')
@app.route('/satellite.html')
def satellite():
direc = "processed_satellite_images/Delhi_July.png"
with open(direc, "rb") as image_file:
image = base64.b64encode(image_file.read())
image = image.decode('utf-8')
return render_template('satellite.html', data=data, image_file=image, months=months, text="Delhi in January 2020")
@app.route('/satellite.html', methods=['GET', 'POST'])
def satelliteimages():
place = request.form.get('place')
date = request.form.get('date')
data = [{'name':'Delhi', "sel": ""}, {'name':'Mumbai', "sel": ""}, {'name':'Kolkata', "sel": ""}, {'name':'Bangalore', "sel": ""}, {'name':'Chennai', "sel": ""}]
months = [{"name":"May", "sel": ""}, {"name":"June", "sel": ""}, {"name":"July", "sel": ""}]
for item in data:
if item["name"] == place:
item["sel"] = "selected"
for item in months:
if item["name"] == date:
item["sel"] = "selected"
text = place + " in " + date + " 2020"
direc = "processed_satellite_images/{}_{}.png".format(place, date)
with open(direc, "rb") as image_file:
image = base64.b64encode(image_file.read())
image = image.decode('utf-8')
return render_template('satellite.html', data=data, image_file=image, months=months, text=text)
@app.route('/predicts.html')
def predicts():
return render_template('predicts.html', cities=cities, cityname="Information about the city")
@app.route('/predicts.html', methods=["GET", "POST"])
def get_predicts():
return render_template('predicts.html')
cityname = request.form["city"]
cities = [{'name':'Delhi', "sel": ""}, {'name':'Mumbai', "sel": ""}, {'name':'Kolkata', "sel": ""}, {'name':'Bangalore', "sel": ""}, {'name':'Chennai', "sel": ""}, {'name':'New York', "sel": ""}, {'name':'Los Angeles', "sel": ""}, {'name':'London', "sel": ""}, {'name':'Paris', "sel": ""}, {'name':'Sydney', "sel": ""}, {'name':'Beijing', "sel": ""}]
for item in cities:
if item['name'] == cityname:
item['sel'] = 'selected'
print(cityname)
URL = "https://geocode.search.hereapi.com/v1/geocode"
location = cityname
api_key = 'Bwv2FJJQHT4FTQBWFC7IEKRE49lNYtrAti6NK7uJVCY' # Acquire from developer.here.com
PARAMS = {'apikey':api_key,'q':location}
# sending get request and saving the response as response object
r = requests.get(url = URL, params = PARAMS)
data = r.json()
latitude = data['items'][0]['position']['lat']
longitude = data['items'][0]['position']['lng']
final = prediction.get_data(latitude, longitude)
final[4] *= 15
if str(model.predict([final])[0]) == "0":
pred = "Safe"
else:
pred = "Unsafe"
return render_template('predicts.html', cityname="Information about " + cityname, cities=cities, temp=round(final[0], 2), maxt=round(final[1], 2), wspd=round(final[2], 2), cloudcover=round(final[3], 2), percip=round(final[4], 2), humidity=round(final[5], 2), pred = pred)
if __name__ == "__main__":
app.run(debug=True)