forked from Oriojas/polkadot_hackathon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot.py
More file actions
80 lines (62 loc) · 2.42 KB
/
Copy pathplot.py
File metadata and controls
80 lines (62 loc) · 2.42 KB
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
import os
import pymysql
import datetime
import pandas as pd
import plotly.graph_objects as go
from plotly.subplots import make_subplots
SERVER = os.environ["SERVER"]
DATABASE = os.environ["DATABASE"]
USERNAME = os.environ["USERNAME"]
PASSWORD = os.environ["PASSWORD"]
DRIVER = os.environ["DRIVER"]
class plotSensor:
def __init__(self):
"""
this class request database co2 info
"""
with pymysql.connect(host=SERVER,
port=3306,
user=USERNAME,
passwd=PASSWORD,
database=DATABASE) as conn:
sql_query = f'SELECT * FROM sys.co2Storage'
df = pd.read_sql(sql_query, conn)
df['date_c'] = pd.to_datetime(df['date_c'])
end = max(df['date_c'])
init = end - datetime.timedelta(hours=1)
print(init)
self.DF = df[df['date_c'] > init]
def plot(self, wallet_1, wallet_2):
DF = self.DF
balance_w = [wallet_2, wallet_1]
names = ['reward', 'pool reward']
fig = make_subplots(1, 2)
fig.add_trace(go.Scatter(x=DF['date_c'],
y=DF['co2'][DF['origin'] == 'sensor03'],
name='co2 (Sensor)',
mode='lines',
line_color='rgb(230,0,122)'), 1, 1)
fig.add_trace(go.Bar(name='Grant',
x=balance_w,
y=names,
text=balance_w,
orientation='h',
textposition='auto',
marker_color='rgb(230,0,122)'), 1, 2)
template = 'plotly_white'
fig.update_layout(template=template,
autosize=True,
height=340,
margin=dict(l=10, r=10, t=25, b=10),
title="PPM co2 and WALLET STATUS LAST HOUR")
# fig.show()
fig.update_layout(showlegend=False)
# convert it to JSON
fig_json = fig.to_json()
# a simple HTML template
template = 'var plotly_data = {}'
# write the JSON to the HTML template
with open('templates/plots/new_plot.txt', 'w', encoding='utf-8') as f:
f.write(template.format(fig_json))
if __name__ == '__main__':
plotSensor().plot(12, 5)