-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtab4.py
More file actions
85 lines (72 loc) · 2.52 KB
/
tab4.py
File metadata and controls
85 lines (72 loc) · 2.52 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
import dash
from dash import dcc, html
from dash.dependencies import Input, Output, State
import sqlite3 as sql
from datetime import datetime
from server import app
# Fetch the last run number
def fetch_last_run():
conn = sql.connect("labjackdb.db")
c = conn.cursor()
c.execute("SELECT * FROM run_number ORDER BY run_id DESC LIMIT 1")
lastrun = (c.fetchone()[0]) - 1
conn.close()
return lastrun
# Fetch preferences
def fetch_preferences():
conn = sql.connect("labjackdb.db")
c = conn.cursor()
c.execute("SELECT * FROM preferences")
prefs = c.fetchone()
conn.close()
return prefs
# Fetch readings for a specific run
def fetch_readings(run_id):
conn = sql.connect("labjackdb.db")
c = conn.cursor()
c.execute(f"SELECT * FROM dac_readings WHERE run_id = {run_id} ORDER BY time")
readings = c.fetchall()
conn.close()
return readings
# Fetch run information
def fetch_run_info(run_id):
conn = sql.connect("labjackdb.db")
c = conn.cursor()
c.execute(f"SELECT * FROM run_number WHERE run_id = {run_id}")
run = c.fetchone()
conn.close()
return run
def tab4():
lastrun = fetch_last_run()
return html.Div([
html.P('Enter filename'),
html.Div(dcc.Input(id='input_filename', value='data.csv', type='text')),
html.Div(id='filename_output'),
html.P(f'Enter run to export, last run was: {lastrun}'),
html.Div(dcc.Input(id='input_run', value=0, type='number', debounce=True)),
html.Div('Run: ', id='data_output'),
html.Button('Save', id='button', value='n_clicks'),
html.Div(id='button_output', style={'display': 'none'}),
], style={'padding': '50px', 'textAlign': 'center'})
@app.callback(
Output('data_output', 'children'),
Output('filename_output', 'children'),
Output('button_output', 'children'),
Input('button', 'n_clicks'),
State('input_run', 'value'),
State('input_filename', 'value')
)
def exportcsv(n_clicks, run_id, path):
if run_id <= 0:
return 'Invalid run ID', '', n_clicks
run_info = fetch_run_info(run_id)
prefs = fetch_preferences()
readings = fetch_readings(run_id)
header_names = ['run', 'time', prefs[12], prefs[13], prefs[14], prefs[15], prefs[16]]
header = ','.join(header_names) + '\n'
with open(path, 'w') as fd:
fd.write(header)
for reading in readings:
row = ','.join(map(str, reading[:8])) + '\n'
fd.write(row)
return f'Run: {run_id}', f'Filename: {path}', n_clicks