DeepLearning101 commited on
Commit
6320354
·
verified ·
1 Parent(s): ccfb5f6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -64
app.py CHANGED
@@ -1,76 +1,83 @@
1
  import gradio as gr
 
2
 
3
- # --- 設定主題配色 (使用 Gradio 原生 Theme Builder) ---
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
- body_background_fill="#1c1c1c", # 整體背景:深炭灰 (非純黑)
12
- block_background_fill="#262626", # 區塊背景:稍亮一點的灰
13
- block_border_width="0px", # 去掉生硬的邊框
14
- input_background_fill="#333333", # 輸入框背景:深灰
15
- input_border_color="#444444", # 輸入框邊框
16
- text_color="#E0E0E0", # 一般文字:灰白 (不刺眼)
17
- block_title_text_color="#d4af37", # 標題文字:香檳金
18
- button_primary_background_fill="#d4af37", # 主按鈕背景:香檳金
19
- button_primary_text_color="#000000", # 主按鈕文字:黑色
20
  )
21
 
22
- # --- 自定義 CSS (只處理 Logo 和特殊排版) ---
23
  custom_css = """
24
- /* 隱藏 Gradio 預設的 footer */
25
  footer {visibility: hidden}
26
-
27
- /* 標題區域美化 */
28
- .header-box {
29
- text-align: center;
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 get_available_slots(date_str):
56
- # 這裡未來接 Supabase
57
- return ["18:00 (剩 4 位)", "19:30 (剩 2 位)", "21:00 (已客滿)"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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>,我們已收到您的資訊。<br>
66
- 系統將發送確認信至您的信箱,請留意查收。</p>
 
 
 
 
 
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
- # 日期改用 Textbox + type='date' 比較穩定,或者用 Dropdown
87
- # 這裡暫用 Calendar,但在暗色模式下有些瀏覽器會顯示白色,這是瀏覽器限制
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=lambda d: (gr.update(choices=["18:00","18:30", "19:00","19:30", "20:00", "20:30", "21:00", "21:30", "22:00", "22:30", "23:00", "23:30", "00:00", "00:30", "01:00", "01:30", "02:00",]), "✅ 已更新時段"),
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
  )