Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -76,6 +76,13 @@ def load_pwd():
|
|
| 76 |
MENU = load_menu()
|
| 77 |
ADMIN_PASSWORD = load_pwd()
|
| 78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
def save_menu_to_file():
|
| 80 |
"""將目前的菜單儲存到 JSON 檔案中,確保伺服器重啟不遺失"""
|
| 81 |
try:
|
|
@@ -101,10 +108,14 @@ def update_menu(df):
|
|
| 101 |
|
| 102 |
# 解析 Gradio 傳來的 DataFrame
|
| 103 |
for row in df.values.tolist():
|
| 104 |
-
# 安全地讀取品項與價格
|
| 105 |
item = str(row[0]).strip() if pd.notna(row[0]) and str(row[0]).strip() else ""
|
| 106 |
try:
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
except:
|
| 109 |
price = 0
|
| 110 |
|
|
@@ -114,7 +125,7 @@ def update_menu(df):
|
|
| 114 |
|
| 115 |
if not new_menu:
|
| 116 |
# 避免手殘把菜單全部刪光
|
| 117 |
-
return gr.update(value="❌ 錯誤:菜單不能為空!"),
|
| 118 |
|
| 119 |
MENU.clear()
|
| 120 |
MENU.update(new_menu)
|
|
@@ -122,7 +133,7 @@ def update_menu(df):
|
|
| 122 |
|
| 123 |
# 產生一個新的時間戳記來觸發前台 Order 畫面的重新渲染
|
| 124 |
new_trigger = datetime.now().timestamp()
|
| 125 |
-
return gr.update(value=f"✅ 菜單已成功更新!({datetime.now().strftime('%H:%M:%S')})"),
|
| 126 |
|
| 127 |
def change_password(old_pwd, new_pwd):
|
| 128 |
"""處理密碼變更邏輯"""
|
|
@@ -255,7 +266,7 @@ def clear_orders():
|
|
| 255 |
return gr.update(value="🗑️ 所有訂單已清空"), gr.update(value=get_orders_data()), gr.update(value=get_revenue())
|
| 256 |
|
| 257 |
def verify_password(pwd):
|
| 258 |
-
#
|
| 259 |
if pwd == ADMIN_PASSWORD:
|
| 260 |
return gr.update(visible=False), gr.update(visible=True), gr.update(value="")
|
| 261 |
return gr.update(visible=True), gr.update(visible=False), gr.update(value="❌ **密碼錯誤。**")
|
|
@@ -332,7 +343,7 @@ with gr.Blocks(theme=gr.themes.Soft(), title="簡易訂單管理系統", css=cus
|
|
| 332 |
def render_order_form(trigger):
|
| 333 |
with gr.Row():
|
| 334 |
with gr.Column():
|
| 335 |
-
#
|
| 336 |
customer_name = gr.Textbox(label="顧客姓名", placeholder="請輸入姓名")
|
| 337 |
gr.Markdown("#### 🛍️ 選擇商品與數量")
|
| 338 |
|
|
@@ -401,12 +412,13 @@ with gr.Blocks(theme=gr.themes.Soft(), title="簡易訂單管理系統", css=cus
|
|
| 401 |
# ==========================
|
| 402 |
gr.Markdown("---")
|
| 403 |
gr.Markdown("### 📝 菜單管理 (新增 / 修改 / 刪除)")
|
| 404 |
-
gr.Markdown("✅ **修改**:直接在表格中改字。<br>✅ **刪除**:將該品項的整列清空。<br>✅ **新增**:在表格最下方空白列輸入新
|
| 405 |
|
| 406 |
menu_table = gr.Dataframe(
|
| 407 |
-
|
|
|
|
| 408 |
headers=["品項", "價格"],
|
| 409 |
-
datatype=["str", "
|
| 410 |
interactive=True,
|
| 411 |
column_count=(2, "fixed")
|
| 412 |
)
|
|
|
|
| 76 |
MENU = load_menu()
|
| 77 |
ADMIN_PASSWORD = load_pwd()
|
| 78 |
|
| 79 |
+
def get_menu_df():
|
| 80 |
+
"""【防呆新功能】產生菜單 DataFrame,並在下方固定保留 5 行空白列供使用者直接點擊新增"""
|
| 81 |
+
data = [[k, str(v)] for k, v in MENU.items()]
|
| 82 |
+
for _ in range(5):
|
| 83 |
+
data.append(["", ""])
|
| 84 |
+
return pd.DataFrame(data, columns=["品項", "價格"])
|
| 85 |
+
|
| 86 |
def save_menu_to_file():
|
| 87 |
"""將目前的菜單儲存到 JSON 檔案中,確保伺服器重啟不遺失"""
|
| 88 |
try:
|
|
|
|
| 108 |
|
| 109 |
# 解析 Gradio 傳來的 DataFrame
|
| 110 |
for row in df.values.tolist():
|
| 111 |
+
# 安全地讀取品項與價格,自動略過空白列
|
| 112 |
item = str(row[0]).strip() if pd.notna(row[0]) and str(row[0]).strip() else ""
|
| 113 |
try:
|
| 114 |
+
val = row[1]
|
| 115 |
+
if pd.isna(val) or str(val).strip() == "":
|
| 116 |
+
price = 0
|
| 117 |
+
else:
|
| 118 |
+
price = int(float(val))
|
| 119 |
except:
|
| 120 |
price = 0
|
| 121 |
|
|
|
|
| 125 |
|
| 126 |
if not new_menu:
|
| 127 |
# 避免手殘把菜單全部刪光
|
| 128 |
+
return gr.update(value="❌ 錯誤:菜單不能為空!"), get_menu_df(), gr.skip()
|
| 129 |
|
| 130 |
MENU.clear()
|
| 131 |
MENU.update(new_menu)
|
|
|
|
| 133 |
|
| 134 |
# 產生一個新的時間戳記來觸發前台 Order 畫面的重新渲染
|
| 135 |
new_trigger = datetime.now().timestamp()
|
| 136 |
+
return gr.update(value=f"✅ 菜單已成功更新!({datetime.now().strftime('%H:%M:%S')})"), get_menu_df(), new_trigger
|
| 137 |
|
| 138 |
def change_password(old_pwd, new_pwd):
|
| 139 |
"""處理密碼變更邏輯"""
|
|
|
|
| 266 |
return gr.update(value="🗑️ 所有訂單已清空"), gr.update(value=get_orders_data()), gr.update(value=get_revenue())
|
| 267 |
|
| 268 |
def verify_password(pwd):
|
| 269 |
+
# 驗證全域的 ADMIN_PASSWORD
|
| 270 |
if pwd == ADMIN_PASSWORD:
|
| 271 |
return gr.update(visible=False), gr.update(visible=True), gr.update(value="")
|
| 272 |
return gr.update(visible=True), gr.update(visible=False), gr.update(value="❌ **密碼錯誤。**")
|
|
|
|
| 343 |
def render_order_form(trigger):
|
| 344 |
with gr.Row():
|
| 345 |
with gr.Column():
|
| 346 |
+
# 已確保只有顧客姓名,無桌號字眼
|
| 347 |
customer_name = gr.Textbox(label="顧客姓名", placeholder="請輸入姓名")
|
| 348 |
gr.Markdown("#### 🛍️ 選擇商品與數量")
|
| 349 |
|
|
|
|
| 412 |
# ==========================
|
| 413 |
gr.Markdown("---")
|
| 414 |
gr.Markdown("### 📝 菜單管理 (新增 / 修改 / 刪除)")
|
| 415 |
+
gr.Markdown("✅ **修改**:直接在表格中改字。<br>✅ **刪除**:將該品項的整列清空。<br>✅ **新增**:在表格最下方的**空白列**直接輸入新菜名與價格。<br>⚠️ 修改完成後請務必點擊 **「💾 儲存菜單並更新」**。")
|
| 416 |
|
| 417 |
menu_table = gr.Dataframe(
|
| 418 |
+
# 使用 get_menu_df() 產生自動帶有 5 行空白列的表格
|
| 419 |
+
value=get_menu_df(),
|
| 420 |
headers=["品項", "價格"],
|
| 421 |
+
datatype=["str", "str"], # 調整為 str 以相容空白輸入不報錯
|
| 422 |
interactive=True,
|
| 423 |
column_count=(2, "fixed")
|
| 424 |
)
|