-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtab2.py
233 lines (179 loc) · 6.12 KB
/
tab2.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import dash
from dash import dcc
from dash import html
import time
from datetime import datetime
from dash.dependencies import Input, Output, State
from server import app
import sqlite3 as sql
import plotly.graph_objs as go
import sqlite3 as sql
import utilities as ut
#---------------- Page Layout ------------------------------------------------------
def tab2():
time_start = int(datetime.now().strftime('%s%f'))
conn = sql.connect("labjackdb.db")
c = conn.cursor()
with conn:
c.execute("SELECT * FROM preferences")
prefs = c.fetchone()
interval = prefs[4]
name0 = prefs[12]
name1 = prefs[13]
name2 = prefs[14]
name3 = prefs[15]
name4 = prefs[16]
name5 = prefs[17]
name6 = prefs[18]
xpoints = prefs[19]
with conn:
c.execute("SELECT * FROM sliderpos")
spos = c.fetchone()
chart = spos[3]
tab2 = html.Div([ dcc.Dropdown(
id='dropdown',
options=[
{'label': name0, 'value': 'AIN0'},
{'label': name1, 'value': 'AIN1'},
{'label': name2, 'value': 'AIN2'},
{'label': name3, 'value': 'AIN3'},
{'label': name4, 'value': 'AIN4'}
],style={'textAlign':'center'},
value=chart, #default value
),
html.Div(id='dd-output-container'),
dcc.Interval(
id='interval',
interval=interval,
n_intervals=0),
html.Div([
dcc.Graph( id='graph' ),
],
style = {'width':'100%',"border":"3px gray solid",'display':'inline-block'}
)
],
style = {'color':'black'},
)
return tab2
#---------------- END PAGE LAYOUT ------------------------------------------------------
@app.callback(
Output('graph','figure'),
[Input('interval','n_intervals')],
State('dropdown', 'value')
)
def interval(n,value):
i = None
data_dict = {'x':[],'y':[]}
conn = sql.connect("labjackdb.db")
c = conn.cursor()
# Set last chart viewed
with conn:
c.execute(f"UPDATE sliderpos SET last_chart == '{value}' ")
with conn:
c.execute("SELECT * FROM preferences")
prefs = c.fetchone()
heading = prefs[1]
max_requests = prefs[2]
scan_frequency = prefs[3]
interval = prefs[4]
factor0 = prefs[5]
factor1 = prefs[6]
factor2 = prefs[7]
factor3 = prefs[8]
factor4 = prefs[9]
factor5 = prefs[10]
factor6 = prefs[11]
name0 = prefs[12]
name1 = prefs[13]
name2 = prefs[14]
name3 = prefs[15]
name4 = prefs[16]
name5 = prefs[17]
name6 = prefs[18]
xpoints = prefs[19]
max0 = prefs[20]
max1 = prefs[21]
max2 = prefs[22]
max3 = prefs[23]
max4 = prefs[24]
max5 = prefs[25]
max6 = prefs[26]
# Pull down selection options
if value == 'AIN0':
i = 1
ymax = 6 * factor0
name = name0
color = 'red'
elif value == 'AIN1':
i = 2
ymax = 6 * factor1
name = name1
color = 'green'
elif value == 'AIN2':
i = 3
ymax = 6 * factor2
name = name2
color = 'orange'
elif value == 'AIN3':
i = 4
ymax = 6 * factor3
name = name3
color = 'blue'
elif value == 'AIN4':
i = 5
ymax = 6 * factor4
name = name4
color = 'purple'
if i == None:
return {'data':[],'layout':[]}
else:
state = ut.if_recording(c)
table_name = "dac_readings" if state == True else "temp_readings"
with conn:
c.execute("SELECT * FROM run_number ORDER BY run_id DESC LIMIT 1")
run = c.fetchone()
lastid = run[0]
time_start = run[1]
if state == False:
lastid = 1
if (value <= 'AIN3'):
c.execute(f"SELECT time, ain0, ain1, ain2, ain3 FROM {table_name} WHERE run_id = {str(lastid)} ORDER BY time DESC LIMIT {xpoints}")
readings = c.fetchall()
for d in readings:
data_dict['x'].append((d[0] - time_start)/1000000)
data_dict['y'].append(d[i])
if (value =='AIN4'):
if state == False:
lastid = 1
c.execute(f"""
SELECT time, ((ain4 - LAG (ain4, 100) OVER (ORDER BY time)) / (time - LAG (time, 100) OVER (ORDER BY time)))*1000000 AS cps
FROM {table_name} WHERE run_id = {lastid} ORDER BY TIME DESC LIMIT {xpoints};""")
readings = c.fetchall()
for d in readings:
data_dict['x'].append((d[0]- time_start)/1000000)
data_dict['y'].append(d[1])
x_data = data_dict.get('x')
y_data = data_dict.get('y')
traces = []
traces.append(
go.Scattergl(
x = x_data,
y = y_data,
name = name,
marker = dict(color = color, size = 3),
mode = 'lines+markers'
)
)
#Layout object defines the style of the graph features, eg axies titles etc
layout = go.Layout(
margin=go.layout.Margin(
l=50,r=30, b=30, t=30),
title = "",
height = 700,
showlegend=False,
xaxis = dict(title = 'time', visible = True),
yaxis=dict(title = name, visible = True, autorange = True)#, range = [0,ymax])
)
#The fig object is returned to place the placeholder graph.
fig = {'data':traces,'layout':layout}
return fig