import os import streamlit as st from phase.Student_view import chatbot, lesson, quiz from utils import db as dbapi import utils.api as api # <-- backend Space client USE_LOCAL_DB = os.getenv("DISABLE_DB", "1") != "1" # --- Load external CSS --- def load_css(file_name: str): try: with open(file_name, "r", encoding="utf-8") as f: st.markdown(f"", unsafe_allow_html=True) except FileNotFoundError: st.warning("β οΈ Stylesheet not found. Please ensure 'assets/styles.css' exists.") def show_student_dashboard(): # Load CSS css_path = os.path.join("assets", "styles.css") load_css(css_path) # Current user user = st.session_state.user name = user["name"] student_id = user["user_id"] # --- Real metrics from DB --- # Requires helper funcs in utils/db.py: user_xp_and_level, recent_lessons_for_student, list_assignments_for_student if USE_LOCAL_DB and hasattr(dbapi, "user_xp_and_level"): stats = dbapi.user_xp_and_level(student_id) else: # Try backend; fall back to defaults if not available yet try: stats = api.user_stats(student_id) except Exception: stats = {"xp": 0, "level": 1, "streak": 0} xp = int(stats.get("xp", 0)) level = int(stats.get("level", 1)) study_streak = int(stats.get("streak", 0)) # # Cap for the visual bar # max_xp = max(500, ((xp // 500) + 1) * 500) # Assignments for βMy Workβ if USE_LOCAL_DB and hasattr(dbapi, "list_assignments_for_student"): rows = dbapi.list_assignments_for_student(student_id) else: try: rows = api.list_assignments_for_student(student_id) except Exception: rows = [] def _pct_from_row(r: dict): sp = r.get("score_pct") if sp is not None: try: return int(round(float(sp))) except Exception: pass s, t = r.get("score"), r.get("total") if s is not None and t not in (None, 0): try: return int(round((float(s) / float(t)) * 100)) except Exception: return None return None if USE_LOCAL_DB and hasattr(dbapi, "student_quiz_average"): quiz_score = dbapi.student_quiz_average(student_id) else: try: quiz_score = api.student_quiz_average(student_id) except Exception: quiz_score = 0 lessons_completed = sum(1 for r in rows if r.get("status") == "completed" or _pct_from_row(r) == 100) total_lessons = len(rows) # Recent lessons assigned to this student if USE_LOCAL_DB and hasattr(dbapi, "recent_lessons_for_student"): recent_lessons = dbapi.recent_lessons_for_student(student_id, limit=5) else: try: recent_lessons = api.recent_lessons_for_student(student_id, limit=5) except Exception: recent_lessons = [] # Daily Challenge derived from real data challenge_difficulty = "Easy" if level < 3 else ("Medium" if level < 6 else "Hard") challenge_title = "Complete 1 quiz with 80%+" challenge_desc = "Prove you remember yesterday's key points." challenge_progress = 100 if quiz_score >= 80 else 0 reward = "+50 XP" time_left = "Ends 11:59 PM" # Achievements from real data achievements = [ {"title": "First Steps", "desc": "Complete your first lesson", "earned": lessons_completed > 0}, {"title": "Quiz Whiz", "desc": "Score 80%+ on any quiz", "earned": quiz_score >= 80}, {"title": "On a Roll", "desc": "Study 3 days in a row", "earned": study_streak >= 3}, {"title": "Consistency", "desc": "Finish 5 assignments", "earned": total_lessons >= 5 and lessons_completed >= 5}, ] # --- Welcome Card --- st.markdown( f"""
{"Ready to continue your financial journey?" if lessons_completed > 0 else "Start your financial journey."}