File size: 4,111 Bytes
e3f0784
 
 
9a6b7dc
0759822
a962ffe
 
9a6b7dc
897c1d2
 
0759822
 
 
90cb4f4
9a6b7dc
897c1d2
e3f0784
00eef23
9a6b7dc
0759822
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
897c1d2
9a6b7dc
 
 
 
e4a1a2b
 
9a6b7dc
 
 
 
0759822
 
 
 
 
 
 
 
9a6b7dc
 
897c1d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9a6b7dc
 
 
0759822
 
9a6b7dc
 
 
 
90cb4f4
0759822
9a6b7dc
 
0759822
897c1d2
9a6b7dc
 
e4a1a2b
0759822
9a6b7dc
0759822
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9a6b7dc
 
 
 
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
import json
from typing import Dict

from db.schema import Feedback, Response
from db.crud import ingest, read, save_feedback
import pandas as pd
import streamlit as st
from datetime import datetime
import os
from dotenv import load_dotenv
from views.intro_screen import welcome_screen
from views.questions_screen import questions_screen, survey_completed
from views.continue_survey import continue_survey_screen
from css.layout import custom_css

load_dotenv()
VALIDATION_CODE = os.getenv("VALIDATION_CODE")


# class SurveyState:
#     """Class to handle survey state management"""
#
#     def __init__(self):
#         pass
#
#     def save_state(self, username: str, current_state: Dict) -> None:
#         """Save current state to Firebase"""
#         try:
#             """Handles feedback submission to the database."""
#             feedback = Feedback(
#                 id=st.session_state.current_index + 1,
#                 user_id=st.session_state.username,
#                 time_stamp=datetime.now().isoformat(),
#                 responses=st.session_state.responses,
#             )
#             save_feedback(feedback)
#             st.success("Your progress has been saved! You can continue later.")
#             st.session_state.completed = True
#             st.rerun()
#         except Exception as e:
#             st.error(f"An error occurred while submitting feedback: {e}")
#

def initialization():
    """Initialize session state variables."""
    if "current_index" not in st.session_state:
        st.session_state.current_index = 0
    if "username" not in st.session_state:
        st.session_state.username = None
    if "responses" not in st.session_state:
        st.session_state.responses = []
    if "completed" not in st.session_state:
        st.session_state.completed = False
    if "show_questions" not in st.session_state:
        st.session_state.show_questions = False
    if "survey_continued" not in st.session_state:
        st.session_state.survey_continued = None
    if "start_new_survey" not in st.session_state:
        st.session_state.start_new_survey = False
    # if "survey_state" not in st.session_state:
    #     st.session_state.survey_state = SurveyState()


def exit_screen():
    """Display exit screen"""
    st.markdown("""
        <div class='exit-container'>
            <h1>Thank you for participating!</h1>
            <p>Your responses have been saved successfully.</p>
            <p>You can safely close this window or start a new survey.</p>
        </div>
    """, unsafe_allow_html=True)

    if st.button("Start New Survey"):
        reset_survey()
        st.rerun()


def reset_survey():
    """Reset the survey state to start over."""
    st.session_state.responses = []
    st.session_state.completed = True #TODO: Change to False?
    st.session_state.start_new_survey = True


def ui():
    """Main function to control the survey flow."""
    custom_css()
    data = pd.read_csv("data/gemini_results_subset.csv")[:5]
    initialization()

    if st.session_state.completed and not st.session_state.start_new_survey:
        exit_screen()
        return

    if st.session_state.username is None:
        welcome_screen()
    else:
        # Check if user progress exists in Firebase
        saved_state = read(st.session_state.username)
        if saved_state:
            # If there's saved progress and the survey has not been continued, show continue screen
            if "survey_continued" not in st.session_state or not st.session_state.survey_continued:
                continue_survey_screen(data)
            else:
                if st.session_state.current_index >= len(data):
                # If all questions have been answered, show the exit screen
                    print("survey completed")
                    survey_completed()
                # Otherwise, show questions from where they left off
                questions_screen(data)
        else:
            # If no saved progress (new user), start with the questions screen
            questions_screen(data)


if __name__ == "__main__":
    ui()