-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery.py
69 lines (48 loc) · 1.65 KB
/
query.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
from sqlalchemy import create_engine, text
import datetime
import matplotlib.pyplot as plt
from matplotlib.pyplot import figure
engine = create_engine("mysql+pymysql://carbonara:[email protected]/plants?charset=utf8")
#defining a function which returns a moving average
def moving_average(period, list):
mean = []
i = 0
while i < len(list) - period + 1:
per = list[i : i + period]
avg = round(sum(per)/period, 2)
mean.append(avg)
i += 1
return mean
def generate_images():
#initializing variables
xm = []
ym = []
xb = []
yb = []
#establishing a connection to database
with engine.connect() as conn:
#selecting data from database
monstera = conn.execute(text(f"select * from monstera;"))
bean = conn.execute(text(f"select * from bean;"))
#creating lists for x and y axes and converting to local time
for row in monstera:
xm.append(row[0] + datetime.timedelta(hours = 2))
ym.append(-1*row[1])
for row in bean:
xb.append(row[0] + datetime.timedelta(hours = 2))
yb.append(-1*row[1])
plt.figure(figsize=(13, 3))
plt.title('monstera')
#plottig the data
plt.plot(xm, ym, linestyle='-')
#plotting a moving average
plt.plot(xm[19:len(xm)], moving_average(20, ym))
plt.savefig('static/monstera.png')
plt.figure(figsize=(13, 3))
plt.title("Bean")
#plottig the data
plt.plot(xb, yb, linestyle='-')
#plotting a moving average
plt.plot(xb[19:len(xb)], moving_average(20, yb))
plt.savefig('static/bean.png')
return ["monstera.png", "bean.png"]