-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmissing_tube_info_app.py
296 lines (254 loc) · 10.6 KB
/
missing_tube_info_app.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import dash
from dash import html
from dash import dcc
from dash.exceptions import PreventUpdate
import dash_bootstrap_components as dbc
from dash.dependencies import Input, Output, ALL, State
import webbrowser
import csv
from datetime import datetime
from pathlib import Path
from itertools import chain
import pandas as pd
import requests
"""Global Variables"""
result_data_dir = ".\\Test Result Data\\"
"""Functions"""
def read_csvs(result_data_dir):
"""Goes through the last week of grossFINAL and tareFINAL csvs and checks if there is missing info"""
fifty_days = 95378.95542693138 * 50 # seconds
fifty_days_ago = datetime.now().timestamp() - fifty_days # seconds
table_rows = []
update_info = []
for path in chain(
Path(result_data_dir).rglob("*tareFINAL.csv"),
Path(result_data_dir).rglob("*grossFINAL.csv"),
):
file_creation_time = path.stat().st_mtime
filename = str(path).split("\\")[-1]
# Changed the key dependng on gross or tare file
if "tareFINAL.csv" in filename:
col_key = "Src2D"
elif "grossFINAL.csv" in filename:
col_key = "Tgt2D"
# Determines if the file was made within the last week
if file_creation_time > fifty_days_ago:
# To get barcode underneath
df = pd.read_csv(path)
df = df.append([""], ignore_index=True)
with open(path, newline="") as csvfile:
spamreader = csv.reader(csvfile, delimiter=",", quotechar="|")
# Goes through the rows of the CSV finds missing things
for i, row in enumerate(spamreader):
if row[2] == "NOREAD" or row[2] == "":
table_rows.append(
html.Tr(
[
html.Th(row[0]),
html.Th(row[1]),
html.Th("Barcode"),
html.Th(df.iloc[i][col_key]),
html.Th(
[
dcc.Input(
id={
"type": "input",
"index": "input"
+ filename
+ row[1]
+ "barcode",
},
type="text",
style={"text-align": "left"},
),
html.Button(
"Upload",
id={
"type": "button",
"index": filename
+ row[1]
+ "barcode",
},
style={"margin-left": "5px"},
),
]
),
]
)
)
update_info.append([filename, i, 2])
if row[3] == "":
table_rows.append(
html.Tr(
[
html.Th(row[0]),
html.Th(row[1]),
html.Th("Weight"),
html.Th(df.iloc[i]["Src2D"]),
html.Th(
[
dcc.Input(
id={
"type": "input",
"index": "input"
+ filename
+ row[1]
+ "weight",
},
type="text",
style={"text-align": "left"},
),
html.Button(
"Upload",
id={
"type": "button",
"index": filename
+ row[1]
+ "weight",
},
style={"margin-left": "5px"},
),
]
),
]
)
)
update_info.append([filename, i, 3])
return table_rows, update_info
def serve_layout():
"""Keeps table up to date on refresh"""
global table_rows
global update_info
global table_header
table_rows, update_info = read_csvs(result_data_dir)
table_header = [
html.Thead(
html.Tr(
[
html.Th("Rack"),
html.Th("Tube"),
html.Th("Missing Info"),
html.Th("Tube Barcode Below"),
html.Th("Input"),
]
)
)
]
table_body = [html.Tbody(table_rows)]
table = dbc.Table(
table_header + table_body, id="table", striped=True, bordered=True, hover=True
)
header = html.H1("Missing Tube info", id="test-output")
loader = dbc.Spinner(html.Div("", id="spinner"))
modal = html.Div(
dbc.Modal(
[
dbc.ModalHeader(dbc.ModalTitle("Fail")),
dbc.ModalBody("Unable to upload"),
dbc.ModalFooter(
dbc.Button("Close", id="close", className="ms-auto", n_clicks=0)
),
],
id="modal",
is_open=False,
)
)
return dbc.Container([header, table, loader, modal])
def change_csv(result_data_dir, filename, row_num, col_num, input):
"""Changes the csv: adds the 'input' variable to 'filename' in 'row_num','col_num'"""
# Read CSV
rows = []
with open(result_data_dir + filename, newline="") as csvfile:
spamreader = csv.reader(csvfile, delimiter=",", quotechar="|")
for row in spamreader:
rows.append(row)
csvfile.close()
# Update CSV
for j, row in enumerate(rows):
if j == row_num:
row[col_num] = input
# Write CSV
with open(result_data_dir + filename, "w", newline="") as csvfile:
spamwriter = csv.writer(
csvfile, delimiter=",", quotechar="|", quoting=csv.QUOTE_MINIMAL
)
spamwriter.writerows(rows)
csvfile.close()
"""App creation"""
app = dash.Dash(__name__)
# Serving locally
app.css.config.serve_locally = True
app.scripts.config.serve_locally = True
# Creating app
app.layout = serve_layout
"""Callbacks"""
@app.callback(
Output("table", "children"),
Output("test-output", "children"),
Output("spinner", "children"),
Output("modal", "is_open"),
Input({"type": "button", "index": ALL}, "n_clicks"),
Input("close", "n_clicks"),
State({"type": "input", "index": ALL}, "value"),
)
def on_click(n, close_modal, missing_info):
for i, clicks in enumerate(n):
if clicks != None:
filename = update_info[i][0]
row_num = update_info[i][1]
col_num = update_info[i][2]
input = missing_info[i]
# Checking input
if input is None:
raise PreventUpdate
else:
# Adding input to CSV
change_csv(result_data_dir, filename, row_num, col_num, input)
# Upload CSV and check if successful
with open(result_data_dir + filename, "r") as file:
file_string = file.read()
if "tareFINAL" in filename:
try:
resp = requests.post(
"path/to/database",
data=file_string,
headers={'processData': 'false', 'Content-Type': 'text/plain'}
)
except:
change_csv(result_data_dir, filename, row_num, col_num, '')
elif "grossFINAL" in filename:
try:
resp = requests.post(
"path/to/database",
data=file_string,
headers={'processData': 'false', 'Content-Type': 'text/plain'}
)
except:
change_csv(result_data_dir, filename, row_num, col_num, '')
# Update Table
table_rows, _ = read_csvs(result_data_dir)
if len(table_rows) == len(update_info):
return (
table_header + [html.Tbody(table_rows)],
"Missing Tube Info",
"",
True,
)
else:
update_info.pop(i)
return (
table_header + [html.Tbody(table_rows)],
"Missing Tube Info",
"",
False,
)
else:
continue
# Closes modal
if close_modal:
table_rows, _ = read_csvs(result_data_dir)
return table_header + [html.Tbody(table_rows)], "Missing Tube Info", "", False
raise PreventUpdate
if __name__ == "__main__":
webbrowser.open("http://127.0.0.1:8050/")
app.run_server(debug=False)