Spaces:
Running
Running
| from fastapi import APIRouter | |
| from fastapi.responses import HTMLResponse, FileResponse | |
| from pathlib import Path | |
| from typing import Dict, List | |
| from core.registry.tasks_registry import TASKS | |
| # ===================================================== | |
| # CONFIG | |
| # ===================================================== | |
| BASE_DIR = Path(__file__).resolve().parents[2] | |
| UI_DIR = BASE_DIR / "ui" | |
| INDEX_FILE = UI_DIR / "index.html" | |
| # ===================================================== | |
| # TASK GROUPING | |
| # ===================================================== | |
| def group_tasks() -> Dict[str, List[dict]]: | |
| """ | |
| Converts registry → categorized UI structure. | |
| """ | |
| categories: Dict[str, List[dict]] = {} | |
| for task in TASKS: | |
| if not getattr(task, "enabled", True): | |
| continue | |
| category = getattr(task, "category", "general") | |
| categories.setdefault(category, []).append( | |
| { | |
| "name": task.name, | |
| "description": getattr(task, "description", ""), | |
| } | |
| ) | |
| return categories | |
| # ===================================================== | |
| # ROUTER BUILDER | |
| # ===================================================== | |
| def build_ui_router() -> APIRouter: | |
| """ | |
| Serves UI + dynamic UI metadata. | |
| Endpoints: | |
| / | |
| /ui | |
| /ui/tasks | |
| /ui/health | |
| """ | |
| router = APIRouter(tags=["UI"]) | |
| # ------------------------------------------------- | |
| # MAIN UI | |
| # ------------------------------------------------- | |
| async def serve_root(): | |
| """ | |
| Serves index.html | |
| """ | |
| if not INDEX_FILE.exists(): | |
| return HTMLResponse( | |
| "<h1>Basyx UI Missing</h1>", | |
| status_code=500, | |
| ) | |
| return FileResponse(INDEX_FILE) | |
| # ------------------------------------------------- | |
| # Explicit UI route | |
| # ------------------------------------------------- | |
| async def serve_ui(): | |
| return await serve_root() | |
| # ------------------------------------------------- | |
| # UI TASK CATALOG | |
| # ------------------------------------------------- | |
| async def ui_tasks(): | |
| """ | |
| UI fetches this to build sidebar dynamically. | |
| """ | |
| return group_tasks() | |
| # ------------------------------------------------- | |
| # HEALTH | |
| # ------------------------------------------------- | |
| async def ui_health(): | |
| return { | |
| "status": "ok", | |
| "ui": "active", | |
| "tasks": len(TASKS), | |
| } | |
| return router |