Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -347,7 +347,6 @@ def _internal_update_menu(df):
|
|
| 347 |
note = str(row[8]).strip() if pd.notna(row[8]) else ""
|
| 348 |
|
| 349 |
if item:
|
| 350 |
-
# 改為 Append 到 List,完美解決同名品項會被覆蓋的問題
|
| 351 |
new_menu.append({
|
| 352 |
"name": item, "price": price, "cost": cost,
|
| 353 |
"enable": enable, "level": target_lvl, "seq": seq,
|
|
@@ -357,6 +356,11 @@ def _internal_update_menu(df):
|
|
| 357 |
if not new_menu:
|
| 358 |
return False
|
| 359 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 360 |
MENU.clear()
|
| 361 |
MENU.extend(new_menu)
|
| 362 |
save_menu_to_file()
|
|
@@ -383,10 +387,55 @@ def _internal_update_members(df):
|
|
| 383 |
def update_menu(df):
|
| 384 |
success = _internal_update_menu(df)
|
| 385 |
if not success:
|
| 386 |
-
return gr.update(value="❌ 錯誤:菜單不能為空!"), gr.update(), gr.skip()
|
| 387 |
|
| 388 |
new_trigger = datetime.now(TAIPEI_TZ).timestamp()
|
| 389 |
-
return gr.update(value=f"✅ 菜單已成功更新!({datetime.now(TAIPEI_TZ).strftime('%H:%M:%S')})"), get_menu_df(), new_trigger
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 390 |
|
| 391 |
def update_members_list(df):
|
| 392 |
_internal_update_members(df)
|
|
@@ -968,6 +1017,19 @@ def autofill_delete_target(evt: gr.SelectData, df_data):
|
|
| 968 |
except:
|
| 969 |
return ""
|
| 970 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 971 |
# ----------------------------
|
| 972 |
# 🖥️ 網頁介面 (UI) 與 終極防護注射
|
| 973 |
# ----------------------------
|
|
@@ -1305,7 +1367,7 @@ with gr.Blocks(theme=gr.themes.Soft(), title="簡易訂單管理系統") as demo
|
|
| 1305 |
gr.Markdown("### 🗑️ 刪除訂單管理")
|
| 1306 |
with gr.Row(elem_classes="menu-row"):
|
| 1307 |
with gr.Column(scale=2):
|
| 1308 |
-
delete_target = gr.Textbox(label="刪除單筆", placeholder="👉 點擊上方表格中的訂單,編號會自動帶入此處")
|
| 1309 |
with gr.Column(scale=1):
|
| 1310 |
delete_single_btn = gr.Button("🗑️ 刪除單筆", variant="stop")
|
| 1311 |
with gr.Column(scale=1):
|
|
@@ -1314,7 +1376,7 @@ with gr.Blocks(theme=gr.themes.Soft(), title="簡易訂單管理系統") as demo
|
|
| 1314 |
admin_status_msg = gr.Textbox(label="管理員狀態提示", interactive=False)
|
| 1315 |
|
| 1316 |
with gr.TabItem("📝 菜單管理"):
|
| 1317 |
-
gr.Markdown("✅ **適用等級**:輸入 `1`~`10` 代表專屬該等級會員可見,輸入 `全部` 則所有會員均可見。<br>✅ **設定啟用**:打勾代表商品在點餐區可見,不打勾則隱藏。<br>⚠️ **【重要提示】**:在表格內輸入 **「中文」** 時,因為網頁輸入法的關係,**請務必在輸入完畢後按下鍵盤的「Enter (確認鍵)」**,再點擊下一個儲存格,否則文字可能會消失!<br>⚠️ 修改完成後請點擊 **「💾 儲存菜單並更新」**。")
|
| 1318 |
|
| 1319 |
menu_table = gr.Dataframe(
|
| 1320 |
value=get_menu_df(),
|
|
@@ -1325,6 +1387,14 @@ with gr.Blocks(theme=gr.themes.Soft(), title="簡易訂單管理系統") as demo
|
|
| 1325 |
)
|
| 1326 |
save_menu_btn = gr.Button("💾 儲存菜單並更新", variant="primary")
|
| 1327 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1328 |
with gr.TabItem("👥 會員資料管理"):
|
| 1329 |
gr.Markdown("✅ **新增或修改會員**:直接在表格內輸入會員資訊。若要刪除會員,請將該列清空即可。<br>✅ **會員等級**:可輸入 `1`~`10`,以對應菜單中專屬等級商品。<br>⚠️ **【重要提示】**:輸入 **「中文」** 時,請務必在打完字後按下鍵盤的 **「Enter」**,再切換到下一格,避免輸入法導致文字未成功記錄。<br>⚠️ 修改完成後請點擊 **「💾 儲存會員資料並更新」**。")
|
| 1330 |
|
|
@@ -1373,6 +1443,7 @@ with gr.Blocks(theme=gr.themes.Soft(), title="簡易訂單管理系統") as demo
|
|
| 1373 |
outputs=[orders_table, revenue_display, customer_filter, time_filter, item_filter, delivery_filter]
|
| 1374 |
)
|
| 1375 |
|
|
|
|
| 1376 |
orders_table.select(
|
| 1377 |
fn=autofill_delete_target, inputs=[orders_table], outputs=[delete_target]
|
| 1378 |
)
|
|
@@ -1391,6 +1462,18 @@ with gr.Blocks(theme=gr.themes.Soft(), title="簡易訂單管理系統") as demo
|
|
| 1391 |
js="(filter) => { return confirm('🚨 極度危險:確定要清空【所有】訂單資料嗎?此動作無法復原!') ? [filter] : ['__CANCEL__']; }"
|
| 1392 |
)
|
| 1393 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1394 |
# 備份按鈕,傳入目前的表單資料
|
| 1395 |
github_btn.click(
|
| 1396 |
fn=backup_and_push_to_github,
|
|
@@ -1399,7 +1482,7 @@ with gr.Blocks(theme=gr.themes.Soft(), title="簡易訂單管理系統") as demo
|
|
| 1399 |
)
|
| 1400 |
|
| 1401 |
save_menu_btn.click(
|
| 1402 |
-
fn=update_menu, inputs=[menu_table], outputs=[global_sys_msg, menu_table, menu_trigger]
|
| 1403 |
)
|
| 1404 |
|
| 1405 |
restore_btn.click(
|
|
|
|
| 347 |
note = str(row[8]).strip() if pd.notna(row[8]) else ""
|
| 348 |
|
| 349 |
if item:
|
|
|
|
| 350 |
new_menu.append({
|
| 351 |
"name": item, "price": price, "cost": cost,
|
| 352 |
"enable": enable, "level": target_lvl, "seq": seq,
|
|
|
|
| 356 |
if not new_menu:
|
| 357 |
return False
|
| 358 |
|
| 359 |
+
# 自動重新排列流水號 (依據當前設定的順序,重新編排為 1~N)
|
| 360 |
+
new_menu = sorted(new_menu, key=lambda x: x.get("seq", 999))
|
| 361 |
+
for i, item_dict in enumerate(new_menu):
|
| 362 |
+
item_dict["seq"] = i + 1
|
| 363 |
+
|
| 364 |
MENU.clear()
|
| 365 |
MENU.extend(new_menu)
|
| 366 |
save_menu_to_file()
|
|
|
|
| 387 |
def update_menu(df):
|
| 388 |
success = _internal_update_menu(df)
|
| 389 |
if not success:
|
| 390 |
+
return gr.update(value="❌ 錯誤:菜單不能為空!"), gr.update(), gr.update(), gr.skip()
|
| 391 |
|
| 392 |
new_trigger = datetime.now(TAIPEI_TZ).timestamp()
|
| 393 |
+
return gr.update(value=f"✅ 菜單已成功更新,流水號已自動重新排列!({datetime.now(TAIPEI_TZ).strftime('%H:%M:%S')})"), get_menu_df(), gr.update(value=""), new_trigger
|
| 394 |
+
|
| 395 |
+
def delete_menu_item(target_str):
|
| 396 |
+
if target_str == "__CANCEL__":
|
| 397 |
+
return gr.update(), gr.update(), gr.update(), gr.update()
|
| 398 |
+
|
| 399 |
+
global MENU
|
| 400 |
+
target_str = str(target_str).strip()
|
| 401 |
+
if not target_str:
|
| 402 |
+
return gr.update(value="❌ 錯誤:請點擊表格選擇或輸入要刪除的品項!"), gr.update(), gr.update(), gr.update()
|
| 403 |
+
|
| 404 |
+
# 解析名稱與規格
|
| 405 |
+
parts = target_str.split(" | ")
|
| 406 |
+
target_name = parts[0].strip()
|
| 407 |
+
target_spec = parts[1].strip() if len(parts) > 1 else ""
|
| 408 |
+
|
| 409 |
+
original_len = len(MENU)
|
| 410 |
+
new_menu = []
|
| 411 |
+
deleted = False
|
| 412 |
+
|
| 413 |
+
# 尋找並刪除匹配的品項 (只刪除第一筆符合的,避免誤刪全部同名同規格項目)
|
| 414 |
+
for m in MENU:
|
| 415 |
+
if not deleted and m.get("name") == target_name and m.get("spec", "") == target_spec:
|
| 416 |
+
deleted = True
|
| 417 |
+
continue
|
| 418 |
+
new_menu.append(m)
|
| 419 |
+
|
| 420 |
+
if len(new_menu) == original_len:
|
| 421 |
+
return gr.update(value=f"❌ 找不到對應的品項:{target_str}"), gr.update(), gr.update(), gr.update()
|
| 422 |
+
|
| 423 |
+
# 刪除後自動重排流水號
|
| 424 |
+
for i, m in enumerate(new_menu):
|
| 425 |
+
m["seq"] = i + 1
|
| 426 |
+
|
| 427 |
+
MENU.clear()
|
| 428 |
+
MENU.extend(new_menu)
|
| 429 |
+
save_menu_to_file()
|
| 430 |
+
|
| 431 |
+
new_trigger = datetime.now(TAIPEI_TZ).timestamp()
|
| 432 |
+
|
| 433 |
+
return (
|
| 434 |
+
gr.update(value=f"✅ 成功刪除品項:{target_str},且流水號已自動重排!({datetime.now(TAIPEI_TZ).strftime('%H:%M:%S')})"),
|
| 435 |
+
get_menu_df(),
|
| 436 |
+
gr.update(value=""), # 清空文字框
|
| 437 |
+
new_trigger
|
| 438 |
+
)
|
| 439 |
|
| 440 |
def update_members_list(df):
|
| 441 |
_internal_update_members(df)
|
|
|
|
| 1017 |
except:
|
| 1018 |
return ""
|
| 1019 |
|
| 1020 |
+
def autofill_menu_delete_target(evt: gr.SelectData, df_data):
|
| 1021 |
+
try:
|
| 1022 |
+
row_idx = evt.index[0]
|
| 1023 |
+
item_name = str(df_data.iloc[row_idx, 4] or "").strip()
|
| 1024 |
+
item_spec = str(df_data.iloc[row_idx, 5] or "").strip()
|
| 1025 |
+
if not item_name: return ""
|
| 1026 |
+
# 以「名稱 | 規格」格式回傳,確保能精準刪除同名品項
|
| 1027 |
+
if item_spec:
|
| 1028 |
+
return f"{item_name} | {item_spec}"
|
| 1029 |
+
return item_name
|
| 1030 |
+
except:
|
| 1031 |
+
return ""
|
| 1032 |
+
|
| 1033 |
# ----------------------------
|
| 1034 |
# 🖥️ 網頁介面 (UI) 與 終極防護注射
|
| 1035 |
# ----------------------------
|
|
|
|
| 1367 |
gr.Markdown("### 🗑️ 刪除訂單管理")
|
| 1368 |
with gr.Row(elem_classes="menu-row"):
|
| 1369 |
with gr.Column(scale=2):
|
| 1370 |
+
delete_target = gr.Textbox(label="刪除單筆訂單", placeholder="👉 點擊上方表格中的訂單,編號會自動帶入此處")
|
| 1371 |
with gr.Column(scale=1):
|
| 1372 |
delete_single_btn = gr.Button("🗑️ 刪除單筆", variant="stop")
|
| 1373 |
with gr.Column(scale=1):
|
|
|
|
| 1376 |
admin_status_msg = gr.Textbox(label="管理員狀態提示", interactive=False)
|
| 1377 |
|
| 1378 |
with gr.TabItem("📝 菜單管理"):
|
| 1379 |
+
gr.Markdown("✅ **適用等級**:輸入 `1`~`10` 代表專屬該等級會員可見,輸入 `全部` 則所有會員均可見。<br>✅ **設定啟用**:打勾代表商品在點餐區可見,不打勾則隱藏。<br>⚠️ **【重要提示】**:在表格內輸入 **「中文」** 時,因為網頁輸入法的關係,**請務必在輸入完畢後按下鍵盤的「Enter (確認鍵)」**,再點擊下一個儲存格,否則文字可能會消失!<br>⚠️ 修改完成後請點擊 **「💾 儲存菜單並更新」**,**系統會自動重新排列流水號**。")
|
| 1380 |
|
| 1381 |
menu_table = gr.Dataframe(
|
| 1382 |
value=get_menu_df(),
|
|
|
|
| 1387 |
)
|
| 1388 |
save_menu_btn = gr.Button("💾 儲存菜單並更新", variant="primary")
|
| 1389 |
|
| 1390 |
+
gr.Markdown("---")
|
| 1391 |
+
gr.Markdown("### 🗑️ 刪除單筆菜單品項")
|
| 1392 |
+
with gr.Row(elem_classes="menu-row"):
|
| 1393 |
+
with gr.Column(scale=2):
|
| 1394 |
+
menu_delete_target = gr.Textbox(label="刪除品項", placeholder="👉 點擊上方表格中的品項,名稱與規格會自動帶入此處")
|
| 1395 |
+
with gr.Column(scale=1):
|
| 1396 |
+
menu_delete_btn = gr.Button("🗑️ 刪除該品項", variant="stop")
|
| 1397 |
+
|
| 1398 |
with gr.TabItem("👥 會員資料管理"):
|
| 1399 |
gr.Markdown("✅ **新增或修改會員**:直接在表格內輸入會員資訊。若要刪除會員,請將該列清空即可。<br>✅ **會員等級**:可輸入 `1`~`10`,以對應菜單中專屬等級商品。<br>⚠️ **【重要提示】**:輸入 **「中文」** 時,請務必在打完字後按下鍵盤的 **「Enter」**,再切換到下一格,避免輸入法導致文字未成功記錄。<br>⚠️ 修改完成後請點擊 **「💾 儲存會員資料並更新」**。")
|
| 1400 |
|
|
|
|
| 1443 |
outputs=[orders_table, revenue_display, customer_filter, time_filter, item_filter, delivery_filter]
|
| 1444 |
)
|
| 1445 |
|
| 1446 |
+
# 綁定訂單刪除自動填入
|
| 1447 |
orders_table.select(
|
| 1448 |
fn=autofill_delete_target, inputs=[orders_table], outputs=[delete_target]
|
| 1449 |
)
|
|
|
|
| 1462 |
js="(filter) => { return confirm('🚨 極度危險:確定要清空【所有】訂單資料嗎?此動作無法復原!') ? [filter] : ['__CANCEL__']; }"
|
| 1463 |
)
|
| 1464 |
|
| 1465 |
+
# 綁定菜單刪除自動填入
|
| 1466 |
+
menu_table.select(
|
| 1467 |
+
fn=autofill_menu_delete_target, inputs=[menu_table], outputs=[menu_delete_target]
|
| 1468 |
+
)
|
| 1469 |
+
|
| 1470 |
+
menu_delete_btn.click(
|
| 1471 |
+
fn=delete_menu_item,
|
| 1472 |
+
inputs=[menu_delete_target],
|
| 1473 |
+
outputs=[global_sys_msg, menu_table, menu_delete_target, menu_trigger],
|
| 1474 |
+
js="(target) => { if (!target) { return target; } return confirm('⚠️ 確認要刪除品項 [' + target + '] 嗎?此動作無法復原!') ? target : '__CANCEL__'; }"
|
| 1475 |
+
)
|
| 1476 |
+
|
| 1477 |
# 備份按鈕,傳入目前的表單資料
|
| 1478 |
github_btn.click(
|
| 1479 |
fn=backup_and_push_to_github,
|
|
|
|
| 1482 |
)
|
| 1483 |
|
| 1484 |
save_menu_btn.click(
|
| 1485 |
+
fn=update_menu, inputs=[menu_table], outputs=[global_sys_msg, menu_table, menu_delete_target, menu_trigger]
|
| 1486 |
)
|
| 1487 |
|
| 1488 |
restore_btn.click(
|