Geophysics_class / ui /layouts.py
cwadayi's picture
Update ui/layouts.py
1b65622 verified
raw
history blame
9.1 kB
# ui/layouts.py
import gradio as gr
from config import content, data, defaults
from core import callbacks
def create_ui(visit_count_html: str, theme: gr.Theme):
"""
Creates and returns the Gradio UI Blocks.
Args:
visit_count_html: The Markdown string to display the visit count.
theme: The Gradio theme object to apply to the UI.
"""
# --- Main UI Layout ---
with gr.Blocks(theme=theme, title="地球物理學") as demo: # ✨ 調整 title 顯示在瀏覽器標籤頁
# --- Display Visit Counter at the top (如果需要,也可以選擇移除) ---
# gr.Markdown(visit_count_html)
# --- ✨ 新增: 自訂頂部 Header 區塊 (模仿圖片中的藍綠色背景) ---
gr.Markdown(
"""
<style>
.custom-header {
background: linear-gradient(135deg, #005f73, #0a9396); /* 藍綠漸變 */
color: #fff;
padding: 4rem 2rem; /* 增加上下內距 */
text-align: center;
border-radius: 0 0 20px 20px; /* 底部圓角 */
margin-bottom: 2rem; /* 與下方內容的間距 */
box-shadow: 0 8px 15px rgba(0,0,0,0.1); /* 輕微陰影 */
}
.custom-header h1 {
font-size: 3.5rem; /* 大標題字體 */
font-weight: 700;
margin-bottom: 0.8rem; /* 標題與副標題間距 */
line-height: 1.2;
}
.custom-header p {
font-size: 1.6rem; /* 副標題字體 */
font-weight: 400;
opacity: 0.9;
margin-top: 0;
}
/* RWD for header */
@media (max-width: 768px) {
.custom-header {
padding: 3rem 1rem;
border-radius: 0; /* 手機版不設底部圓角 */
}
.custom-header h1 {
font-size: 2.5rem;
}
.custom-header p {
font-size: 1.2rem;
}
}
</style>
<div class="custom-header">
<h1>地球物理概論</h1>
<p>一個探索我們星球內在奧秘的互動式指南</p>
</div>
""",
# 為了讓這些 CSS 生效,將 render_as_html 設為 True
# 但是 Gradio 預設的 markdown 已經是 render_as_html,所以通常不需要特別設定
)
# --- ✨ 新增: 模仿網站導覽列的 Tab 容器 ---
# 移除 container=False,讓它有白色背景和陰影
with gr.Row(variant="panel", elem_id="main-nav-tabs"): # 給予 ID 方便未來 CSS 微調
# gr.Tabs 放在 Row 裡面,方便控制其寬度與位置
# class_name 用於自訂 CSS 樣式,但 Gradio 主題已能控制大部分外觀
with gr.Tabs(selected=0): # 預設選中第一個 Tab
with gr.TabItem("🎯 課程目標"):
gr.Markdown(content.course_goals_md)
with gr.TabItem("🗓️ 課程進度"):
gr.Markdown("### 每週課程安排")
gr.DataFrame(data.schedule_df, wrap=True)
with gr.TabItem("💯 成績計算"):
gr.Markdown(content.grading_policy_md)
with gr.TabItem("🚀 互動體驗區 (程式碼實驗室)"):
gr.Markdown("## 🚀 互動程式碼實驗室")
gr.Markdown("歡迎來到這裡!直接修改下方的 Python 程式碼,點擊「執行」,即可在右側看到成果。這是學習程式與地球物理最直接的方式!")
gr.Info("注意:執行環境已受限,僅支援資料視覺化相關操作。請勿嘗試檔案讀寫或網路請求。")
with gr.Accordion("🌍 地圖繪製實驗室 (PyGMT/Cartopy 概念)", open=True):
with gr.Row():
with gr.Column(scale=2):
gr.Markdown("### 說明\n這段程式碼使用 `cartopy` 和 `matplotlib` 函式庫來繪製地理地圖。\n\n**您可以試著:**\n1. 修改 `center_lon`, `center_lat` 來改變地圖中心。\n2. 調整 `extent_lon`, `extent_lat` 來縮放地圖。\n3. 將 `coastline_color` 改成 'red' 或其他顏色。\n4. **在 `symbols` 列表中新增或修改字典,來繪製自訂的符號(例如:標示您所在的城市)。**")
map_code = gr.Code(label="可編輯的 Python 程式碼", value=defaults.DEFAULT_MAP_CODE, language="python", lines=25)
map_run_button = gr.Button("執行程式碼", variant="primary")
with gr.Column(scale=3):
map_plot_output = gr.Plot(label="地圖輸出")
map_console_output = gr.Textbox(label="執行結果 / 錯誤訊息", lines=8, interactive=False)
with gr.Accordion("📈 震波圖繪製實驗室 (ObsPy 概念)", open=False):
with gr.Row():
with gr.Column(scale=2):
gr.Markdown("### 說明\n這段程式碼使用 `numpy` 產生模擬的地震波數據,並用 `matplotlib` 將其視覺化。\n\n**您可以試著:**\n1. 修改 `p_wave_arrival` 和 `s_wave_arrival` 來改變 P/S 波的抵達時間。\n2. 調整 `main_freq` 來改變地震波的頻率(數值越大,波形越密集)。\n3. 將 `decay_rate` 調小,觀察振幅衰減變慢的效果。")
seismo_code = gr.Code(label="可編輯的 Python 程式碼", value=defaults.DEFAULT_SEISMO_CODE, language="python", lines=25)
seismo_run_button = gr.Button("執行程式碼", variant="primary")
with gr.Column(scale=3):
seismo_plot_output = gr.Plot(label="震波圖輸出")
seismo_console_output = gr.Textbox(label="執行結果 / 錯誤訊息", lines=8, interactive=False)
with gr.TabItem("🤖 AI 課程助教"):
with gr.Group():
gr.Markdown("### 🤖 AI 課程助教 (知識庫 & 即時資訊強化版)")
gr.Markdown("""
歡迎使用課程 AI 助教!我整合了靜態的課程知識與多種即時資訊來源,您可以隨時向我提問。
---
#### 📚 **課程知識庫**
我可以回答關於本課程的各種問題,例如:`「這門課的評分標準是什麼?」`
---
#### ⚡ **即時資訊查詢**
我還能幫您查詢最新的動態資訊,試著問我:
- **📰 今日新聞**:`「今天有什麼重要新聞?」`
- **🚨 地震報告**:`「最新的 CWA 顯著地震報告」` 或 `「最近全球有哪些大地震?」`
- **📢 災害預警**:`「現在有地震預警嗎?」`
---
#### 🔎 **進階地震查詢**
您也可以指定日期與規模來查詢全球地震紀錄,請**完全依照以下格式**提問:
- `查詢 2024-04-01 到 2024-04-07 規模 6.0 以上地震`
""")
custom_textbox = gr.Textbox(
placeholder="對課程有什麼問題嗎?或查詢即時資訊...",
show_label=False,
container=False,
)
gr.ChatInterface(
callbacks.ai_chatbot_with_kb,
chatbot=gr.Chatbot(height=450, type="messages", avatar_images=(None, "https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png")),
title="課程AI助教",
description="由結構化知識庫與即時工具驅動的問答機器人",
textbox=custom_textbox,
)
# --- Connect Buttons to Backend Functions ---
map_run_button.click(
fn=lambda code: callbacks.execute_user_code(code, "地圖繪製"),
inputs=[map_code],
outputs=[map_plot_output, map_console_output]
)
seismo_run_button.click(
fn=lambda code: callbacks.execute_user_code(code, "震波圖"),
inputs=[seismo_code],
outputs=[seismo_plot_output, seismo_console_output]
)
return demo