lanna_lalala;- commited on
Commit ·
4605217
1
Parent(s): 7c45ed6
Try again 2
Browse files- dashboards/student_db.py +34 -4
- utils/api.py +11 -0
dashboards/student_db.py
CHANGED
|
@@ -2,6 +2,9 @@ import os
|
|
| 2 |
import streamlit as st
|
| 3 |
from phase.Student_view import chatbot, lesson, quiz
|
| 4 |
from utils import db as dbapi
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# --- Load external CSS ---
|
| 7 |
def load_css(file_name: str):
|
|
@@ -23,7 +26,15 @@ def show_student_dashboard():
|
|
| 23 |
|
| 24 |
# --- Real metrics from DB ---
|
| 25 |
# Requires helper funcs in utils/db.py: user_xp_and_level, recent_lessons_for_student, list_assignments_for_student
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
xp = int(stats.get("xp", 0))
|
| 28 |
level = int(stats.get("level", 1))
|
| 29 |
study_streak = int(stats.get("streak", 0))
|
|
@@ -32,7 +43,13 @@ def show_student_dashboard():
|
|
| 32 |
max_xp = max(500, ((xp // 500) + 1) * 500)
|
| 33 |
|
| 34 |
# Assignments for “My Work”
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
def _pct_from_row(r: dict):
|
| 38 |
sp = r.get("score_pct")
|
|
@@ -49,12 +66,25 @@ def show_student_dashboard():
|
|
| 49 |
return None
|
| 50 |
return None
|
| 51 |
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
lessons_completed = sum(1 for r in rows if r.get("status") == "completed" or _pct_from_row(r) == 100)
|
| 54 |
total_lessons = len(rows)
|
| 55 |
|
| 56 |
# Recent lessons assigned to this student
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
# Daily Challenge derived from real data
|
| 60 |
challenge_difficulty = "Easy" if level < 3 else ("Medium" if level < 6 else "Hard")
|
|
|
|
| 2 |
import streamlit as st
|
| 3 |
from phase.Student_view import chatbot, lesson, quiz
|
| 4 |
from utils import db as dbapi
|
| 5 |
+
import utils.api as api # <-- backend Space client
|
| 6 |
+
|
| 7 |
+
USE_LOCAL_DB = os.getenv("DISABLE_DB", "1") != "1"
|
| 8 |
|
| 9 |
# --- Load external CSS ---
|
| 10 |
def load_css(file_name: str):
|
|
|
|
| 26 |
|
| 27 |
# --- Real metrics from DB ---
|
| 28 |
# Requires helper funcs in utils/db.py: user_xp_and_level, recent_lessons_for_student, list_assignments_for_student
|
| 29 |
+
if USE_LOCAL_DB and hasattr(dbapi, "user_xp_and_level"):
|
| 30 |
+
stats = dbapi.user_xp_and_level(student_id)
|
| 31 |
+
else:
|
| 32 |
+
# Try backend; fall back to defaults if not available yet
|
| 33 |
+
try:
|
| 34 |
+
stats = api.user_stats(student_id)
|
| 35 |
+
except Exception:
|
| 36 |
+
stats = {"xp": 0, "level": 1, "streak": 0}
|
| 37 |
+
|
| 38 |
xp = int(stats.get("xp", 0))
|
| 39 |
level = int(stats.get("level", 1))
|
| 40 |
study_streak = int(stats.get("streak", 0))
|
|
|
|
| 43 |
max_xp = max(500, ((xp // 500) + 1) * 500)
|
| 44 |
|
| 45 |
# Assignments for “My Work”
|
| 46 |
+
if USE_LOCAL_DB and hasattr(dbapi, "list_assignments_for_student"):
|
| 47 |
+
rows = dbapi.list_assignments_for_student(student_id)
|
| 48 |
+
else:
|
| 49 |
+
try:
|
| 50 |
+
rows = api.list_assignments_for_student(student_id)
|
| 51 |
+
except Exception:
|
| 52 |
+
rows = []
|
| 53 |
|
| 54 |
def _pct_from_row(r: dict):
|
| 55 |
sp = r.get("score_pct")
|
|
|
|
| 66 |
return None
|
| 67 |
return None
|
| 68 |
|
| 69 |
+
if USE_LOCAL_DB and hasattr(dbapi, "student_quiz_average"):
|
| 70 |
+
quiz_score = dbapi.student_quiz_average(student_id)
|
| 71 |
+
else:
|
| 72 |
+
try:
|
| 73 |
+
quiz_score = api.student_quiz_average(student_id)
|
| 74 |
+
except Exception:
|
| 75 |
+
quiz_score = 0
|
| 76 |
+
|
| 77 |
lessons_completed = sum(1 for r in rows if r.get("status") == "completed" or _pct_from_row(r) == 100)
|
| 78 |
total_lessons = len(rows)
|
| 79 |
|
| 80 |
# Recent lessons assigned to this student
|
| 81 |
+
if USE_LOCAL_DB and hasattr(dbapi, "recent_lessons_for_student"):
|
| 82 |
+
recent_lessons = dbapi.recent_lessons_for_student(student_id, limit=5)
|
| 83 |
+
else:
|
| 84 |
+
try:
|
| 85 |
+
recent_lessons = api.recent_lessons_for_student(student_id, limit=5)
|
| 86 |
+
except Exception:
|
| 87 |
+
recent_lessons = []
|
| 88 |
|
| 89 |
# Daily Challenge derived from real data
|
| 90 |
challenge_difficulty = "Easy" if level < 3 else ("Medium" if level < 6 else "Hard")
|
utils/api.py
CHANGED
|
@@ -85,6 +85,17 @@ def health():
|
|
| 85 |
return {"ok": True}
|
| 86 |
except Exception:
|
| 87 |
return {"ok": False}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
# ---- Legacy agent endpoints (keep) ----
|
| 90 |
def start_agent(student_id: int, lesson_id: int, level_slug: str):
|
|
|
|
| 85 |
return {"ok": True}
|
| 86 |
except Exception:
|
| 87 |
return {"ok": False}
|
| 88 |
+
|
| 89 |
+
#---helpers
|
| 90 |
+
def user_stats(student_id: int):
|
| 91 |
+
return _req("GET", f"/students/{student_id}/stats").json()
|
| 92 |
+
def list_assignments_for_student(student_id: int):
|
| 93 |
+
return _req("GET", f"/students/{student_id}/assignments").json()
|
| 94 |
+
def student_quiz_average(student_id: int):
|
| 95 |
+
return _req("GET", f"/students/{student_id}/quiz_avg").json()
|
| 96 |
+
def recent_lessons_for_student(student_id: int, limit: int = 5):
|
| 97 |
+
return _req("GET", f"/students/{student_id}/recent", params={"limit": limit}).json()
|
| 98 |
+
|
| 99 |
|
| 100 |
# ---- Legacy agent endpoints (keep) ----
|
| 101 |
def start_agent(student_id: int, lesson_id: int, level_slug: str):
|