-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbasic_front_end.py
202 lines (146 loc) · 4.82 KB
/
basic_front_end.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
from dash import Dash, dcc, html, Input, Output, callback, dash_table
import time
import datetime
import random
import plotly.express as px
app = Dash(__name__, suppress_callback_exceptions=True)
from Functions import *
# Readed data
df2 = read_example_csv()
total_power_df = pd.DataFrame(columns=["Total Power"], index=pd.DatetimeIndex([]))
df4= create_df('daily_energy',time_data=90);
def random_data_df():# df_update
total_power_df.loc[datetime.datetime.now()] = random.randint(0, 200)
return total_power_df
# d = {'Energy consumed': [1], 'Current energy': [3],'Current power': [3],'Current cost': [3]}
# Create a blant table
# create dataframe for total energy frame df3
d = {'Sensor name': ["robot1","robot2"], 'Type': ["robot","robot"],'State': [3,3],'Power': [4,4],'Total energy used today': [df4["daily_energy"].iloc[-1],5],'Total cost today': [6,6]}
df3=pd.DataFrame(data=d)
app.layout = html.Div([
dcc.Location(id='url', refresh=False),
html.Div(id='page-content')
])
index_page = html.Div([
dcc.Link('Pie chart', href='/page-1'),
html.Br(),
dcc.Link('Line chart', href='/page-2'),
html.Br(),
dcc.Link('Table', href='/page-3'),
html.Br(),
dcc.Link('Second based updated table', href='/page-4'),
])
page_1_layout = html.Div([
html.H1('Page 1'),
dcc.Graph(
figure=px.pie(df2, values='Power', names='Machine')
),
html.Div(id='page-1-content'),
html.Br(),
dcc.Link('Go to Line chart', href='/page-2'),
html.Br(),
dcc.Link('Go back to home', href='/'),
])
@callback(Output('page-1-content', 'children'),
[Input('page-1-dropdown', 'value')])
def page_1_dropdown(value):
return f'You have selected {value}'
page_2_layout = html.Div([
html.H1('Page 2'),
# dcc.Graph(
# figure = px.line(df2, x=df2.index, y='Power', title='Time Series with Range Slider and Selectors')
# ),
dcc.Graph(
figure=dict(
data=[
dict(
x=df2.index,
y=df2['Power'].loc[df2['Machine'] == 'robot1'], # df.loc[df['column_name'] == some_value]
name='Robot 1',
marker=dict(
color='rgb(55, 83, 109)'
)
),
dict(
x=df2.index,
y=df2['Power'].loc[df2['Machine'] == 'robot2'], # df.loc[df['column_name'] == some_value]
name='Robot 2',
marker=dict(
color='rgb(26, 118, 255)'
)
)
],
layout=dict(
title='Robot Power',
showlegend=True,
legend=dict(
x=0,
y=1.0
),
margin=dict(l=40, r=0, t=40, b=30)
)
),
style={'height': 300},
id='my-graph'
),
html.Div(id='page-2-content'),
html.Br(),
dcc.Link('Go to Pie Chart', href='/page-1'),
html.Br(),
dcc.Link('Go back to home', href='/')
])
@callback(Output('page-2-content', 'children'),
[Input('page-2-radios', 'value')])
def page_2_radios(value):
return f'You have selected {value}'
page_3_layout = html.Div([
html.H1('Page 3'),
dash_table.DataTable(df3.to_dict('records'), [{"name": i, "id": i} for i in df3.columns]),
html.Div(id='page-3-content'),
html.Br(),
dcc.Link('Go to Line chart', href='/page-3'),
html.Br(),
dcc.Link('Go back to home', href='/'),
])
@callback(Output('page-3-content', 'children'),
[Input('page-3-dropdown', 'value')])
def page_3_dropdown(value):
return f'You have selected {value}'
page_4_layout = html.Div(
html.Div([
html.H4('Total Power'),
html.Div(id='live-update-text'),
dcc.Interval(
id='interval-component',
interval=1*1000, # in milliseconds
n_intervals=0
)
])
)
@app.callback(Output('live-update-text', 'children'),
Input('interval-component', 'n_intervals'))
def update_metrics(n):
random_data_df()
df_update(df4,"daily_energy")
style = {'padding': '5px', 'fontSize': '16px'}
return [
total_power_df["Total Power"].iloc[-1],
df4["daily_energy"].iloc[-1]
]
# Update the index
@callback(Output('page-content', 'children'),
[Input('url', 'pathname')])
def display_page(pathname):
if pathname == '/page-1':
return page_1_layout
elif pathname == '/page-2':
return page_2_layout
elif pathname == '/page-3':
return page_3_layout
elif pathname == '/page-4':
return page_4_layout
else:
return index_page
# You could also return a 404 "URL not found" page here
if __name__ == '__main__':
app.run_server(debug=True)