Kerikim commited on
Commit
89d9b5c
·
1 Parent(s): 04ada98

elkay frontend lesson.py

Browse files
Files changed (1) hide show
  1. phase/Student_view/lesson.py +52 -20
phase/Student_view/lesson.py CHANGED
@@ -6,8 +6,38 @@ from typing import List, Dict, Tuple, Optional
6
 
7
  import streamlit as st
8
  from utils import db as dbapi
 
9
 
 
10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  # ========== Optional direct agent imports (same process) ==========
12
  AGENTS_AVAILABLE = False
13
  try:
@@ -316,25 +346,27 @@ def _backend_post(path: str, payload: dict, timeout=6) -> dict | None:
316
  pass
317
  return None
318
 
319
- def fetch_auto_quiz_from_backend() -> List[Dict] | None:
320
- # Uses your legacy stub: /agent/quiz returns {"items":[{question,options,answer_key,points}...]}
321
- resp = _backend_post("/agents/quiz/auto",
322
- {"lesson": lesson_title, "module": module_id, "topic": topic_name}) \
323
- or _backend_post("/agent/quiz", {"lesson_id": module_id, "level_slug": "beginner"})
324
- items = None
325
- if resp:
326
- items = resp.get("questions") or resp.get("items")
327
- if items and isinstance(items, list):
328
- out = []
329
- for i, it in enumerate(items, start=1):
330
- out.append({
331
- "id": it.get("id") or f"auto_{i}",
332
- "question": it.get("question",""),
333
- "options": it.get("options", []),
334
- "answer_key": it.get("answer_key","A")
335
- })
336
- return out
337
- return None
 
 
338
 
339
  def grade_and_chat_via_backend(lesson_title: str, module_id: str, topic_name: str,
340
  responses: Dict[str, str]) -> Tuple[int, int, str]:
@@ -848,7 +880,7 @@ def render_assigned_lesson(lesson_id: int, assignment_id: int | None = None):
848
  user = st.session_state.user
849
  user_id = user["user_id"]
850
 
851
- data = dbapi.get_lesson(lesson_id) # {"lesson": {...}, "sections":[...] }
852
  if not data or not data.get("lesson"):
853
  st.error("Lesson not found.")
854
  if st.button("⬅ Back to classes"):
 
6
 
7
  import streamlit as st
8
  from utils import db as dbapi
9
+ import utils.api as api
10
 
11
+ USE_LOCAL_DB = os.getenv("DISABLE_DB", "1") != "1"
12
 
13
+ def _api_post(path: str, payload: dict):
14
+ # try to reuse your utils.api client; gracefully no-op if unavailable
15
+ try:
16
+ if hasattr(api, "_req"):
17
+ return api._req("POST", path, json=payload)
18
+ except Exception:
19
+ pass
20
+ return None
21
+
22
+ def _get_lesson(lesson_id: int):
23
+ """Fetch a lesson from local DB if enabled, otherwise from the backend API."""
24
+ if USE_LOCAL_DB and hasattr(dbapi, "get_lesson"):
25
+ return dbapi.get_lesson(lesson_id)
26
+ return api.get_lesson(lesson_id)
27
+
28
+ def _save_progress(user_id: int, lesson_id: int, current_pos: int, status: str):
29
+ """Best-effort save; don’t crash the UI if the backend route is missing."""
30
+ try:
31
+ if USE_LOCAL_DB and hasattr(dbapi, "save_progress"):
32
+ return dbapi.save_progress(user_id, lesson_id, current_pos, status)
33
+ # If you exposed an HTTP route in the backend (see note below), you can call it here.
34
+ # Otherwise we silently skip saving when the DB is disabled.
35
+ return True
36
+ except Exception:
37
+ # Keep the lesson page usable even if the save call fails.
38
+ return False
39
+
40
+
41
  # ========== Optional direct agent imports (same process) ==========
42
  AGENTS_AVAILABLE = False
43
  try:
 
346
  pass
347
  return None
348
 
349
+ def fetch_auto_quiz_from_backend(lesson_title: str, module_id: str, topic_name: str):
350
+ # Try new agents path first; fall back to legacy if present; else None
351
+ resp = _api_post("/agents/quiz/auto",
352
+ {"lesson": lesson_title, "module": module_id, "topic": topic_name}) \
353
+ or _api_post("/agent/quiz", {"lesson_id": module_id, "level_slug": "beginner"})
354
+ if not resp:
355
+ return None
356
+
357
+ items = resp.get("questions") or resp.get("items")
358
+ if not isinstance(items, list):
359
+ return None
360
+
361
+ out = []
362
+ for i, it in enumerate(items, start=1):
363
+ out.append({
364
+ "id": it.get("id") or f"auto_{i}",
365
+ "question": it.get("question", ""),
366
+ "options": it.get("options", []),
367
+ "answer_key": (it.get("answer_key") or "A").strip().upper()
368
+ })
369
+ return out
370
 
371
  def grade_and_chat_via_backend(lesson_title: str, module_id: str, topic_name: str,
372
  responses: Dict[str, str]) -> Tuple[int, int, str]:
 
880
  user = st.session_state.user
881
  user_id = user["user_id"]
882
 
883
+ data = _get_lesson(lesson_id) # {"lesson": {...}, "sections":[...] }
884
  if not data or not data.get("lesson"):
885
  st.error("Lesson not found.")
886
  if st.button("⬅ Back to classes"):