import os import sys from nicegui import ui, app, run, native from loguru import logger # 导入我们的模块 from config.config import load_config from ui.core.logger import setup_logger from utils.font_utils import install_fonts_from_directory from ui.views.home_page import create_page sys.stdout.reconfigure(encoding='utf-8') sys.stderr.reconfigure(encoding='utf-8') # 1. 初始化配置 config = load_config("config.toml") setup_logger() # === 关键修改:定义一个获取路径的通用函数 === def get_path(relative_path): """ 获取资源的绝对路径。 兼容:开发环境(直接运行) 和 生产环境(打包成exe后解压的临时目录) """ if hasattr(sys, '_MEIPASS'): base_path = sys._MEIPASS else: # 开发环境当前目录 base_path = os.path.dirname(os.path.abspath(__file__)) return os.path.join(base_path, relative_path) # 1. 挂载静态资源 (CSS/图片) # 注意:这里使用 get_path 确保打包后能找到 static_dir = get_path(os.path.join("ui", "assets")) app.add_static_files('/assets', static_dir) # 3. 页面路由 @ui.page('/') def index(): create_page() # 4. 启动时钩子 async def startup_check(): try: logger.info("系统启动: 初始化资源...") await run.io_bound(install_fonts_from_directory, config["fonts_dir"]) os.makedirs(config["output_folder"], exist_ok=True) logger.success("资源初始化完成") except Exception as e: logger.error(f"初始化失败: {e}") app.on_startup(startup_check) if __name__ in {"__main__", "__mp_main__"}: ui.run( title="尚城幼儿园成长报告助手", native=True, window_size=(900, 900), port=native.find_open_port(), # 自动寻找端口 reload=False )