DeepLearning101 commited on
Commit
e0e49dd
·
verified ·
1 Parent(s): 0ad419c

新版 Gradio 的 gr.Markdown() 元件不再支援直接寫 style="..." 參數,所以程式一啟動就崩潰了。

Browse files
Files changed (1) hide show
  1. app.py +10 -10
app.py CHANGED
@@ -5,7 +5,7 @@ import requests
5
  from supabase import create_client, Client
6
  from datetime import datetime, timedelta, timezone
7
 
8
- # ✅ 補回:設定台北時區
9
  TAIPEI_TZ = timezone(timedelta(hours=8))
10
 
11
  # --- 設定 ---
@@ -13,6 +13,7 @@ SUPABASE_URL = os.getenv("SUPABASE_URL")
13
  SUPABASE_KEY = os.getenv("SUPABASE_KEY")
14
  GAS_MAIL_URL = os.getenv("GAS_MAIL_URL")
15
  LINE_ACCESS_TOKEN = os.getenv("LINE_ACCESS_TOKEN")
 
16
  PUBLIC_SPACE_URL = "https://deeplearning101-ciecietaipei.hf.space"
17
 
18
  # 取得帳密 (若沒設定則使用預設值)
@@ -118,33 +119,34 @@ def send_confirmation_hybrid(booking_id):
118
  return log_msg
119
  except Exception as e: return f"❌ Error: {str(e)}"
120
 
121
- # --- [修正點] 登入邏輯 ---
122
  def check_login(user, password):
123
  if user == REAL_ADMIN_USER and password == REAL_ADMIN_PASSWORD:
124
- # 登入成功:隱藏登入頁,顯示後台,並清空錯誤訊息
125
  return {
126
  login_row: gr.update(visible=False),
127
  admin_row: gr.update(visible=True),
128
  error_msg: ""
129
  }
130
  else:
 
131
  return {
132
- error_msg: " 帳號或密碼錯誤"
133
  }
134
 
135
  # --- 介面開始 ---
136
  with gr.Blocks(title="Admin") as demo:
137
 
138
- # 1. 登入介面 (預設顯示)
139
  with gr.Group(visible=True) as login_row:
140
  gr.Markdown("# 🔒 請登入後台")
141
  with gr.Row():
142
  username_input = gr.Textbox(label="帳號 Username", placeholder="Enter username")
143
  password_input = gr.Textbox(label="密碼 Password", type="password", placeholder="Enter password")
144
  login_btn = gr.Button("登入 Login", variant="primary")
145
- error_msg = gr.Markdown("", style="color: red;")
 
146
 
147
- # 2. 後台介面 (預設隱藏)
148
  with gr.Group(visible=False) as admin_row:
149
  gr.Markdown("# 🍷 訂位管理後台 (Dashboard)")
150
  refresh_btn = gr.Button("🔄 重新整理")
@@ -154,11 +156,10 @@ with gr.Blocks(title="Admin") as demo:
154
  action_btn = gr.Button("📧 發送確認信 (Hybrid)", variant="primary")
155
  log_output = gr.Textbox(label="結果")
156
 
157
- # 綁定後台按鈕功能
158
  refresh_btn.click(get_bookings, outputs=booking_table)
159
  action_btn.click(send_confirmation_hybrid, inputs=id_input, outputs=log_output)
160
 
161
- # 3. 綁定登入按鈕事件
162
  login_btn.click(
163
  check_login,
164
  inputs=[username_input, password_input],
@@ -166,5 +167,4 @@ with gr.Blocks(title="Admin") as demo:
166
  )
167
 
168
  if __name__ == "__main__":
169
- # ⚠️ 注意:這裡不再使用 auth=(...),改用上面寫的 Python 邏輯來切換顯示
170
  demo.launch()
 
5
  from supabase import create_client, Client
6
  from datetime import datetime, timedelta, timezone
7
 
8
+ # 設定台北時區
9
  TAIPEI_TZ = timezone(timedelta(hours=8))
10
 
11
  # --- 設定 ---
 
13
  SUPABASE_KEY = os.getenv("SUPABASE_KEY")
14
  GAS_MAIL_URL = os.getenv("GAS_MAIL_URL")
15
  LINE_ACCESS_TOKEN = os.getenv("LINE_ACCESS_TOKEN")
16
+ # ⚠️ 請確認這是您 Space A 的正確網址 (結尾不要有斜線)
17
  PUBLIC_SPACE_URL = "https://deeplearning101-ciecietaipei.hf.space"
18
 
19
  # 取得帳密 (若沒設定則使用預設值)
 
119
  return log_msg
120
  except Exception as e: return f"❌ Error: {str(e)}"
121
 
122
+ # --- 登入邏輯 (回傳 HTML 字串來控制顏色) ---
123
  def check_login(user, password):
124
  if user == REAL_ADMIN_USER and password == REAL_ADMIN_PASSWORD:
 
125
  return {
126
  login_row: gr.update(visible=False),
127
  admin_row: gr.update(visible=True),
128
  error_msg: ""
129
  }
130
  else:
131
+ # ✅ 使用 HTML span 標籤來顯示紅色,而不是在 gr.Markdown 用 style 參數
132
  return {
133
+ error_msg: "<span style='color: red'>❌ 帳號或密碼錯誤</span>"
134
  }
135
 
136
  # --- 介面開始 ---
137
  with gr.Blocks(title="Admin") as demo:
138
 
139
+ # 1. 登入介面
140
  with gr.Group(visible=True) as login_row:
141
  gr.Markdown("# 🔒 請登入後台")
142
  with gr.Row():
143
  username_input = gr.Textbox(label="帳號 Username", placeholder="Enter username")
144
  password_input = gr.Textbox(label="密碼 Password", type="password", placeholder="Enter password")
145
  login_btn = gr.Button("登入 Login", variant="primary")
146
+ # 修正:移除 style 參數,避免報錯
147
+ error_msg = gr.Markdown("")
148
 
149
+ # 2. 後台介面
150
  with gr.Group(visible=False) as admin_row:
151
  gr.Markdown("# 🍷 訂位管理後台 (Dashboard)")
152
  refresh_btn = gr.Button("🔄 重新整理")
 
156
  action_btn = gr.Button("📧 發送確認信 (Hybrid)", variant="primary")
157
  log_output = gr.Textbox(label="結果")
158
 
 
159
  refresh_btn.click(get_bookings, outputs=booking_table)
160
  action_btn.click(send_confirmation_hybrid, inputs=id_input, outputs=log_output)
161
 
162
+ # 3. 綁定登入按鈕
163
  login_btn.click(
164
  check_login,
165
  inputs=[username_input, password_input],
 
167
  )
168
 
169
  if __name__ == "__main__":
 
170
  demo.launch()