Files
growth_report/ui/views/config_page.py
2026-01-30 00:19:09 +08:00

99 lines
5.0 KiB
Python

import os
from nicegui import ui, app
from ui.views.templates.back_home import backHome
from utils.template_utils import get_template_files
def create_config_page():
# 获取当前持久化存储中的数据
cfg = app.storage.general
template_options = get_template_files()
current_filename = os.path.basename(cfg.get("source_file", ""))
if current_filename and current_filename not in template_options:
template_options.append(current_filename)
ui.add_head_html('<link href="/assets/style.css" rel="stylesheet" />')
ui.add_head_html("""
<style>
body { overflow: hidden; }
.main-card { height: calc(100vh - 100px); display: flex; flex-direction: column; }
.q-tab-panels { flex-grow: 1; overflow-y: auto !important; }
</style>
""")
with ui.header().classes("app-header items-center justify-between shadow-md"):
backHome()
with ui.card().classes("w-full max-w-5xl mx-auto shadow-lg main-card p-0"):
with ui.tabs().classes("w-full") as tabs:
tab_path = ui.tab("路径设置", icon="folder")
tab_class = ui.tab("班级与教师", icon="school")
tab_ai = ui.tab("AI 接口配置", icon="psychology")
with ui.tab_panels(tabs, value=tab_path).classes("w-full flex-grow bg-transparent"):
# --- 1. 路径设置 ---
with ui.tab_panel(tab_path).classes("w-full p-4 gap-4"):
# 注意:这里改用普通的 value= 参数,不使用 bind_value
source_file = ui.select(options=template_options, label="PPT 模板", value=cfg.get('source_file')).props(
"outlined").classes("w-full")
excel_file = ui.input("Excel 文件名", value=cfg.get('excel_file')).props("outlined").classes("w-full")
image_folder = ui.input("图片目录名", value=cfg.get('image_folder')).props("outlined").classes("w-full")
output_folder = ui.input("输出目录名", value=cfg.get('output_folder', 'output')).props(
"outlined").classes("w-full")
# --- 2. 班级信息 ---
with ui.tab_panel(tab_class).classes("w-full p-4 gap-4"):
class_name = ui.input("班级名称", value=cfg.get('class_name')).props("outlined").classes("w-full")
age_group = ui.select(
options=["小班上学期", "小班下学期", "中班上学期", "中班下学期", "大班上学期", "大班下学期"],
label="年龄段",
value=cfg.get('age_group')
).props("outlined").classes("w-full")
teachers_text = ui.textarea("教师名单(每行一个)", value="\n".join(cfg.get("teachers", []))).props(
"outlined").classes("w-full h-40")
class_type = ui.select(
options={0: "便宜班", 1: "昂贵班", 2: "昂贵的双木桥班"},
label="班级类型",
value=cfg.get('class_type', 0)
).props("outlined").classes("w-full")
# --- 3. AI 配置 ---
with ui.tab_panel(tab_ai).classes("w-full p-4 gap-4"):
ai_data = cfg.get('ai', {}) # 获取子字典
ai_key = ui.input("API Key", value=ai_data.get("api_key")).props("outlined password").classes("w-full")
ai_url = ui.input("API URL", value=ai_data.get("api_url")).props("outlined").classes("w-full")
ai_model = ui.input("Model Name", value=ai_data.get("model")).props("outlined").classes("w-full")
ai_prompt = ui.textarea("System Prompt", value=ai_data.get("prompt")).props("outlined").classes(
"w-full h-64")
# --- 底部按钮:点击后统一更新到 Storage ---
with ui.row().classes("w-full p-4"):
def handle_manual_save():
# 统一更新到 app.storage.general
# NiceGUI 会在此时感知到字典变化并触发自动保存到 JSON 文件
cfg.update({
"source_file": source_file.value,
"excel_file": excel_file.value,
"image_folder": image_folder.value,
"output_folder": output_folder.value,
"class_name": class_name.value,
"age_group": age_group.value,
"teachers": [t.strip() for t in teachers_text.value.split('\n') if t.strip()],
"class_type": class_type.value,
"ai": {
"api_key": ai_key.value,
"api_url": ai_url.value,
"model": ai_model.value,
"prompt": ai_prompt.value,
}
})
ui.notify("配置已成功更新至系统存储", type="positive", icon="save")
ui.button("保存配置", icon="save", on_click=handle_manual_save).classes("w-full py-4 shadow-md").props(
"color=primary")