-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtab2.py
More file actions
136 lines (118 loc) · 4.05 KB
/
tab2.py
File metadata and controls
136 lines (118 loc) · 4.05 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
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
import dash
from dash import dcc, html
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 utilities as ut
def fetch_preferences():
conn = sql.connect("labjackdb.db")
c = conn.cursor()
c.execute("SELECT * FROM preferences")
prefs = c.fetchone()
conn.close()
return prefs
def fetch_slider_positions():
conn = sql.connect("labjackdb.db")
c = conn.cursor()
c.execute("SELECT * FROM sliderpos")
spos = c.fetchone()
conn.close()
return spos
def fetch_run_number():
conn = sql.connect("labjackdb.db")
c = conn.cursor()
c.execute("SELECT * FROM run_number ORDER BY run_id DESC LIMIT 1")
run = c.fetchone()
conn.close()
return run
def update_last_chart(value):
conn = sql.connect("labjackdb.db")
c = conn.cursor()
c.execute(f"UPDATE sliderpos SET last_chart = '{value}'")
conn.commit()
conn.close()
def tab2():
prefs = fetch_preferences()
interval = prefs[4]
names = prefs[12:17]
xpoints = prefs[19]
chart = fetch_slider_positions()[3]
return html.Div([
dcc.Dropdown(
id='dropdown',
options=[{'label': name, 'value': f'AIN{i}'} for i, name in enumerate(names)],
value=chart,
style={'textAlign': 'center'}
),
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'})
@app.callback(
Output('graph', 'figure'),
Input('interval', 'n_intervals'),
State('dropdown', 'value')
)
def interval_callback(n_intervals, value):
conn = sql.connect("labjackdb.db")
c = conn.cursor()
update_last_chart(value)
prefs = fetch_preferences()
factors = prefs[5:12]
names = prefs[12:19]
xpoints = prefs[19]
i, ymax, name, color = None, None, None, None
if value == 'AIN0':
i, ymax, name, color = 1, 6 * factors[0], names[0], 'red'
elif value == 'AIN1':
i, ymax, name, color = 2, 6 * factors[1], names[1], 'green'
elif value == 'AIN2':
i, ymax, name, color = 3, 6 * factors[2], names[2], 'orange'
elif value == 'AIN3':
i, ymax, name, color = 4, 6 * factors[3], names[3], 'blue'
elif value == 'AIN4':
i, ymax, name, color = 5, 6 * factors[4], names[4], 'purple'
if i is None:
return {'data': [], 'layout': []}
state = ut.if_recording(c)
table_name = "dac_readings" if state else "temp_readings"
run = fetch_run_number()
lastid, time_start = run[0], run[1]
if not state:
lastid = 1
data_dict = {'x': [], 'y': []}
if value <= 'AIN3':
c.execute(f"SELECT time, ain0, ain1, ain2, ain3 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[i])
elif value == 'AIN4':
c.execute(f"""
SELECT time, ((ain4 - LAG (ain4, 30) OVER (ORDER BY time)) / (time - LAG (time, 30) 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])
conn.close()
traces = [go.Scattergl(
x=data_dict['x'],
y=data_dict['y'],
name=name,
marker=dict(color=color, size=3),
mode='lines+markers'
)]
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)
)
return {'data': traces, 'layout': layout}