diff --git a/backend/routers/causal_lab.py b/backend/routers/causal_lab.py index 3ff613b..e88d9fc 100644 --- a/backend/routers/causal_lab.py +++ b/backend/routers/causal_lab.py @@ -320,6 +320,27 @@ class AnalyzeRequest(BaseModel): # ── Endpoints ───────────────────────────────────────────────────────────────── +@router.get("/api/causal-lab/debug") +def debug_causal(): + """Endpoint de diagnostic — vérifie que causal_graphs est bien chargé.""" + try: + from services.database import get_conn + from services.causal_graphs import BUILT_IN_TEMPLATES, init_tables, seed_templates + conn = get_conn() + init_tables(conn) + seed_templates(conn) + count = conn.execute("SELECT COUNT(*) FROM causal_graph_templates").fetchone()[0] + conn.close() + return { + "built_in_templates": len(BUILT_IN_TEMPLATES), + "db_templates": count, + "status": "ok", + } + except Exception as e: + import traceback + return {"status": "error", "error": str(e), "trace": traceback.format_exc()} + + @router.get("/api/causal-lab/templates") def list_templates(category: str = Query("")): try: @@ -329,8 +350,11 @@ def list_templates(category: str = Query("")): _init(conn) data = get_templates(conn, category) conn.close() + print(f"[causal_lab] list_templates → {len(data)} templates (category={category!r})", flush=True) return data except Exception as e: + import traceback + print(f"[causal_lab] list_templates ERROR: {e}\n{traceback.format_exc()}", flush=True) logger.error(f"[causal_lab] list_templates: {e}") raise HTTPException(500, str(e)) diff --git a/frontend/src/pages/CausalLab.tsx b/frontend/src/pages/CausalLab.tsx index c9632ce..7210901 100644 --- a/frontend/src/pages/CausalLab.tsx +++ b/frontend/src/pages/CausalLab.tsx @@ -173,11 +173,21 @@ function TabLibrary() { const [saving, setSaving] = useState(false) const [editCoefs, setEditCoefs] = useState>({}) const [loading, setLoading] = useState(true) + const [apiError, setApiError] = useState(null) + const [debugInfo, setDebugInfo] = useState | null>(null) useEffect(() => { - setLoading(true) + setLoading(true); setApiError(null) api(`/api/causal-lab/templates${catFilter ? `?category=${catFilter}` : ''}`) - .then(setTemplates).finally(() => setLoading(false)) + .then(data => { + setTemplates(data) + if (data.length === 0) { + // Auto-run debug to show diagnostic + api('/api/causal-lab/debug').then(setDebugInfo).catch(() => {}) + } + }) + .catch(e => setApiError(String(e))) + .finally(() => setLoading(false)) }, [catFilter]) function selectTemplate(t: Template) { @@ -213,6 +223,18 @@ function TabLibrary() { {cats.map(c => )} + {apiError && ( +
+
Erreur API
+ {apiError} +
+ )} + {debugInfo && ( +
+
Diagnostic
+
{JSON.stringify(debugInfo, null, 2)}
+
+ )} {loading ?
Chargement…
: