Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,76 +1,83 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
|
| 3 |
-
# --- 設定主題配色 (
|
| 4 |
-
# 我們選用 "Zinc" (冷灰) 作為基底,"Gold" (金) 作為強調色
|
| 5 |
theme = gr.themes.Soft(
|
| 6 |
-
primary_hue="amber",
|
| 7 |
-
neutral_hue="zinc",
|
| 8 |
font=[gr.themes.GoogleFont("Playfair Display"), "ui-sans-serif", "system-ui", "sans-serif"],
|
| 9 |
).set(
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
button_primary_text_color="#000000", # 主按鈕文字:黑色
|
| 20 |
)
|
| 21 |
|
| 22 |
-
# --- 自定義 CSS
|
| 23 |
custom_css = """
|
| 24 |
-
/* 隱藏 Gradio 預設的 footer */
|
| 25 |
footer {visibility: hidden}
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
.header-
|
| 29 |
-
|
| 30 |
-
padding: 30px 0;
|
| 31 |
-
margin-bottom: 20px;
|
| 32 |
-
border-bottom: 1px solid #d4af37;
|
| 33 |
-
}
|
| 34 |
-
.header-title {
|
| 35 |
-
font-family: 'Playfair Display', serif;
|
| 36 |
-
font-size: 42px;
|
| 37 |
-
color: #d4af37; /* 香檳金 */
|
| 38 |
-
letter-spacing: 3px;
|
| 39 |
-
margin: 0;
|
| 40 |
-
}
|
| 41 |
-
.header-subtitle {
|
| 42 |
-
font-family: sans-serif;
|
| 43 |
-
color: #888;
|
| 44 |
-
font-size: 14px;
|
| 45 |
-
margin-top: 10px;
|
| 46 |
-
letter-spacing: 1px;
|
| 47 |
-
}
|
| 48 |
-
/* 讓日曆選單不要太亮 */
|
| 49 |
-
input[type="datetime-local"] {
|
| 50 |
-
color-scheme: dark;
|
| 51 |
-
}
|
| 52 |
"""
|
| 53 |
|
| 54 |
-
# ---
|
| 55 |
-
def
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
|
|
|
| 59 |
def handle_booking(name, tel, date, time, pax):
|
| 60 |
-
if not name or not tel:
|
| 61 |
-
return "⚠️
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
return f"""
|
| 63 |
<div style='background: #2a2a2a; padding: 20px; border-left: 3px solid #d4af37; color: #fff;'>
|
| 64 |
<h3 style='color: #d4af37; margin:0;'>🥂 預約申請已提交</h3>
|
| 65 |
-
<p>親愛的 <b>{name}</b
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
</div>
|
| 68 |
"""
|
| 69 |
|
| 70 |
# --- 介面佈局 ---
|
| 71 |
with gr.Blocks(theme=theme, css=custom_css, title="Cié Cié Taipei") as demo:
|
| 72 |
|
| 73 |
-
# 1. 頂部 Logo 區
|
| 74 |
gr.HTML("""
|
| 75 |
<div class="header-box">
|
| 76 |
<h1 class="header-title">Cié Cié Taipei</h1>
|
|
@@ -78,41 +85,33 @@ with gr.Blocks(theme=theme, css=custom_css, title="Cié Cié Taipei") as demo:
|
|
| 78 |
</div>
|
| 79 |
""")
|
| 80 |
|
| 81 |
-
# 2. 主要操作區 (分左右兩欄)
|
| 82 |
with gr.Row():
|
| 83 |
-
# 左側:選擇時間與人數
|
| 84 |
with gr.Column(scale=1):
|
| 85 |
gr.Markdown("### 📅 預約資訊")
|
| 86 |
-
#
|
| 87 |
-
|
| 88 |
-
# 建議實務上也可以用 Dropdown 選「今天/明天/後天」
|
| 89 |
-
booking_date = gr.DateTime(label="日期與時間", include_time=False)
|
| 90 |
|
| 91 |
pax_count = gr.Slider(minimum=1, maximum=10, value=2, step=1, label="用餐人數",
|
| 92 |
info="超過 10 人請直接來電")
|
| 93 |
|
| 94 |
-
# 右側:時段與狀態
|
| 95 |
with gr.Column(scale=1):
|
| 96 |
gr.Markdown("### 🕰️ 選擇時段")
|
| 97 |
-
|
| 98 |
-
status_box = gr.Markdown("請選擇日期以查看剩餘座位", visible=True)
|
| 99 |
time_slot = gr.Dropdown(choices=[], label="可用時段", interactive=True)
|
| 100 |
|
| 101 |
-
# 3. 個人資料區
|
| 102 |
gr.Markdown("### 👤 聯絡人資料")
|
| 103 |
with gr.Group():
|
| 104 |
with gr.Row():
|
| 105 |
cust_name = gr.Textbox(label="訂位姓名", placeholder="ex. 王小明")
|
| 106 |
cust_tel = gr.Textbox(label="手機號碼", placeholder="ex. 0912-345-678")
|
| 107 |
|
| 108 |
-
# 4. 送出按鈕與結果
|
| 109 |
submit_btn = gr.Button("確認預約 Request Booking", size="lg", variant="primary")
|
| 110 |
output_msg = gr.HTML(label="系統訊息")
|
| 111 |
|
| 112 |
# --- 互動邏輯 ---
|
| 113 |
-
#
|
| 114 |
booking_date.change(
|
| 115 |
-
fn=
|
| 116 |
inputs=booking_date,
|
| 117 |
outputs=[time_slot, status_box]
|
| 118 |
)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from datetime import datetime
|
| 3 |
|
| 4 |
+
# --- 設定主題配色 (修正參數名稱錯誤) ---
|
|
|
|
| 5 |
theme = gr.themes.Soft(
|
| 6 |
+
primary_hue="amber",
|
| 7 |
+
neutral_hue="zinc",
|
| 8 |
font=[gr.themes.GoogleFont("Playfair Display"), "ui-sans-serif", "system-ui", "sans-serif"],
|
| 9 |
).set(
|
| 10 |
+
body_background_fill="#1c1c1c",
|
| 11 |
+
block_background_fill="#262626",
|
| 12 |
+
block_border_width="0px",
|
| 13 |
+
input_background_fill="#333333",
|
| 14 |
+
input_border_color="#444444",
|
| 15 |
+
body_text_color="#E0E0E0", # ⚠️ 修正:這裡改成 body_text_color
|
| 16 |
+
block_title_text_color="#d4af37",
|
| 17 |
+
button_primary_background_fill="#d4af37",
|
| 18 |
+
button_primary_text_color="#000000",
|
|
|
|
| 19 |
)
|
| 20 |
|
| 21 |
+
# --- 自定義 CSS ---
|
| 22 |
custom_css = """
|
|
|
|
| 23 |
footer {visibility: hidden}
|
| 24 |
+
.header-box { text-align: center; padding: 30px 0; margin-bottom: 20px; border-bottom: 1px solid #d4af37; }
|
| 25 |
+
.header-title { font-family: 'Playfair Display', serif; font-size: 42px; color: #d4af37; letter-spacing: 3px; margin: 0; }
|
| 26 |
+
.header-subtitle { font-family: sans-serif; color: #888; font-size: 14px; margin-top: 10px; letter-spacing: 1px; }
|
| 27 |
+
input[type="datetime-local"] { color-scheme: dark; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
"""
|
| 29 |
|
| 30 |
+
# --- 核心邏輯:動態計算時段 ---
|
| 31 |
+
def update_time_slots(date_obj):
|
| 32 |
+
if date_obj is None:
|
| 33 |
+
return gr.update(choices=[]), "請先選擇日期"
|
| 34 |
+
|
| 35 |
+
# 取得星期幾 (0=週一, 1=週二 ... 4=週五, 5=週六, 6=週日)
|
| 36 |
+
weekday = date_obj.weekday()
|
| 37 |
+
|
| 38 |
+
# 基礎時段 (每天都有)
|
| 39 |
+
slots = ["18:00", "18:30", "19:00", "19:30", "20:00", "20:30",
|
| 40 |
+
"21:00", "21:30", "22:00", "22:30", "23:00", "23:30",
|
| 41 |
+
"00:00", "00:30", "01:00"]
|
| 42 |
+
|
| 43 |
+
# 判斷營業時間
|
| 44 |
+
if weekday == 4 or weekday == 5:
|
| 45 |
+
# --- 週五 (4) 與 週六 (5) ---
|
| 46 |
+
# 營業到 03:00,最後訂位開放到 02:00 或 02:30
|
| 47 |
+
slots.extend(["01:30", "02:00", "02:30"])
|
| 48 |
+
status_msg = f"📅 選擇了 {date_obj.strftime('%m/%d')} (週末時段 營業至 03:00)"
|
| 49 |
+
else:
|
| 50 |
+
# --- 週日 到 週四 ---
|
| 51 |
+
# 營業到 02:00,最後訂位開放到 01:00 或 01:30
|
| 52 |
+
slots.extend(["01:30"])
|
| 53 |
+
status_msg = f"📅 選擇了 {date_obj.strftime('%m/%d')} (平日時段 營業至 02:00)"
|
| 54 |
+
|
| 55 |
+
return gr.update(choices=slots, value=slots[0]), status_msg
|
| 56 |
|
| 57 |
+
# --- 模擬寫入資料 ---
|
| 58 |
def handle_booking(name, tel, date, time, pax):
|
| 59 |
+
if not name or not tel or not date or not time:
|
| 60 |
+
return "⚠️ 請完整填寫所有欄位 (姓名、電話、日期、時間)"
|
| 61 |
+
|
| 62 |
+
# 這裡格式化一下顯示的日期
|
| 63 |
+
date_str = date.strftime("%Y-%m-%d")
|
| 64 |
+
|
| 65 |
return f"""
|
| 66 |
<div style='background: #2a2a2a; padding: 20px; border-left: 3px solid #d4af37; color: #fff;'>
|
| 67 |
<h3 style='color: #d4af37; margin:0;'>🥂 預約申請已提交</h3>
|
| 68 |
+
<p>親愛的 <b>{name}</b>,我們已收到您的資訊。</p>
|
| 69 |
+
<ul style='color: #ccc;'>
|
| 70 |
+
<li>日期:{date_str}</li>
|
| 71 |
+
<li>時間:{time}</li>
|
| 72 |
+
<li>人數:{pax} 位</li>
|
| 73 |
+
</ul>
|
| 74 |
+
<p>系統將發送確認信至您的信箱,請留意查收。</p>
|
| 75 |
</div>
|
| 76 |
"""
|
| 77 |
|
| 78 |
# --- 介面佈局 ---
|
| 79 |
with gr.Blocks(theme=theme, css=custom_css, title="Cié Cié Taipei") as demo:
|
| 80 |
|
|
|
|
| 81 |
gr.HTML("""
|
| 82 |
<div class="header-box">
|
| 83 |
<h1 class="header-title">Cié Cié Taipei</h1>
|
|
|
|
| 85 |
</div>
|
| 86 |
""")
|
| 87 |
|
|
|
|
| 88 |
with gr.Row():
|
|
|
|
| 89 |
with gr.Column(scale=1):
|
| 90 |
gr.Markdown("### 📅 預約資訊")
|
| 91 |
+
# ⚠️ 重點:這裡設定 type="datetime",讓 Python 直接拿到 datetime 物件,方便判斷星期幾
|
| 92 |
+
booking_date = gr.DateTime(label="日期與時間", include_time=False, type="datetime")
|
|
|
|
|
|
|
| 93 |
|
| 94 |
pax_count = gr.Slider(minimum=1, maximum=10, value=2, step=1, label="用餐人數",
|
| 95 |
info="超過 10 人請直接來電")
|
| 96 |
|
|
|
|
| 97 |
with gr.Column(scale=1):
|
| 98 |
gr.Markdown("### 🕰️ 選擇時段")
|
| 99 |
+
status_box = gr.Markdown("請選擇日期以查看剩餘座位")
|
|
|
|
| 100 |
time_slot = gr.Dropdown(choices=[], label="可用時段", interactive=True)
|
| 101 |
|
|
|
|
| 102 |
gr.Markdown("### 👤 聯絡人資料")
|
| 103 |
with gr.Group():
|
| 104 |
with gr.Row():
|
| 105 |
cust_name = gr.Textbox(label="訂位姓名", placeholder="ex. 王小明")
|
| 106 |
cust_tel = gr.Textbox(label="手機號碼", placeholder="ex. 0912-345-678")
|
| 107 |
|
|
|
|
| 108 |
submit_btn = gr.Button("確認預約 Request Booking", size="lg", variant="primary")
|
| 109 |
output_msg = gr.HTML(label="系統訊息")
|
| 110 |
|
| 111 |
# --- 互動邏輯 ---
|
| 112 |
+
# 當日期改變時,呼叫 update_time_slots 函式
|
| 113 |
booking_date.change(
|
| 114 |
+
fn=update_time_slots,
|
| 115 |
inputs=booking_date,
|
| 116 |
outputs=[time_slot, status_box]
|
| 117 |
)
|