-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.py
124 lines (104 loc) · 4.15 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
import json
import pandas as pd
import numpy as np
import sqlalchemy
from sqlalchemy.ext.automap import automap_base
from sqlalchemy.orm import Session
from sqlalchemy import create_engine, inspect, func
from flask import Flask, jsonify, render_template
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
# Database Setup
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///USEnergy.sqlite"
engine = create_engine("sqlite:///USEnergy.sqlite")
inspector = inspect(engine)
inspector.get_table_names()
#Available Routes
@app.route("/")
def index():
return render_template("index.html")
@app.route("/energy")
def energy():
return render_template("energy.html")
@app.route("/energy2")
def energy2():
return render_template("energy2.html")
@app.route("/electrical")
def electrical():
return render_template("electrical.html")
#Returns a list of all 48 states
@app.route("/states")
def states():
columns = inspector.get_columns('Average Energy Prices')
states = []
for column in columns:
states.append(column["name"])
return jsonify(states[1:])
#Returns a list of all years
@app.route("/year")
def years():
prices = pd.read_sql_query("SELECT * from 'Average Energy Prices'",con = engine)
years = prices["index"]
years_list = years.tolist()
return jsonify(years_list)
#Returns a list of prices based on the year selected
@app.route("/prices/<year>")
def prices(year):
price = pd.read_sql_query("SELECT * from 'Average Energy Prices'",con = engine)
year_df = price.loc[price['index'] == year]
return year_df.to_json()
#Returns a list of prices based on the state selected
@app.route("/state_price/<state>")
def state_price(state):
df = pd.read_sql_query("SELECT * from 'Average Energy Prices'",con = engine)
df.set_index("index",inplace =True)
state_df = df.loc[:,state]
return state_df.to_json()
#Returns a list of energy generation based on the state selected
@app.route("/state_gen/<state>")
def state_gen(state):
df = pd.read_sql_query(f"SELECT * from '{state} Generation'", con=engine)
df.set_index("index",inplace =True)
return df.to_json()
#Returns all of the price data
@app.route("/price_data")
def price_data():
df = pd.read_sql_query("SELECT * from 'Average Energy Prices'",con = engine)
df.set_index("index",inplace =True)
return df.to_json()
#Returns all of the user end consumption data
@app.route("/user")
def user():
total_df = pd.read_sql_query("SELECT * from 'End User Consumption'",con = engine)
total_df.set_index("index",inplace=True)
df=total_df.T
df["Coal"] = list(map(lambda x: x[1:-2], df["Coal"].values))
df['Coal'] = pd.to_numeric(df.Coal)
df["Naturalgas"] = list(map(lambda x: x[1:-2], df["Naturalgas"].values))
df['Naturalgas'] = pd.to_numeric(df.Naturalgas)
df["Petroleum"] = list(map(lambda x: x[1:-2], df["Petroleum"].values))
df['Petroleum'] = pd.to_numeric(df.Petroleum)
df["Renewableenergy"] = list(map(lambda x: x[1:-2], df["Renewableenergy"].values))
df['Renewableenergy'] = pd.to_numeric(df.Renewableenergy)
df["Electricity"] = list(map(lambda x: x[1:-2], df["Electricity"].values))
df['Electricity'] = pd.to_numeric(df.Electricity)
return df.to_json()
#Returns all of the electrical consumption data
@app.route("/electricity")
def electricity():
total_df = pd.read_sql_query("SELECT * from 'Electric Power Consumption'",con = engine)
total_df.set_index("index",inplace=True)
df=total_df.T
df["Coal"] = list(map(lambda x: x[1:-2], df["Coal"].values))
df['Coal'] = pd.to_numeric(df.Coal)
df["Naturalgas"] = list(map(lambda x: x[1:-2], df["Naturalgas"].values))
df['Naturalgas'] = pd.to_numeric(df.Naturalgas)
df["Petroleum"] = list(map(lambda x: x[1:-2], df["Petroleum"].values))
df['Petroleum'] = pd.to_numeric(df.Petroleum)
df["Renewableenergy"] = list(map(lambda x: x[1:-2], df["Renewableenergy"].values))
df['Renewableenergy'] = pd.to_numeric(df.Renewableenergy)
df["Nuclearpower"] = list(map(lambda x: x[1:-2], df["Nuclearpower"].values))
df['Nuclearpower'] = pd.to_numeric(df.Nuclearpower)
return df.to_json()
if __name__ == "__main__":
app.run(debug=True)