Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -39,7 +39,8 @@ def download_and_load(file_id):
|
|
| 39 |
local_file_path = f'{file_id}.xlsx'
|
| 40 |
with open(local_file_path, 'wb') as file:
|
| 41 |
file.write(response.content)
|
| 42 |
-
return pd.read_excel(local_file_path, header=0)
|
|
|
|
| 43 |
|
| 44 |
inventory = download_and_load(inventory_file_id)
|
| 45 |
fixed_columns = ['Part Number', 'Size', 'Description']
|
|
@@ -108,8 +109,11 @@ def update_inventory_and_reservation(preferred, search_by, action, part_number=N
|
|
| 108 |
job_number = str(job_number) # Ensure job number is a string
|
| 109 |
reservation['Job Number'] = reservation['Job Number'].astype(str) # Convert job number column to string
|
| 110 |
|
| 111 |
-
|
| 112 |
-
|
|
|
|
|
|
|
|
|
|
| 113 |
if not reservation_data.empty:
|
| 114 |
reserved_length = reservation_data['Reserved'].values[0]
|
| 115 |
if used_length > reserved_length:
|
|
@@ -120,10 +124,17 @@ def update_inventory_and_reservation(preferred, search_by, action, part_number=N
|
|
| 120 |
reservation = reservation.drop(reservation_data.index[0])
|
| 121 |
|
| 122 |
# Update quantity for all relevant rows
|
| 123 |
-
|
|
|
|
|
|
|
|
|
|
| 124 |
|
| 125 |
else:
|
| 126 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
if not available_data.empty:
|
| 128 |
available_length = available_data['Available'].values[0]
|
| 129 |
if used_length > available_length:
|
|
@@ -134,7 +145,11 @@ def update_inventory_and_reservation(preferred, search_by, action, part_number=N
|
|
| 134 |
|
| 135 |
action_description = f"Consumed {used_length} inches from {from_length} inch sheet"
|
| 136 |
else:
|
| 137 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
if not available_data.empty:
|
| 139 |
available_length = available_data['Available'].values[0]
|
| 140 |
if used_length > available_length:
|
|
@@ -207,10 +222,11 @@ def update_inventory_and_reservation(preferred, search_by, action, part_number=N
|
|
| 207 |
else:
|
| 208 |
return "Length not found."
|
| 209 |
|
| 210 |
-
|
| 211 |
updated_inventory_path = "updated_inventory.xlsx"
|
| 212 |
updated_reservation_path = "updated_reservation.xlsx"
|
| 213 |
updated_activity_log_path = "updated_activity_log.xlsx"
|
|
|
|
| 214 |
|
| 215 |
inventory.to_excel(updated_inventory_path, index=False)
|
| 216 |
reservation.to_excel(updated_reservation_path, index=False)
|
|
@@ -257,6 +273,7 @@ def main(preferred, search_by, action, part_number=None, size=None, description=
|
|
| 257 |
message = update_inventory_and_reservation(preferred, search_by, action, part_number, size, description, from_length, used_length, new_length, count, job_number)
|
| 258 |
return message
|
| 259 |
|
|
|
|
| 260 |
with gr.Blocks() as demo:
|
| 261 |
preferred = gr.Radio(choices=["Yes", "No"], label="Preferred Material")
|
| 262 |
search_by = gr.Radio(choices=["Part Number", "Size and Description"], label="Search By")
|
|
@@ -276,7 +293,6 @@ with gr.Blocks() as demo:
|
|
| 276 |
def update_visibility(search_by):
|
| 277 |
return gr.update(visible=search_by == "Part Number"), gr.update(visible=search_by == "Size and Description"), gr.update(visible=search_by == "Size and Description")
|
| 278 |
|
| 279 |
-
|
| 280 |
def update_options(preferred, search_by):
|
| 281 |
options = get_options(preferred, search_by)
|
| 282 |
if search_by == "Part Number":
|
|
@@ -307,29 +323,40 @@ with gr.Blocks() as demo:
|
|
| 307 |
part_data = inventory[(inventory['Part Number'] == part_number) & (inventory['Preferred'] == preferred)]
|
| 308 |
else:
|
| 309 |
part_data = inventory[(inventory['Size'] == size) & (inventory['Description'] == description) & (inventory['Preferred'] == preferred)]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 310 |
lengths = part_data[[col for col in inventory.columns if 'QTY ' in col]].values[0].tolist()
|
| 311 |
lengths = [length for length in lengths if not pd.isna(length)]
|
| 312 |
return gr.update(choices=lengths, visible=bool(lengths))
|
| 313 |
-
|
| 314 |
|
| 315 |
# Modify update_reserved_lengths to reload sheets before updating reserved lengths
|
| 316 |
-
def update_reserved_lengths(job_number, part_number, from_length):
|
| 317 |
job_number = str(job_number) # Ensure job number is a string
|
| 318 |
reservation['Job Number'] = reservation['Job Number'].astype(str) # Convert job number column to string
|
| 319 |
|
| 320 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 321 |
if not reservation_data.empty:
|
| 322 |
reserved_lengths = reservation_data['Reserved'].tolist() # Get all reserved lengths as a list
|
| 323 |
reserved_lengths_str = ", ".join(map(str, reserved_lengths)) # Convert list to comma-separated string
|
| 324 |
-
return gr.update(visible=True), f"Reserved
|
| 325 |
else:
|
| 326 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 327 |
if not available_data.empty:
|
| 328 |
available_length = available_data['Available'].values[0] # Show maximum available length from a single entry
|
| 329 |
return gr.update(visible=True), f"Maximum available length = {available_length}"
|
| 330 |
else:
|
| 331 |
return gr.update(visible=True), "No matching reservation or available length found."
|
| 332 |
-
|
| 333 |
search_by.change(update_visibility, inputs=[search_by], outputs=[part_number, size, description])
|
| 334 |
preferred.change(update_options, inputs=[preferred, search_by], outputs=[part_number, size, description])
|
| 335 |
search_by.change(update_options, inputs=[preferred, search_by], outputs=[part_number, size, description])
|
|
@@ -339,7 +366,7 @@ with gr.Blocks() as demo:
|
|
| 339 |
description.change(update_lengths, inputs=[preferred, search_by, part_number, size, description], outputs=from_length)
|
| 340 |
|
| 341 |
from_length.change(lambda x: (gr.update(visible=True), gr.update(visible=False)), inputs=[from_length], outputs=[job_number, used_length])
|
| 342 |
-
job_number.change(update_reserved_lengths, inputs=[job_number, part_number, from_length], outputs=[used_length, output_message])
|
| 343 |
|
| 344 |
def show_length_inputs(action):
|
| 345 |
if action == "Add":
|
|
@@ -375,6 +402,8 @@ with gr.Blocks() as demo:
|
|
| 375 |
gr.update(visible=False) # job_number
|
| 376 |
)
|
| 377 |
|
|
|
|
|
|
|
| 378 |
action.change(show_length_inputs, inputs=[action], outputs=[from_length, used_length, new_length, count, job_number])
|
| 379 |
|
| 380 |
# Rest of the code to define the behavior of submit and confirm buttons
|
|
|
|
| 39 |
local_file_path = f'{file_id}.xlsx'
|
| 40 |
with open(local_file_path, 'wb') as file:
|
| 41 |
file.write(response.content)
|
| 42 |
+
return pd.read_excel(local_file_path, engine='openpyxl', header=0)
|
| 43 |
+
|
| 44 |
|
| 45 |
inventory = download_and_load(inventory_file_id)
|
| 46 |
fixed_columns = ['Part Number', 'Size', 'Description']
|
|
|
|
| 109 |
job_number = str(job_number) # Ensure job number is a string
|
| 110 |
reservation['Job Number'] = reservation['Job Number'].astype(str) # Convert job number column to string
|
| 111 |
|
| 112 |
+
if search_by == "Part Number":
|
| 113 |
+
reservation_data = reservation[(reservation['Job Number'] == job_number) & (reservation['Part Number'] == part_number) & (reservation['Quantity'] == from_length)]
|
| 114 |
+
else:
|
| 115 |
+
reservation_data = reservation[(reservation['Job Number'] == job_number) & (reservation['Size'] == size) & (reservation['Description'] == description) & (reservation['Quantity'] == from_length)]
|
| 116 |
+
|
| 117 |
if not reservation_data.empty:
|
| 118 |
reserved_length = reservation_data['Reserved'].values[0]
|
| 119 |
if used_length > reserved_length:
|
|
|
|
| 124 |
reservation = reservation.drop(reservation_data.index[0])
|
| 125 |
|
| 126 |
# Update quantity for all relevant rows
|
| 127 |
+
if search_by == "Part Number":
|
| 128 |
+
reservation.loc[reservation['Part Number'] == part_number, 'Quantity'] -= used_length
|
| 129 |
+
else:
|
| 130 |
+
reservation.loc[(reservation['Size'] == size) & (reservation['Description'] == description), 'Quantity'] -= used_length
|
| 131 |
|
| 132 |
else:
|
| 133 |
+
if search_by == "Part Number":
|
| 134 |
+
available_data = reservation[(reservation['Part Number'] == part_number) & (reservation['Quantity'] == from_length)]
|
| 135 |
+
else:
|
| 136 |
+
available_data = reservation[(reservation['Size'] == size) & (reservation['Description'] == description) & (reservation['Quantity'] == from_length)]
|
| 137 |
+
|
| 138 |
if not available_data.empty:
|
| 139 |
available_length = available_data['Available'].values[0]
|
| 140 |
if used_length > available_length:
|
|
|
|
| 145 |
|
| 146 |
action_description = f"Consumed {used_length} inches from {from_length} inch sheet"
|
| 147 |
else:
|
| 148 |
+
if search_by == "Part Number":
|
| 149 |
+
available_data = reservation[(reservation['Part Number'] == part_number) & (reservation['Quantity'] == from_length)]
|
| 150 |
+
else:
|
| 151 |
+
available_data = reservation[(reservation['Size'] == size) & (reservation['Description'] == description) & (reservation['Quantity'] == from_length)]
|
| 152 |
+
|
| 153 |
if not available_data.empty:
|
| 154 |
available_length = available_data['Available'].values[0]
|
| 155 |
if used_length > available_length:
|
|
|
|
| 222 |
else:
|
| 223 |
return "Length not found."
|
| 224 |
|
| 225 |
+
|
| 226 |
updated_inventory_path = "updated_inventory.xlsx"
|
| 227 |
updated_reservation_path = "updated_reservation.xlsx"
|
| 228 |
updated_activity_log_path = "updated_activity_log.xlsx"
|
| 229 |
+
|
| 230 |
|
| 231 |
inventory.to_excel(updated_inventory_path, index=False)
|
| 232 |
reservation.to_excel(updated_reservation_path, index=False)
|
|
|
|
| 273 |
message = update_inventory_and_reservation(preferred, search_by, action, part_number, size, description, from_length, used_length, new_length, count, job_number)
|
| 274 |
return message
|
| 275 |
|
| 276 |
+
|
| 277 |
with gr.Blocks() as demo:
|
| 278 |
preferred = gr.Radio(choices=["Yes", "No"], label="Preferred Material")
|
| 279 |
search_by = gr.Radio(choices=["Part Number", "Size and Description"], label="Search By")
|
|
|
|
| 293 |
def update_visibility(search_by):
|
| 294 |
return gr.update(visible=search_by == "Part Number"), gr.update(visible=search_by == "Size and Description"), gr.update(visible=search_by == "Size and Description")
|
| 295 |
|
|
|
|
| 296 |
def update_options(preferred, search_by):
|
| 297 |
options = get_options(preferred, search_by)
|
| 298 |
if search_by == "Part Number":
|
|
|
|
| 323 |
part_data = inventory[(inventory['Part Number'] == part_number) & (inventory['Preferred'] == preferred)]
|
| 324 |
else:
|
| 325 |
part_data = inventory[(inventory['Size'] == size) & (inventory['Description'] == description) & (inventory['Preferred'] == preferred)]
|
| 326 |
+
|
| 327 |
+
if part_data.empty:
|
| 328 |
+
return gr.update(choices=[], visible=False) # No data found
|
| 329 |
+
|
| 330 |
lengths = part_data[[col for col in inventory.columns if 'QTY ' in col]].values[0].tolist()
|
| 331 |
lengths = [length for length in lengths if not pd.isna(length)]
|
| 332 |
return gr.update(choices=lengths, visible=bool(lengths))
|
|
|
|
| 333 |
|
| 334 |
# Modify update_reserved_lengths to reload sheets before updating reserved lengths
|
| 335 |
+
def update_reserved_lengths(job_number, part_number=None, size=None, description=None, from_length=None):
|
| 336 |
job_number = str(job_number) # Ensure job number is a string
|
| 337 |
reservation['Job Number'] = reservation['Job Number'].astype(str) # Convert job number column to string
|
| 338 |
|
| 339 |
+
if part_number:
|
| 340 |
+
reservation_data = reservation[(reservation['Job Number'] == job_number) & (reservation['Part Number'] == part_number) & (reservation['Quantity'] == from_length)]
|
| 341 |
+
else:
|
| 342 |
+
reservation_data = reservation[(reservation['Job Number'] == job_number) & (reservation['Size'] == size) & (reservation['Description'] == description) & (reservation['Quantity'] == from_length)]
|
| 343 |
+
|
| 344 |
if not reservation_data.empty:
|
| 345 |
reserved_lengths = reservation_data['Reserved'].tolist() # Get all reserved lengths as a list
|
| 346 |
reserved_lengths_str = ", ".join(map(str, reserved_lengths)) # Convert list to comma-separated string
|
| 347 |
+
return gr.update(visible=True), f"Reserved lengths = {reserved_lengths_str}"
|
| 348 |
else:
|
| 349 |
+
if part_number:
|
| 350 |
+
available_data = reservation[(reservation['Part Number'] == part_number) & (reservation['Quantity'] == from_length)]
|
| 351 |
+
else:
|
| 352 |
+
available_data = reservation[(reservation['Size'] == size) & (reservation['Description'] == description) & (reservation['Quantity'] == from_length)]
|
| 353 |
+
|
| 354 |
if not available_data.empty:
|
| 355 |
available_length = available_data['Available'].values[0] # Show maximum available length from a single entry
|
| 356 |
return gr.update(visible=True), f"Maximum available length = {available_length}"
|
| 357 |
else:
|
| 358 |
return gr.update(visible=True), "No matching reservation or available length found."
|
| 359 |
+
|
| 360 |
search_by.change(update_visibility, inputs=[search_by], outputs=[part_number, size, description])
|
| 361 |
preferred.change(update_options, inputs=[preferred, search_by], outputs=[part_number, size, description])
|
| 362 |
search_by.change(update_options, inputs=[preferred, search_by], outputs=[part_number, size, description])
|
|
|
|
| 366 |
description.change(update_lengths, inputs=[preferred, search_by, part_number, size, description], outputs=from_length)
|
| 367 |
|
| 368 |
from_length.change(lambda x: (gr.update(visible=True), gr.update(visible=False)), inputs=[from_length], outputs=[job_number, used_length])
|
| 369 |
+
job_number.change(update_reserved_lengths, inputs=[job_number, part_number, size, description, from_length], outputs=[used_length, output_message])
|
| 370 |
|
| 371 |
def show_length_inputs(action):
|
| 372 |
if action == "Add":
|
|
|
|
| 402 |
gr.update(visible=False) # job_number
|
| 403 |
)
|
| 404 |
|
| 405 |
+
|
| 406 |
+
|
| 407 |
action.change(show_length_inputs, inputs=[action], outputs=[from_length, used_length, new_length, count, job_number])
|
| 408 |
|
| 409 |
# Rest of the code to define the behavior of submit and confirm buttons
|