-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.py
231 lines (204 loc) · 8 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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import numpy as np
import os
import json
import requests
import sqlalchemy
from sqlalchemy.ext.automap import automap_base
from sqlalchemy.orm import Session
from sqlalchemy import create_engine
import pandas.io.sql as pdsql
from config import pg_user, pg_password, db_name, pg_host # Corrected import
from flask import Flask, jsonify, render_template, abort, redirect
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import create_engine, MetaData
from flask import Flask, jsonify, render_template
import pandas as pd
import json
from sqlalchemy import create_engine
#################################################
# Database Setup
##################################################
# Construct the DATABASE_URL
DATABASE_URL = f"postgresql://{pg_user}:{pg_password}@{pg_host}/{db_name}"
DATABASE_URL = DATABASE_URL.replace(
'postgres://',
'postgresql://',
1
)
engine = create_engine(DATABASE_URL)
meta = sqlalchemy.MetaData()
meta.reflect(bind=engine)
table_names = meta.tables.keys()
print(table_names) # or use the list of table_names as needed
#################################################
# Flask Setup
#################################################
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DATABASE_URI', '') or "sqlite:///db.sqlite"
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
#################################################
# Flask Routes
#################################################
@app.route("/")
def index():
return render_template("index.html")
@app.route("/Home")
def home():
return render_template("index.html")
@app.route('/searchbyyear')
def searchbyyear():
sqlStatement = """
SELECT year, SUM ("Cancer" + "cardiovascular" + "stroke" + "depression" + "rehab" + "vaccine" + "diarrhea" + "obesity" + "diabetes") AS Searches
FROM search_condition
GROUP BY year
ORDER BY year;
"""
df = pdsql.read_sql(sqlStatement, engine)
df.set_index('year', inplace=True)
df = df.to_json(orient='table')
result = json.loads(df)
return jsonify(result)
@app.route('/searchyearandcondition')
def searchyearandcondition():
sqlStatement = """
SELECT year, SUM ("Cancer") AS Cancer, SUM ("cardiovascular") AS Cardiovascular, SUM ("stroke") AS Stroke,
SUM ("depression") AS Depression, SUM ("rehab") AS Rehab, SUM ("vaccine") AS Vaccine,
SUM ("diarrhea") AS Diarrhea, SUM("obesity") AS Obesity, SUM ("diabetes") AS Diabetes
FROM search_condition
GROUP BY year
ORDER BY year;
"""
try:
with engine.connect() as connection:
result = connection.execute(sqlStatement)
df = pd.DataFrame(result.fetchall(), columns=result.keys())
df.set_index('year', inplace=True)
json_result = df.to_json(orient='table')
parsed_result = json.loads(json_result)
return jsonify(parsed_result)
except Exception as e:
return jsonify({"error": str(e)})
@app.route('/searchbycity')
def searchbycity():
sqlStatement = """
SELECT l.city,l.postal,l.state, l.latitude, l.longitude, SUM (s."Cancer" + s."cardiovascular" + s."stroke" + s."depression" + s."rehab" + s."vaccine" + s."diarrhea" + s."obesity" + s."diabetes") AS Searches
FROM location l
INNER JOIN search_condition s on s.location_id = l.location_id
GROUP BY l.city,l.state,l.postal, l.latitude, l.longitude
ORDER BY l.city;
"""
df = pdsql.read_sql(sqlStatement, engine)
df.set_index('city', inplace=True)
df = df.to_json(orient='table')
result = json.loads(df)
return jsonify(result)
@app.route('/searchbystate')
def searchbystate():
sqlStatement = """
SELECT l.state,l.postal, SUM (s."Cancer" + s."cardiovascular" + s."stroke" + s."depression" + s."rehab" + s."vaccine" + s."diarrhea" + s."obesity" + s."diabetes") AS Searches
FROM location l
INNER JOIN search_condition s on s.location_id = l.location_id
GROUP BY l.state,l.postal;
"""
df = pdsql.read_sql(sqlStatement, engine)
df.set_index('state', inplace=True)
df = df.to_json(orient='table')
result = json.loads(df)
return jsonify(result)
@app.route('/bystateandyear')
def bylocationandyear():
sqlStatement = """
SELECT l.state, l.latitude, l.longitude,s.year, SUM (s."Cancer" + s."cardiovascular" + s."stroke" + s."depression" + s."rehab" + s."vaccine" + s."diarrhea" + s."obesity" + s."diabetes") AS Searches
FROM location l
INNER JOIN search_condition s on s.location_id = l.location_id
GROUP BY l.state, l.latitude, l.longitude, s.year
ORDER BY s.year;
"""
df = pdsql.read_sql(sqlStatement, engine)
df.set_index('state', inplace=True)
df = df.to_json(orient='table')
result = json.loads(df)
return jsonify(result)
@app.route('/casesleadingdeath')
def casesleadingdeath():
sqlStatement = """
SELECT * FROM leading_causes_of_death;
"""
df = pdsql.read_sql(sqlStatement, engine)
df.set_index('year', inplace=True)
df = df.to_json(orient='table')
result = json.loads(df)
return jsonify(result)
@app.route('/allsearchrecord')
def allsearchrecord():
sqlStatement = """
SELECT *
FROM location l
INNER JOIN search_condition s on s.location_id = l.location_id
ORDER BY s.year;
"""
df = pdsql.read_sql(sqlStatement, engine)
df.set_index('year', inplace=True)
df = df.to_json(orient='table')
result = json.loads(df)
return jsonify(result)
@app.route('/location')
def location():
sqlStatement = """
SELECT * FROM location;
"""
df = pdsql.read_sql(sqlStatement, engine)
df.set_index('location_id', inplace=True)
df = df.to_json(orient='table')
result = json.loads(df)
return jsonify(result)
@app.route('/conditions')
def conditions():
sqlStatement = """
SELECT * FROM search_condition;
"""
df = pdsql.read_sql(sqlStatement, engine)
df.set_index('location_id', inplace=True)
df = df.to_json(orient='table')
result = json.loads(df)
return jsonify(result)
@app.route('/mostsserached')
def mostsserached():
sqlStatement = """
SELECT l.state, SUM ("Cancer") AS Cancer,SUM ("cardiovascular") As Cardiovascular,SUM ("stroke") As Stroke,SUM ("depression") As Depression,SUM ("rehab") AS Rehab,SUM ("vaccine") AS Vaccine, SUM ("diarrhea") AS Diarrhea, SUM("obesity") AS Obesity, SUM ("diabetes") AS Diabetes
FROM location l
INNER JOIN search_condition s on s.location_id = l.location_id
GROUP BY state
order by Cancer desc, Cardiovascular desc,Stroke desc,Depression desc,Rehab desc,Vaccine desc, Diarrhea desc, Diabetes desc, Obesity desc
LIMIT 10;
"""
df = pdsql.read_sql(sqlStatement, engine)
df.set_index('state', inplace=True)
df = df.to_json(orient='table')
result = json.loads(df)
return jsonify(result)
@app.route('/totalcondition')
def totalcondition():
sqlStatement = """
SELECT SUM ("Cancer") AS Cancer,SUM ("cardiovascular") As Cardiovascular,SUM ("stroke") As Stroke,SUM ("depression") As Depression,SUM ("rehab") AS Rehab,SUM ("vaccine") AS Vaccine, SUM ("diarrhea") AS Diarrhea, SUM("obesity") AS Obesity, SUM ("diabetes") AS Diabetes
FROM search_condition
"""
df = pdsql.read_sql(sqlStatement, engine)
df.set_index('cancer', inplace=True)
df = df.to_json(orient='table')
result = json.loads(df)
return jsonify(result)
@app.route('/totaldeathcase')
def totaldeathcase():
sqlStatement = """
SELECT SUM ("Accidents") AS Accidents,SUM ("Alzheimer") As Alzheimer,SUM ("Cerebrovascular") As Cerebrovascular,SUM ("Diabetes") As Diabetes,SUM ("Diseases_of_heart") AS Diseases_of_Heart, SUM ("Influenza_and_pneumonia") AS Influenza_and_Pneumonia, SUM ("Malignant_neoplasms") AS Malignant_eoplasms, SUM("Nephrosis") AS Nephrosis, SUM ("Suicide") AS Suicide,SUM ("Respiratory") AS Respiratory
FROM leading_causes_of_death
"""
df = pdsql.read_sql(sqlStatement, engine)
df.set_index('alzheimer', inplace=True)
df = df.to_json(orient='table')
result = json.loads(df)
return jsonify(result)
if __name__ == '__main__':
app.run(debug=True)