| 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 |
| import os, requests |
|
|
| from utils.api import BACKEND |
| st.sidebar.caption(f"Backend URL: {BACKEND}") |
|
|
|
|
| DISABLE_DB = os.getenv("DISABLE_DB", "1") == "1" |
|
|
| try: |
| ok = api.health().get("ok") |
| if ok: |
| st.sidebar.success("Backend: UP") |
| else: |
| st.sidebar.warning("Backend: ?") |
| except Exception as e: |
| st.sidebar.error(f"Backend DOWN: {e}") |
|
|
| |
| |
| 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 |
|
|
| |
| 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 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) |
| except Exception: |
| pass |
|
|
| |
| |
| if ( |
| st.session_state.get("current_page") == "Game" |
| and st.session_state.get("current_game") == "profit_puzzle" |
| ): |
| profitpuzzle.show_profit_progress_sidebar() |
|
|
| |
| 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() |
|
|
|
|
| |
| def main(): |
| setup_navigation() |
| page = st.session_state.current_page |
|
|
| |
| if page == "Welcome": |
| st.title("πΉ Welcome to FinEdu App") |
| if st.session_state.user: |
| st.success(f"Welcome back, {st.session_state.user['name']}! β
") |
| st.write( |
| "This app helps you improve your **financial education and numeracy skills**. \n" |
| "π Use the sidebar to **Signup** or **Login** to get started." |
| ) |
|
|
| |
| elif page == "Signup": |
| st.title("π Signup") |
|
|
| |
| 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() |
|
|
| |
| 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 |
|
|
| |
| 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, |
| 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: |
| |
| 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() |
|
|
| |
| 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() |
|
|
| |
| if st.button("β¬
οΈ Choose a different role"): |
| st.session_state.signup_role = None |
| st.rerun() |
|
|
| |
| 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: |
| |
| try: |
| user = api.login(email, password) |
| |
| st.session_state.user = { |
| "user_id": user["user_id"], |
| "name": user["name"], |
| "role": user["role"], |
| "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: |
| |
| 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"], |
| "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() |
|
|
| |
| 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() |
|
|
| |
| 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_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() |
|
|
|
|