-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp_solution_project3.py
140 lines (106 loc) · 4.15 KB
/
app_solution_project3.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
from types import prepare_class
import numpy as np
import pandas as pd
import sqlalchemy
from sqlalchemy.ext.automap import automap_base
from sqlalchemy.orm import Session
from sqlalchemy import create_engine, text, func
from flask import Flask, render_template
import json
from flask import Flask, jsonify
#################################################
# Database Setup
#################################################
# Create the engine
engine = create_engine("sqlite:///ofsted_results.sqlite")
# # reflect an existing database into a new model
# Base = automap_base()
# # reflect the tables
# Base.prepare(autoload_with=engine)
# # Save reference to the table
# School = Base.classes.ofsted_data
#################################################
# Flask Setup
#################################################
app = Flask(__name__)
#################################################
# Flask Routes
#################################################
@app.route("/")
def welcome():
"""List all available api routes."""
return (
f"Available Routes:<br/>"
f"/dashboard"
)
@app.route("/dashboard")
def dashboard():
# Create our session (link) from Python to the DB
session = Session(engine)
# Get the data from the db
data = session.execute(text('SELECT * FROM ofsted_data')).fetchall()
# create a DF of the data
ofsted_data = pd.DataFrame(data)
session.close()
# Convert the df to a JSON file
ofsted_data.to_json("static/ofsted_data.json", orient='records', indent=4)
# Web_Link, URN, School_name, Ofsted_phase, Local_authority, Postcode, lat, lon
web_link = list(ofsted_data["Web_Link"])
urn = list(ofsted_data["URN"])
school_name = list(ofsted_data["School_name"])
ofsted_phase = {
"Phase": list(ofsted_data["Ofsted_phase"].value_counts().index),
"Count": list(ofsted_data["Ofsted_phase"].value_counts())
}
local_auth = list(ofsted_data["Local_authority"])
postcode = list(ofsted_data["Postcode"])
lat = list(ofsted_data["lat"])
lon = list(ofsted_data["lon"])
ofsted_ratings_count = {
"Primary":{
"Grades" : list(ofsted_data["Overall_effectiveness"].loc[ofsted_data['Ofsted_phase'] == "Primary"].value_counts().index),
"Values" : list(ofsted_data["Overall_effectiveness"].loc[ofsted_data['Ofsted_phase'] == "Primary"].value_counts())
},
"Secondary":{
"Grades" : list(ofsted_data["Overall_effectiveness"].loc[ofsted_data['Ofsted_phase'] == "Secondary"].value_counts().index),
"Values" : list(ofsted_data["Overall_effectiveness"].loc[ofsted_data['Ofsted_phase'] == "Secondary"].value_counts())
}
}
file1 = open('static/weblink_JSON.json', 'w')
file1.write(json.dumps(web_link))
file1.close
file2 = open('static/urn_JSON.json', 'w')
file2.write(json.dumps(urn))
file2.close
file3 = open('static/schoolname_JSON.json', 'w')
file3.write(json.dumps(school_name))
file3.close
file4 = open('static/ofstedphase_JSON.json', 'w')
file4.write(json.dumps(ofsted_phase))
file4.close
file5 = open('static/localauth_JSON.json', 'w')
file5.write(json.dumps(local_auth))
file5.close
file6 = open('static/postcode_JSON.json', 'w')
file6.write(json.dumps(postcode))
file6.close
file7 = open('static/lat_JSON.json', 'w')
file7.write(json.dumps(lat))
file7.close
file8 = open('static/lon_JSON.json', 'w')
file8.write(json.dumps(lon))
file8.close
file9 = open('static/ofstedcounts_JSON.json', 'w')
file9.write(json.dumps(ofsted_ratings_count))
file9.close
#pclasslist = [a['pclass'] for a in all_schools]
#pclass_dict = {
#"pclassind":list(pd.Series(pclasslist).value_counts().index),
#"pclassvc" :list(pd.Series(pclasslist).value_counts())
#}
#file4 = open('static/pclass_JSON.json', 'w')
#file4.write(json.dumps(pclass_dict))
#file4.close
return render_template('index.html')
if __name__ == '__main__':
app.run(debug=True)