Update app.py
Browse files
app.py
CHANGED
|
@@ -115,17 +115,24 @@ def add_quantity_columns(df):
|
|
| 115 |
new_column = f'QTY {last_qty_column + 1}'
|
| 116 |
df.insert(df.columns.get_loc('Total'), new_column, None)
|
| 117 |
|
| 118 |
-
def update_inventory_and_reservation(preferred, search_by, action, part_number=None, size=None, description=None, from_length=None, used_length=None, new_length=None, count=None, job_number=None):
|
| 119 |
global reservation # Ensure reservation is accessible
|
| 120 |
global activity_log
|
| 121 |
|
| 122 |
from_length = fraction_to_decimal(from_length) if from_length else None
|
| 123 |
used_length = fraction_to_decimal(used_length) if used_length else None
|
| 124 |
new_length_decimal = fraction_to_decimal(new_length) if new_length else None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
new_length_formatted = decimal_to_fraction(new_length_decimal) if new_length_decimal else None
|
| 126 |
|
| 127 |
print(f"Converted values in update - from_length: {from_length}, used_length: {used_length}, new_length: {new_length_formatted}")
|
| 128 |
|
|
|
|
|
|
|
| 129 |
if preferred:
|
| 130 |
inventory_to_use = inventory[inventory['Preferred'] == "Yes"]
|
| 131 |
else:
|
|
@@ -145,7 +152,6 @@ def update_inventory_and_reservation(preferred, search_by, action, part_number=N
|
|
| 145 |
|
| 146 |
action_description = ""
|
| 147 |
|
| 148 |
-
|
| 149 |
if action == 'Consume':
|
| 150 |
if job_number:
|
| 151 |
job_number = str(job_number) # Ensure job number is a string
|
|
@@ -245,7 +251,7 @@ def update_inventory_and_reservation(preferred, search_by, action, part_number=N
|
|
| 245 |
|
| 246 |
if added:
|
| 247 |
inventory.at[inventory_index, "Total"] = inventory.loc[inventory_index, [col for col in inventory.columns if 'QTY ' in col]].sum()
|
| 248 |
-
action_description = f"Added {count} {new_length_formatted}
|
| 249 |
elif action == 'Scrap':
|
| 250 |
column_to_update = None
|
| 251 |
for column in [col for col in inventory.columns if 'QTY ' in col]:
|
|
@@ -267,11 +273,12 @@ def update_inventory_and_reservation(preferred, search_by, action, part_number=N
|
|
| 267 |
else:
|
| 268 |
return "Length not found."
|
| 269 |
|
| 270 |
-
|
| 271 |
updated_inventory_path = "updated_inventory.xlsx"
|
| 272 |
updated_reservation_path = "updated_reservation.xlsx"
|
| 273 |
updated_activity_log_path = "updated_activity_log.xlsx"
|
| 274 |
|
|
|
|
| 275 |
inventory.to_excel(updated_inventory_path, index=False)
|
| 276 |
reservation.to_excel(updated_reservation_path, index=False)
|
| 277 |
|
|
@@ -312,12 +319,18 @@ def update_inventory_and_reservation(preferred, search_by, action, part_number=N
|
|
| 312 |
else:
|
| 313 |
return (f"Failed to upload inventory file. Inventory upload status: {inventory_status_code}, response: {inventory_response_text}.")
|
| 314 |
|
| 315 |
-
def main(preferred, search_by, action, part_number, size, description, from_length, job_number, used_length, new_length, count):
|
| 316 |
from_length = fraction_to_decimal(from_length) if from_length else None
|
| 317 |
used_length = fraction_to_decimal(used_length) if used_length else None
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 321 |
|
| 322 |
if search_by == "Size and Description" and size and description:
|
| 323 |
part_data = inventory[(inventory["Size"] == size) & (inventory["Description"] == description)]
|
|
@@ -329,14 +342,16 @@ def main(preferred, search_by, action, part_number, size, description, from_leng
|
|
| 329 |
size = part_data["Size"].values[0]
|
| 330 |
description = part_data["Description"].values[0]
|
| 331 |
|
| 332 |
-
if
|
| 333 |
return "Error: New length is not provided or invalid."
|
| 334 |
|
| 335 |
-
message = update_inventory_and_reservation(preferred, search_by, action, part_number, size, description, from_length, used_length,
|
| 336 |
print(f"Update message: {message}")
|
| 337 |
return message
|
| 338 |
|
| 339 |
|
|
|
|
|
|
|
| 340 |
def reload_sheets():
|
| 341 |
global inventory, reservation, activity_log, access_token, headers
|
| 342 |
|
|
@@ -363,13 +378,11 @@ with gr.Blocks() as demo:
|
|
| 363 |
used_length = gr.Textbox(label="Used Length (inches)", interactive=True, visible=False)
|
| 364 |
new_length = gr.Textbox(label="New Length (inches)", interactive=True, visible=True) # Set as Textbox to accept strings like "25 3/8"
|
| 365 |
count = gr.Number(label="Count", interactive=True, visible=False)
|
|
|
|
| 366 |
output_message = gr.Textbox(label="Output Message")
|
| 367 |
confirmation_html = gr.HTML("")
|
| 368 |
confirm_button = gr.Button("Confirm", visible=False)
|
| 369 |
|
| 370 |
-
# ... rest of the code remains unchanged ...
|
| 371 |
-
|
| 372 |
-
|
| 373 |
def update_visibility(search_by):
|
| 374 |
return gr.update(visible=search_by == "Part Number"), gr.update(visible=search_by == "Size and Description"), gr.update(visible=search_by == "Size and Description")
|
| 375 |
|
|
@@ -382,6 +395,9 @@ with gr.Blocks() as demo:
|
|
| 382 |
sizes, descriptions = options
|
| 383 |
return gr.update(visible=False), gr.update(choices=sizes, visible=True), gr.update(choices=descriptions, visible=True)
|
| 384 |
|
|
|
|
|
|
|
|
|
|
| 385 |
# Function to reload the sheets and refresh the access token
|
| 386 |
def reload_sheets():
|
| 387 |
global inventory, reservation, activity_log, access_token, headers
|
|
@@ -463,7 +479,8 @@ with gr.Blocks() as demo:
|
|
| 463 |
gr.update(visible=False), # used_length
|
| 464 |
gr.update(visible=True), # new_length
|
| 465 |
gr.update(visible=True), # count
|
| 466 |
-
gr.update(visible=False) # job_number
|
|
|
|
| 467 |
)
|
| 468 |
elif action == "Consume":
|
| 469 |
return (
|
|
@@ -471,7 +488,8 @@ with gr.Blocks() as demo:
|
|
| 471 |
gr.update(visible=True), # used_length
|
| 472 |
gr.update(visible=False), # new_length
|
| 473 |
gr.update(visible=False), # count
|
| 474 |
-
gr.update(visible=True) # job_number
|
|
|
|
| 475 |
)
|
| 476 |
elif action == "Scrap":
|
| 477 |
return (
|
|
@@ -479,7 +497,8 @@ with gr.Blocks() as demo:
|
|
| 479 |
gr.update(visible=False), # used_length
|
| 480 |
gr.update(visible=False), # new_length
|
| 481 |
gr.update(visible=False), # count
|
| 482 |
-
gr.update(visible=False) # job_number
|
|
|
|
| 483 |
)
|
| 484 |
else:
|
| 485 |
return (
|
|
@@ -487,46 +506,58 @@ with gr.Blocks() as demo:
|
|
| 487 |
gr.update(visible=False), # used_length
|
| 488 |
gr.update(visible=False), # new_length
|
| 489 |
gr.update(visible=False), # count
|
| 490 |
-
gr.update(visible=False) # job_number
|
|
|
|
| 491 |
)
|
| 492 |
|
| 493 |
-
action.change(show_length_inputs, inputs=[action], outputs=[from_length, used_length, new_length, count, job_number])
|
| 494 |
|
| 495 |
submit = gr.Button("Submit")
|
| 496 |
|
| 497 |
-
|
| 498 |
-
|
| 499 |
-
|
| 500 |
-
|
| 501 |
-
|
| 502 |
-
|
| 503 |
-
|
| 504 |
-
|
| 505 |
-
|
| 506 |
-
|
| 507 |
-
|
| 508 |
-
|
| 509 |
-
|
| 510 |
-
|
| 511 |
-
|
| 512 |
-
|
| 513 |
-
|
| 514 |
-
|
| 515 |
-
|
| 516 |
-
|
| 517 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
submit.click(
|
| 522 |
fn=show_confirmation,
|
| 523 |
-
inputs=[preferred, search_by, action, part_number, size, description, from_length, job_number, used_length, new_length, count],
|
| 524 |
outputs=[confirmation_html, confirm_button]
|
| 525 |
)
|
| 526 |
|
| 527 |
confirm_button.click(
|
| 528 |
fn=main,
|
| 529 |
-
inputs=[preferred, search_by, action, part_number, size, description, from_length, job_number, used_length, new_length, count],
|
| 530 |
outputs=[output_message]
|
| 531 |
)
|
| 532 |
|
|
|
|
| 115 |
new_column = f'QTY {last_qty_column + 1}'
|
| 116 |
df.insert(df.columns.get_loc('Total'), new_column, None)
|
| 117 |
|
| 118 |
+
def update_inventory_and_reservation(preferred, search_by, action, part_number=None, size=None, description=None, from_length=None, used_length=None, new_length=None, count=None, job_number=None, unit=None):
|
| 119 |
global reservation # Ensure reservation is accessible
|
| 120 |
global activity_log
|
| 121 |
|
| 122 |
from_length = fraction_to_decimal(from_length) if from_length else None
|
| 123 |
used_length = fraction_to_decimal(used_length) if used_length else None
|
| 124 |
new_length_decimal = fraction_to_decimal(new_length) if new_length else None
|
| 125 |
+
|
| 126 |
+
# Convert to inches if unit is feet
|
| 127 |
+
if unit == "feet":
|
| 128 |
+
new_length_decimal *= 12
|
| 129 |
+
|
| 130 |
new_length_formatted = decimal_to_fraction(new_length_decimal) if new_length_decimal else None
|
| 131 |
|
| 132 |
print(f"Converted values in update - from_length: {from_length}, used_length: {used_length}, new_length: {new_length_formatted}")
|
| 133 |
|
| 134 |
+
# ... rest of the logic remains unchanged ...
|
| 135 |
+
|
| 136 |
if preferred:
|
| 137 |
inventory_to_use = inventory[inventory['Preferred'] == "Yes"]
|
| 138 |
else:
|
|
|
|
| 152 |
|
| 153 |
action_description = ""
|
| 154 |
|
|
|
|
| 155 |
if action == 'Consume':
|
| 156 |
if job_number:
|
| 157 |
job_number = str(job_number) # Ensure job number is a string
|
|
|
|
| 251 |
|
| 252 |
if added:
|
| 253 |
inventory.at[inventory_index, "Total"] = inventory.loc[inventory_index, [col for col in inventory.columns if 'QTY ' in col]].sum()
|
| 254 |
+
action_description = f"Added {count} {new_length_formatted} {unit} sheets"
|
| 255 |
elif action == 'Scrap':
|
| 256 |
column_to_update = None
|
| 257 |
for column in [col for col in inventory.columns if 'QTY ' in col]:
|
|
|
|
| 273 |
else:
|
| 274 |
return "Length not found."
|
| 275 |
|
| 276 |
+
# Save updated inventory and reservation to local files
|
| 277 |
updated_inventory_path = "updated_inventory.xlsx"
|
| 278 |
updated_reservation_path = "updated_reservation.xlsx"
|
| 279 |
updated_activity_log_path = "updated_activity_log.xlsx"
|
| 280 |
|
| 281 |
+
|
| 282 |
inventory.to_excel(updated_inventory_path, index=False)
|
| 283 |
reservation.to_excel(updated_reservation_path, index=False)
|
| 284 |
|
|
|
|
| 319 |
else:
|
| 320 |
return (f"Failed to upload inventory file. Inventory upload status: {inventory_status_code}, response: {inventory_response_text}.")
|
| 321 |
|
| 322 |
+
def main(preferred, search_by, action, part_number, size, description, from_length, job_number, used_length, new_length, count, unit):
|
| 323 |
from_length = fraction_to_decimal(from_length) if from_length else None
|
| 324 |
used_length = fraction_to_decimal(used_length) if used_length else None
|
| 325 |
+
new_length_decimal = fraction_to_decimal(new_length) if new_length else None
|
| 326 |
+
|
| 327 |
+
# Convert to inches if unit is feet
|
| 328 |
+
if unit == "feet":
|
| 329 |
+
new_length_decimal *= 12
|
| 330 |
+
|
| 331 |
+
new_length_formatted = decimal_to_fraction(new_length_decimal) if new_length_decimal else None
|
| 332 |
+
|
| 333 |
+
print(f"Converted values - from_length: {from_length}, used_length: {used_length}, new_length: {new_length_decimal}")
|
| 334 |
|
| 335 |
if search_by == "Size and Description" and size and description:
|
| 336 |
part_data = inventory[(inventory["Size"] == size) & (inventory["Description"] == description)]
|
|
|
|
| 342 |
size = part_data["Size"].values[0]
|
| 343 |
description = part_data["Description"].values[0]
|
| 344 |
|
| 345 |
+
if new_length_decimal is None and action == 'Add':
|
| 346 |
return "Error: New length is not provided or invalid."
|
| 347 |
|
| 348 |
+
message = update_inventory_and_reservation(preferred, search_by, action, part_number, size, description, from_length, used_length, new_length_decimal, count, job_number)
|
| 349 |
print(f"Update message: {message}")
|
| 350 |
return message
|
| 351 |
|
| 352 |
|
| 353 |
+
|
| 354 |
+
|
| 355 |
def reload_sheets():
|
| 356 |
global inventory, reservation, activity_log, access_token, headers
|
| 357 |
|
|
|
|
| 378 |
used_length = gr.Textbox(label="Used Length (inches)", interactive=True, visible=False)
|
| 379 |
new_length = gr.Textbox(label="New Length (inches)", interactive=True, visible=True) # Set as Textbox to accept strings like "25 3/8"
|
| 380 |
count = gr.Number(label="Count", interactive=True, visible=False)
|
| 381 |
+
unit = gr.Radio(choices=["inches", "feet"], label="Unit", visible=True) # Add unit selector
|
| 382 |
output_message = gr.Textbox(label="Output Message")
|
| 383 |
confirmation_html = gr.HTML("")
|
| 384 |
confirm_button = gr.Button("Confirm", visible=False)
|
| 385 |
|
|
|
|
|
|
|
|
|
|
| 386 |
def update_visibility(search_by):
|
| 387 |
return gr.update(visible=search_by == "Part Number"), gr.update(visible=search_by == "Size and Description"), gr.update(visible=search_by == "Size and Description")
|
| 388 |
|
|
|
|
| 395 |
sizes, descriptions = options
|
| 396 |
return gr.update(visible=False), gr.update(choices=sizes, visible=True), gr.update(choices=descriptions, visible=True)
|
| 397 |
|
| 398 |
+
|
| 399 |
+
|
| 400 |
+
|
| 401 |
# Function to reload the sheets and refresh the access token
|
| 402 |
def reload_sheets():
|
| 403 |
global inventory, reservation, activity_log, access_token, headers
|
|
|
|
| 479 |
gr.update(visible=False), # used_length
|
| 480 |
gr.update(visible=True), # new_length
|
| 481 |
gr.update(visible=True), # count
|
| 482 |
+
gr.update(visible=False), # job_number
|
| 483 |
+
gr.update(visible=True) # unit
|
| 484 |
)
|
| 485 |
elif action == "Consume":
|
| 486 |
return (
|
|
|
|
| 488 |
gr.update(visible=True), # used_length
|
| 489 |
gr.update(visible=False), # new_length
|
| 490 |
gr.update(visible=False), # count
|
| 491 |
+
gr.update(visible=True), # job_number
|
| 492 |
+
gr.update(visible=False) # unit
|
| 493 |
)
|
| 494 |
elif action == "Scrap":
|
| 495 |
return (
|
|
|
|
| 497 |
gr.update(visible=False), # used_length
|
| 498 |
gr.update(visible=False), # new_length
|
| 499 |
gr.update(visible=False), # count
|
| 500 |
+
gr.update(visible=False), # job_number
|
| 501 |
+
gr.update(visible=False) # unit
|
| 502 |
)
|
| 503 |
else:
|
| 504 |
return (
|
|
|
|
| 506 |
gr.update(visible=False), # used_length
|
| 507 |
gr.update(visible=False), # new_length
|
| 508 |
gr.update(visible=False), # count
|
| 509 |
+
gr.update(visible=False), # job_number
|
| 510 |
+
gr.update(visible=False) # unit
|
| 511 |
)
|
| 512 |
|
| 513 |
+
action.change(show_length_inputs, inputs=[action], outputs=[from_length, used_length, new_length, count, job_number, unit]) # Add unit to outputs
|
| 514 |
|
| 515 |
submit = gr.Button("Submit")
|
| 516 |
|
| 517 |
+
|
| 518 |
+
|
| 519 |
+
def show_confirmation(preferred, search_by, action, part_number, size, description, from_length, job_number, used_length, new_length, count, unit):
|
| 520 |
+
new_length_decimal = fraction_to_decimal(new_length) if new_length else None
|
| 521 |
+
|
| 522 |
+
if unit == "feet" and new_length_decimal:
|
| 523 |
+
new_length_decimal *= 12
|
| 524 |
+
|
| 525 |
+
new_length_formatted = decimal_to_fraction(new_length_decimal) if new_length_decimal else None
|
| 526 |
+
|
| 527 |
+
if search_by == "Size and Description" and size and description:
|
| 528 |
+
part_data = inventory[(inventory["Size"] == size) & (inventory["Description"] == description)]
|
| 529 |
+
if not part_data.empty:
|
| 530 |
+
part_number = part_data["Part Number"].values[0]
|
| 531 |
+
elif search_by == "Part Number" and part_number:
|
| 532 |
+
part_data = inventory[inventory["Part Number"] == part_number]
|
| 533 |
+
if not part_data.empty:
|
| 534 |
+
size = part_data["Size"].values[0]
|
| 535 |
+
description = part_data["Description"].values[0]
|
| 536 |
+
|
| 537 |
+
if action == "Consume":
|
| 538 |
+
message = f"Are you sure you want to consume {used_length} inches of {part_number} ({size} {description}) from a {from_length} inch piece for job number {job_number}?"
|
| 539 |
+
elif action == "Add":
|
| 540 |
+
message = f"Are you sure you want to add {count} pieces of {new_length_formatted} inches to {part_number} ({size} {description})?"
|
| 541 |
+
elif action == "Scrap":
|
| 542 |
+
message = f"Are you sure you want to scrap {from_length} inch pieces of {part_number} ({size} {description})?"
|
| 543 |
+
|
| 544 |
+
html_message = f"<div style='font-size: 30px;'>{message}</div>"
|
| 545 |
+
|
| 546 |
+
return html_message, gr.update(visible=True), part_number
|
| 547 |
+
|
| 548 |
+
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
submit.click(
|
| 553 |
fn=show_confirmation,
|
| 554 |
+
inputs=[preferred, search_by, action, part_number, size, description, from_length, job_number, used_length, new_length, count, unit], # Add unit to inputs
|
| 555 |
outputs=[confirmation_html, confirm_button]
|
| 556 |
)
|
| 557 |
|
| 558 |
confirm_button.click(
|
| 559 |
fn=main,
|
| 560 |
+
inputs=[preferred, search_by, action, part_number, size, description, from_length, job_number, used_length, new_length, count, unit], # Add unit to inputs
|
| 561 |
outputs=[output_message]
|
| 562 |
)
|
| 563 |
|