File size: 15,287 Bytes
0aa6283 14f0eb2 e432420 0aa6283 14f0eb2 0aa6283 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 | import streamlit as st
st.set_page_config(
page_title="Financial Education App",
page_icon="πΉ",
layout="centered",
initial_sidebar_state="expanded"
)
from secrets import choice
from dashboards import student_db,teacher_db
from phase.Student_view import chatbot, lesson, quiz, game, teacherlink
from phase.Teacher_view import classmanage,studentlist,contentmanage
from phase.Student_view.games import profitpuzzle
from utils import db,api
from phase import welcome
import os, requests
DISABLE_DB = os.getenv("DISABLE_DB", "1") == "1"
# --- SESSION STATE INITIALIZATION ---
for key, default in [("user", None), ("current_page", "Welcome"),
("xp", 2450), ("streak", 7), ("current_game", None),
("temp_user", None)]:
if key not in st.session_state:
st.session_state[key] = default
# --- NAVIGATION ---
def setup_navigation():
if st.session_state.user:
public_pages = ["Welcome", "Login"]
else:
public_pages = ["Welcome", "Signup", "Login"]
nav_choice = st.sidebar.selectbox(
"Go to",
public_pages,
index=public_pages.index(st.session_state.current_page) if st.session_state.current_page in public_pages else 0
)
# --- if quiz is in progress, show progress tracker ---
if st.session_state.get("current_page") == "Quiz":
qid = st.session_state.get("selected_quiz")
if qid is not None:
try:
quiz.show_quiz_progress_sidebar(qid) # renders into sidebar
except Exception:
pass
# --- if profit puzzle game is in progress, show progress tracker ---
if (
st.session_state.get("current_page") == "Game"
and st.session_state.get("current_game") == "profit_puzzle"
):
profitpuzzle.show_profit_progress_sidebar()
# Only override if user is already on a public page
if st.session_state.current_page in public_pages:
st.session_state.current_page = nav_choice
if st.session_state.user:
st.sidebar.markdown("---")
st.sidebar.subheader("Dashboard")
role = st.session_state.user["role"]
if role == "Student":
if st.sidebar.button("π Student Dashboard"):
st.session_state.current_page = "Student Dashboard"
if st.sidebar.button("π Lessons"):
st.session_state.current_page = "Lessons"
if st.sidebar.button("π Quiz"):
st.session_state.current_page = "Quiz"
if st.sidebar.button("π¬ Chatbot"):
st.session_state.current_page = "Chatbot"
if st.sidebar.button("π Game"):
st.session_state.current_page = "Game"
if st.sidebar.button("β¨οΈβ Teacher Link"):
st.session_state.current_page = "Teacher Link"
elif role == "Teacher":
if st.sidebar.button("π Teacher Dashboard"):
st.session_state.current_page = "Teacher Dashboard"
if st.sidebar.button("Class management"):
st.session_state.current_page = "Class management"
if st.sidebar.button("Students List"):
st.session_state.current_page = "Students List"
if st.sidebar.button("Content Management"):
st.session_state.current_page = "Content Management"
if st.sidebar.button("Logout"):
st.session_state.user = None
st.session_state.current_page = "Welcome"
st.rerun()
# --- ROUTING ---
def main():
setup_navigation()
page = st.session_state.current_page
# --- WELCOME PAGE ---
if page == "Welcome":
welcome.show_page()
# --- SIGNUP PAGE ---
elif page == "Signup":
st.title("π Signup")
# remember the picked role between reruns
if "signup_role" not in st.session_state:
st.session_state.signup_role = None
if st.session_state.user:
st.success(f"Already logged in as {st.session_state.user['name']}.")
st.stop()
# Step 1: choose role
if not st.session_state.signup_role:
st.subheader("Who are you signing up as?")
c1, c2 = st.columns(2)
with c1:
if st.button("π©βπ Student", use_container_width=True):
st.session_state.signup_role = "Student"
st.rerun()
with c2:
if st.button("π¨βπ« Teacher", use_container_width=True):
st.session_state.signup_role = "Teacher"
st.rerun()
st.info("Pick your role to continue with the correct form.")
st.stop()
role = st.session_state.signup_role
# Step 2a: Student form
if role == "Student":
st.subheader("Student Signup")
with st.form("student_signup_form", clear_on_submit=False):
name = st.text_input("Full Name")
email = st.text_input("Email")
password = st.text_input("Password", type="password")
country = st.selectbox("Country", ["Jamaica", "USA", "UK", "India", "Canada", "Other"])
level = st.selectbox("Level", ["Beginner", "Intermediate", "Advanced"])
submitted = st.form_submit_button("Create Student Account")
if submitted:
if not (name.strip() and email.strip() and password.strip()):
st.error("β οΈ Please complete all required fields.")
st.stop()
if DISABLE_DB:
try:
api.signup_student(
name=name.strip(),
email=email.strip().lower(),
password=password,
level_label=level, # <-- keep these names
country_label=country,
)
st.success("β
Signup successful! Please go to the **Login** page to continue.")
st.session_state.current_page = "Login"
st.session_state.signup_role = None
st.rerun()
except Exception as e:
st.error(f"β Signup failed: {e}")
else:
# Local DB path (unchanged)
conn = db.get_db_connection()
if not conn:
st.error("β Unable to connect to the database.")
st.stop()
try:
ok = db.create_student(
name=name, email=email, password=password,
level_label=level, country_label=country
)
if ok:
st.success("β
Signup successful! Please go to the **Login** page to continue.")
st.session_state.current_page = "Login"
st.session_state.signup_role = None
st.rerun()
else:
st.error("β Failed to create user. Email may already exist.")
finally:
conn.close()
# Step 2b: Teacher form
elif role == "Teacher":
st.subheader("Teacher Signup")
with st.form("teacher_signup_form", clear_on_submit=False):
title = st.selectbox("Title", ["Mr", "Ms", "Miss", "Mrs", "Dr", "Prof", "Other"])
name = st.text_input("Full Name")
email = st.text_input("Email")
password = st.text_input("Password", type="password")
submitted = st.form_submit_button("Create Teacher Account")
if submitted:
if not (title.strip() and name.strip() and email.strip() and password.strip()):
st.error("β οΈ Please complete all required fields.")
st.stop()
if DISABLE_DB:
try:
api.signup_teacher(
title=title.strip(),
name=name.strip(),
email=email.strip().lower(),
password=password,
)
st.success("β
Signup successful! Please go to the **Login** page to continue.")
st.session_state.current_page = "Login"
st.session_state.signup_role = None
st.rerun()
except Exception as e:
st.error(f"β Signup failed: {e}")
else:
conn = db.get_db_connection()
if not conn:
st.error("β Unable to connect to the database.")
st.stop()
try:
ok = db.create_teacher(
title=title, name=name, email=email, password=password
)
if ok:
st.success("β
Signup successful! Please go to the **Login** page to continue.")
st.session_state.current_page = "Login"
st.session_state.signup_role = None
st.rerun()
else:
st.error("β Failed to create user. Email may already exist.")
finally:
conn.close()
# Allow changing role without going back manually
if st.button("β¬
οΈ Choose a different role"):
st.session_state.signup_role = None
st.rerun()
# --- LOGIN PAGE ---
elif page == "Login":
st.title("π Login")
if st.session_state.user:
st.success(f"Welcome, {st.session_state.user['name']}! β
")
else:
with st.form("login_form"):
email = st.text_input("Email")
password = st.text_input("Password", type="password")
submit = st.form_submit_button("Login")
if submit:
if DISABLE_DB:
# Route login to your Backend Space
try:
user = api.login(email, password) # calls POST /auth/login
# Normalize to the structure your app already uses
st.session_state.user = {
"user_id": user["user_id"],
"name": user["name"],
"role": user["role"], # "Student" or "Teacher" from backend
"email": user["email"],
}
st.success(f"π Logged in as {user['name']} ({user['role']})")
st.session_state.current_page = (
"Student Dashboard" if user["role"] == "Student" else "Teacher Dashboard"
)
st.rerun()
except Exception as e:
st.error(f"Login failed. {e}")
else:
# Local fallback: keep your old direct-DB logic
conn = db.get_db_connection()
if not conn:
st.error("β Unable to connect to the database.")
else:
try:
user = db.check_password(email, password)
if user:
st.session_state.user = {
"user_id": user["user_id"],
"name": user["name"],
"role": user["role"], # "Student" or "Teacher"
"email": user["email"],
}
st.success(f"π Logged in as {user['name']} ({user['role']})")
st.session_state.current_page = (
"Student Dashboard" if user["role"] == "Student" else "Teacher Dashboard"
)
st.rerun()
else:
st.error("β Incorrect email or password, or account not found.")
finally:
conn.close()
# --- STUDENT DASHBOARD ---
elif page == "Student Dashboard":
if not st.session_state.user:
st.error("β Please login first.")
st.session_state.current_page = "Login"
st.rerun()
elif st.session_state.user["role"] != "Student":
st.error("π« Only students can access this page.")
st.session_state.current_page = "Welcome"
st.rerun()
else:
student_db.show_student_dashboard()
# --- TEACHER DASHBOARD ---
elif page == "Teacher Dashboard":
if not st.session_state.user:
st.error("β Please login first.")
st.session_state.current_page = "Login"
st.rerun()
elif st.session_state.user["role"] != "Teacher":
st.error("π« Only teachers can access this page.")
st.session_state.current_page = "Welcome"
st.rerun()
else:
teacher_db.show_teacher_dashboard()
# --- PRIVATE PAGES ---
private_pages_map = {
"Lessons": lesson.show_page,
"Quiz": quiz.show_page,
"Chatbot": chatbot.show_page,
"Game": game.show_games,
"Teacher Link": teacherlink.show_code,
"Class management": classmanage.show_page,
"Students List": studentlist.show_page,
"Content Management": contentmanage.show_page
}
if page in private_pages_map:
if not st.session_state.user:
st.error("β Please login first.")
st.session_state.current_page = "Login"
st.rerun()
elif page in ["Lessons", "Quiz", "Chatbot", "Game", "Teacher Link"] and st.session_state.user["role"] == "Student":
private_pages_map[page]()
elif page in ["Class management", "Students List", "Content Management"] and st.session_state.user["role"] == "Teacher":
private_pages_map[page]()
else:
st.error("π« You donβt have access to this page.")
st.session_state.current_page = "Welcome"
st.rerun()
if __name__ == "__main__":
main()
|