-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
117 lines (94 loc) · 3.78 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
from flask import Flask, render_template, redirect, url_for, request
from datetime import datetime
loc = ("")
import mysql.connector
from flask_table import Table, Col
#connect to local DB
mydb = mysql.connector.connect(
host="localhost",
user="rishabh",
passwd="123456",
database="power"
)
mycursor = mydb.cursor(buffered=True)
# create the application object
app = Flask(__name__,static_url_path='/static')
# use decorators to link the function to a url
@app.route('/', methods=['GET', 'POST'])
def home():
return redirect(url_for('login'))
#lgoin page
@app.route('/login', methods=['GET', 'POST'])
def login():
error = None
if request.method == 'POST':
if request.form['username'] != 'admin' or request.form['password'] != 'admin':
error = 'Invalid Credentials. Please try again.'
else:
return redirect(url_for('main'))
return render_template('login.html', error=error)
#landing page
@app.route('/main', methods=['GET', 'POST'])
def main():
return render_template('main.html', methods=['GET', 'POST'])
@app.route('/form', methods=['GET', 'POST'])
def form():
if request.method == 'POST':
timeform = request.form['datetimepicker']
timeform = datetime.strptime(timeform, "%m/%d/%Y %I:%M %p")
timeformdate = timeform.strftime("%Y-%m-%d %H:%M:%S")
#timeformtime = timeform.strftime("%H:%M:%S")
print(timeformdate)
#print(timeformtime)
sql = "SELECT * FROM sensor WHERE date <= " + "\'" + str(timeformdate) +"\'" + "ORDER BY date DESC"
#print(sql)
mycursor.execute(sql)
val1 = mycursor.fetchone()
val = mycursor.fetchmany(10)
if val1 == None:
print("date does not exist in records")
else:
print("exists")
# Get some objects
class ItemTable(Table):
name = Col('Date & Time')
description = Col('Current Reading')
class Item(object):
def __init__(self, name, description):
self.name = name
self.description = description
#fetching custom values from DB
val = mycursor.fetchmany(10)
fetchtime1, fetchvalue1 = val[0]
fetchtime2, fetchvalue2 = val[1]
fetchtime3, fetchvalue3 = val[2]
fetchtime4, fetchvalue4 = val[3]
fetchtime5, fetchvalue5 = val[4]
# fetchtime6, fetchvalue6 = val[5]
# fetchtime7, fetchvalue7 = val[6]
# fetchtime8, fetchvalue8 = val[7]
# fetchtime9, fetchvalue9 = val[8]
# fetchtime10, fetchvalue10 = val[9]
items = [Item(fetchtime1, fetchvalue1),
Item(fetchtime2, fetchvalue2),
Item(fetchtime3, fetchvalue3),
Item(fetchtime4, fetchvalue4),
Item(fetchtime5, fetchvalue5),]
# Item(fetchtime6, fetchvalue6),
# Item(fetchtime7, fetchvalue7),
# Item(fetchtime8, fetchvalue8),
# Item(fetchtime9, fetchvalue9),
# Item(fetchtime10, fetchvalue10),
# Populate the table
table = ItemTable(items)
return render_template('form.html',methods=['GET', 'POST'], tableprint = table)
return render_template('form.html',methods=['GET', 'POST'])
@app.route('/result',methods = ['GET', 'POST'])
def request_data():
return render_template('form.html', methods=['GET', 'POST'])
@app.route('/power')
def power():
return render_template('power.html', methods=['GET', 'POST'])
# start the server with the 'run()' method
if __name__ == '__main__':
app.run(debug=True,host= '0.0.0.0')