from nicegui import ui
import os
from utils.template_utils import get_template_files
# 修改点 1:统一导入,避免与变量名 config 冲突
from config.config import load_config, save_config
def create_config_page():
# 修改点 2:将加载逻辑放入页面生成函数内,确保每次刷新页面获取最新值
conf_data = load_config("config.toml")
template_options = get_template_files()
current_filename = os.path.basename(conf_data.get("source_file", ""))
if current_filename and current_filename not in template_options:
template_options.append(current_filename)
ui.add_head_html('')
# 样式修正:添加全屏且不滚动条的 CSS
ui.add_head_html(
"""
"""
)
with ui.header().classes("app-header items-center justify-between shadow-md"):
# 左侧:图标和标题
with ui.row().classes("items-center gap-2"):
ui.image("/assets/icon.ico").classes("w-8 h-8").props("fit=contain")
ui.label("尚城幼儿园成长报告助手").classes("text-xl font-bold")
# 右侧:署名 + 配置按钮
with ui.row().classes("items-center gap-4"):
ui.label("By 寒寒 | 这里的每一份评语都充满爱意").classes(
"text-xs opacity-90"
)
ui.button(icon="home", on_click=lambda: ui.navigate.to("/")).props(
"flat round color=white"
)
# 修改点 3:使用 flex 布局撑满
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"
):
# --- 路径设置 ---
with ui.tab_panel(tab_path).classes("w-full p-0"):
with ui.column().classes("w-full p-4 gap-4"):
source_file = (
ui.select(
options=template_options,
label="PPT 模板",
value=current_filename,
)
.props("outlined fill-input")
.classes("w-full")
)
excel_file = (
ui.input(
"Excel 文件",
value=os.path.basename(conf_data.get("excel_file", "")),
)
.props("outlined")
.classes("w-full")
)
image_folder = (
ui.input(
"图片目录",
value=os.path.basename(conf_data.get("image_folder", "")),
)
.props("outlined")
.classes("w-full")
)
output_folder = (
ui.input(
"输出目录",
value=os.path.basename(
conf_data.get("output_folder", "output")
),
)
.props("outlined")
.classes("w-full")
)
# --- 班级信息 ---
with ui.tab_panel(tab_class).classes("w-full p-0"):
with ui.column().classes("w-full p-4 gap-4"):
class_name = (
ui.input("班级名称", value=conf_data.get("class_name", ""))
.props("outlined")
.classes("w-full")
)
age_group = (
ui.select(
options=[
"小班上学期",
"小班下学期",
"中班上学期",
"中班下学期",
"大班上学期",
"大班下学期",
],
label="年龄段",
value=conf_data.get("age_group", "中班上学期"),
)
.props("outlined")
.classes("w-full")
)
teachers_text = (
ui.textarea(
"教师名单", value="\n".join(conf_data.get("teachers", []))
)
.props("outlined")
.classes("w-full h-40")
)
class_type = (
ui.select(
options={0: "便宜班", 1: "昂贵班", 2: "昂贵的双木桥班"},
label="班级类型",
value=conf_data.get("class_type", 0),
)
.props("outlined")
.classes("w-full")
)
# --- AI 配置 ---
with ui.tab_panel(tab_ai).classes("w-full p-0"):
with ui.column().classes("w-full p-4 gap-4"):
ai_key = (
ui.input("API Key", value=conf_data["ai"].get("api_key", ""))
.props("outlined password")
.classes("w-full")
)
ai_url = (
ui.input("API URL", value=conf_data["ai"].get("api_url", ""))
.props("outlined")
.classes("w-full")
)
ai_model = (
ui.input("Model Name", value=conf_data["ai"].get("model", ""))
.props("outlined")
.classes("w-full")
)
ai_prompt = (
ui.textarea(
"System Prompt", value=conf_data["ai"].get("prompt", "")
)
.props("outlined")
.classes("w-full h-full")
)
# 底部固定按钮
with ui.row().classes("w-full p-4"):
async def handle_save():
new_data = {
"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,
},
}
# 修改点 4:直接调用导入的 save_config 函数名
success, message = save_config(new_data)
ui.notify("配置已保存重启生效", type="positive")
ui.button("保存配置", on_click=handle_save).classes("w-full py-4").props(
"outline color=primary"
)