Spaces:
Sleeping
Sleeping
Ashmi Banerjee
commited on
Commit
·
e4a1a2b
1
Parent(s):
e3f0784
added next button on the intro screen
Browse files- app.py +68 -38
- db/crud.py +31 -40
app.py
CHANGED
|
@@ -2,7 +2,7 @@ import json
|
|
| 2 |
from typing import Dict
|
| 3 |
|
| 4 |
from db.schema import Feedback, Response
|
| 5 |
-
from db.crud import ingest
|
| 6 |
import pandas as pd
|
| 7 |
import streamlit as st
|
| 8 |
from datetime import datetime
|
|
@@ -17,43 +17,72 @@ class SurveyState:
|
|
| 17 |
"""Class to handle survey state management"""
|
| 18 |
|
| 19 |
def __init__(self):
|
| 20 |
-
|
| 21 |
|
| 22 |
def save_state(self, prolific_id: str, current_state: Dict) -> None:
|
| 23 |
-
"""Save current state to
|
| 24 |
try:
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
states[prolific_id] = current_state
|
| 32 |
-
|
| 33 |
-
with open(self.state_file, 'w') as f:
|
| 34 |
-
json.dump(states, f)
|
| 35 |
-
|
| 36 |
except Exception as e:
|
| 37 |
-
st.error(f"Error saving state: {e}")
|
| 38 |
|
| 39 |
def load_state(self, prolific_id: str) -> Dict:
|
| 40 |
-
"""Load state for a specific user"""
|
| 41 |
try:
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
return
|
| 46 |
except Exception as e:
|
| 47 |
-
st.error(f"Error loading state: {e}")
|
| 48 |
return {}
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
def initialization():
|
| 52 |
"""Initialize session state variables."""
|
| 53 |
if "current_index" not in st.session_state:
|
| 54 |
st.session_state.current_index = 0
|
| 55 |
-
if "
|
| 56 |
-
st.session_state.
|
| 57 |
if "responses" not in st.session_state:
|
| 58 |
st.session_state.responses = []
|
| 59 |
if "completed" not in st.session_state:
|
|
@@ -62,8 +91,8 @@ def initialization():
|
|
| 62 |
st.session_state.survey_state = SurveyState()
|
| 63 |
|
| 64 |
|
| 65 |
-
def
|
| 66 |
-
return bool(
|
| 67 |
|
| 68 |
|
| 69 |
def validate_code(input_code: str) -> bool:
|
|
@@ -71,20 +100,21 @@ def validate_code(input_code: str) -> bool:
|
|
| 71 |
return input_code.strip() == VALIDATION_CODE
|
| 72 |
|
| 73 |
|
| 74 |
-
def
|
| 75 |
"""Display the Prolific ID entry screen."""
|
| 76 |
st.title("Welcome to the Feedback Survey")
|
| 77 |
|
| 78 |
-
|
| 79 |
-
validation_code_input = st.text_input("Enter the validation code to proceed:")
|
| 80 |
|
| 81 |
-
|
|
|
|
| 82 |
# Validate Prolific ID and code
|
| 83 |
-
if
|
| 84 |
-
st.session_state.
|
| 85 |
|
| 86 |
# Load previous state if exists
|
| 87 |
-
saved_state = st.session_state.survey_state.load_state(
|
| 88 |
if saved_state:
|
| 89 |
st.session_state.current_index = saved_state.get('current_index', 0)
|
| 90 |
st.session_state.responses = saved_state.get('responses', [])
|
|
@@ -93,7 +123,7 @@ def prolific_id_screen():
|
|
| 93 |
st.success("Prolific ID and code validated! Starting new survey.")
|
| 94 |
st.rerun()
|
| 95 |
else:
|
| 96 |
-
if not
|
| 97 |
st.warning("Invalid Prolific ID. Please check and try again.")
|
| 98 |
if not validate_code(validation_code_input):
|
| 99 |
st.warning("Invalid validation code. Please check and try again.")
|
|
@@ -169,7 +199,7 @@ def navigation_buttons(data, rating_v, rating_p0, rating_p1):
|
|
| 169 |
else:
|
| 170 |
feedback = Feedback(
|
| 171 |
id=current_index + 1,
|
| 172 |
-
user_id=st.session_state.
|
| 173 |
time_stamp=datetime.now().isoformat(),
|
| 174 |
responses=st.session_state.responses,
|
| 175 |
)
|
|
@@ -201,7 +231,7 @@ def submit_survey():
|
|
| 201 |
try:
|
| 202 |
feedback = Feedback(
|
| 203 |
id=st.session_state.current_index,
|
| 204 |
-
user_id=st.session_state.
|
| 205 |
time_stamp=datetime.now().isoformat(),
|
| 206 |
responses=st.session_state.responses,
|
| 207 |
)
|
|
@@ -215,7 +245,7 @@ def submit_survey():
|
|
| 215 |
|
| 216 |
def reset_survey():
|
| 217 |
"""Reset the survey state to start over."""
|
| 218 |
-
st.session_state.
|
| 219 |
st.session_state.current_index = 0
|
| 220 |
st.session_state.responses = []
|
| 221 |
st.session_state.completed = False
|
|
@@ -235,8 +265,8 @@ def ui():
|
|
| 235 |
exit_screen()
|
| 236 |
return
|
| 237 |
|
| 238 |
-
if st.session_state.
|
| 239 |
-
|
| 240 |
else:
|
| 241 |
questions_screen(data)
|
| 242 |
|
|
|
|
| 2 |
from typing import Dict
|
| 3 |
|
| 4 |
from db.schema import Feedback, Response
|
| 5 |
+
from db.crud import ingest, read
|
| 6 |
import pandas as pd
|
| 7 |
import streamlit as st
|
| 8 |
from datetime import datetime
|
|
|
|
| 17 |
"""Class to handle survey state management"""
|
| 18 |
|
| 19 |
def __init__(self):
|
| 20 |
+
pass
|
| 21 |
|
| 22 |
def save_state(self, prolific_id: str, current_state: Dict) -> None:
|
| 23 |
+
"""Save current state to Firebase"""
|
| 24 |
try:
|
| 25 |
+
# Saving to Firebase via the ingest function
|
| 26 |
+
feedback = Feedback(user_id=prolific_id, time_stamp=datetime.now().isoformat(),
|
| 27 |
+
responses=current_state["responses"])
|
| 28 |
+
ingest(feedback) # Ingest feedback to Firebase
|
| 29 |
+
st.success("Your progress has been saved! You can continue later.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
except Exception as e:
|
| 31 |
+
st.error(f"Error saving state to Firebase: {e}")
|
| 32 |
|
| 33 |
def load_state(self, prolific_id: str) -> Dict:
|
| 34 |
+
"""Load state for a specific user from Firebase"""
|
| 35 |
try:
|
| 36 |
+
# Retrieve the state from Firebase for the given Prolific ID
|
| 37 |
+
state = self.get_state_from_firebase(prolific_id)
|
| 38 |
+
if state:
|
| 39 |
+
return state
|
| 40 |
except Exception as e:
|
| 41 |
+
st.error(f"Error loading state from Firebase: {e}")
|
| 42 |
return {}
|
| 43 |
|
| 44 |
+
class SurveyState:
|
| 45 |
+
"""Class to handle survey state management"""
|
| 46 |
+
|
| 47 |
+
def __init__(self):
|
| 48 |
+
pass
|
| 49 |
+
|
| 50 |
+
@staticmethod
|
| 51 |
+
def save_state(self, username: str, current_state: Dict) -> None:
|
| 52 |
+
"""Save current state to Firebase"""
|
| 53 |
+
try:
|
| 54 |
+
# Saving to Firebase via the ingest function
|
| 55 |
+
feedback = Feedback(user_id=username, time_stamp=datetime.now().isoformat(),
|
| 56 |
+
responses=current_state["responses"])
|
| 57 |
+
ingest(feedback) # Ingest feedback to Firebase
|
| 58 |
+
st.success("Your progress has been saved! You can continue later.")
|
| 59 |
+
except Exception as e:
|
| 60 |
+
st.error(f"Error saving state to Firebase: {e}")
|
| 61 |
+
|
| 62 |
+
def load_state(self, username: str) -> Dict:
|
| 63 |
+
"""Load state for a specific user from Firebase"""
|
| 64 |
+
try:
|
| 65 |
+
# Retrieve the state from Firebase for the given Prolific ID
|
| 66 |
+
state = self.get_state_from_firebase(username)
|
| 67 |
+
if state:
|
| 68 |
+
return state
|
| 69 |
+
except Exception as e:
|
| 70 |
+
st.error(f"Error loading state from Firebase: {e}")
|
| 71 |
+
return {}
|
| 72 |
+
|
| 73 |
+
def get_state_from_firebase(self, username: str) -> Dict:
|
| 74 |
+
# Placeholder method to simulate getting state from Firebase
|
| 75 |
+
# In practice, you will query Firebase to get the current state
|
| 76 |
+
data = read(username)
|
| 77 |
+
return {}
|
| 78 |
+
|
| 79 |
|
| 80 |
def initialization():
|
| 81 |
"""Initialize session state variables."""
|
| 82 |
if "current_index" not in st.session_state:
|
| 83 |
st.session_state.current_index = 0
|
| 84 |
+
if "username" not in st.session_state:
|
| 85 |
+
st.session_state.username = None
|
| 86 |
if "responses" not in st.session_state:
|
| 87 |
st.session_state.responses = []
|
| 88 |
if "completed" not in st.session_state:
|
|
|
|
| 91 |
st.session_state.survey_state = SurveyState()
|
| 92 |
|
| 93 |
|
| 94 |
+
def validate_username(username: str) -> bool:
|
| 95 |
+
return bool(username.strip())
|
| 96 |
|
| 97 |
|
| 98 |
def validate_code(input_code: str) -> bool:
|
|
|
|
| 100 |
return input_code.strip() == VALIDATION_CODE
|
| 101 |
|
| 102 |
|
| 103 |
+
def username_screen():
|
| 104 |
"""Display the Prolific ID entry screen."""
|
| 105 |
st.title("Welcome to the Feedback Survey")
|
| 106 |
|
| 107 |
+
username_input = st.text_input("Enter your First name and press TAB:")
|
| 108 |
+
validation_code_input = st.text_input("Enter the validation code to proceed and press ENTER:")
|
| 109 |
|
| 110 |
+
next_button = st.button("Next")
|
| 111 |
+
if (username_input and validation_code_input) or next_button:
|
| 112 |
# Validate Prolific ID and code
|
| 113 |
+
if validate_username(username_input) and validate_code(validation_code_input):
|
| 114 |
+
st.session_state.username = username_input
|
| 115 |
|
| 116 |
# Load previous state if exists
|
| 117 |
+
saved_state = st.session_state.survey_state.load_state(username_input)
|
| 118 |
if saved_state:
|
| 119 |
st.session_state.current_index = saved_state.get('current_index', 0)
|
| 120 |
st.session_state.responses = saved_state.get('responses', [])
|
|
|
|
| 123 |
st.success("Prolific ID and code validated! Starting new survey.")
|
| 124 |
st.rerun()
|
| 125 |
else:
|
| 126 |
+
if not validate_username(username_input):
|
| 127 |
st.warning("Invalid Prolific ID. Please check and try again.")
|
| 128 |
if not validate_code(validation_code_input):
|
| 129 |
st.warning("Invalid validation code. Please check and try again.")
|
|
|
|
| 199 |
else:
|
| 200 |
feedback = Feedback(
|
| 201 |
id=current_index + 1,
|
| 202 |
+
user_id=st.session_state.username,
|
| 203 |
time_stamp=datetime.now().isoformat(),
|
| 204 |
responses=st.session_state.responses,
|
| 205 |
)
|
|
|
|
| 231 |
try:
|
| 232 |
feedback = Feedback(
|
| 233 |
id=st.session_state.current_index,
|
| 234 |
+
user_id=st.session_state.username,
|
| 235 |
time_stamp=datetime.now().isoformat(),
|
| 236 |
responses=st.session_state.responses,
|
| 237 |
)
|
|
|
|
| 245 |
|
| 246 |
def reset_survey():
|
| 247 |
"""Reset the survey state to start over."""
|
| 248 |
+
st.session_state.username = None
|
| 249 |
st.session_state.current_index = 0
|
| 250 |
st.session_state.responses = []
|
| 251 |
st.session_state.completed = False
|
|
|
|
| 265 |
exit_screen()
|
| 266 |
return
|
| 267 |
|
| 268 |
+
if st.session_state.username is None:
|
| 269 |
+
username_screen()
|
| 270 |
else:
|
| 271 |
questions_screen(data)
|
| 272 |
|
db/crud.py
CHANGED
|
@@ -25,9 +25,9 @@ def ingest(data: Feedback):
|
|
| 25 |
|
| 26 |
|
| 27 |
# Read operation (Fetch feedback data from Firebase)
|
| 28 |
-
def read(
|
| 29 |
ref = db_setup()
|
| 30 |
-
feedback_ref = ref.child('feedback').order_by_child('
|
| 31 |
|
| 32 |
if feedback_ref:
|
| 33 |
return feedback_ref
|
|
@@ -66,48 +66,39 @@ def delete(feedback_id: int):
|
|
| 66 |
|
| 67 |
def test():
|
| 68 |
# Create a feedback object
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
)
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
timestamp='2025-01-30T21:25:12.642038'),
|
| 85 |
-
Response(config_id='c_p_1_pop_medium_medium', rating_v=1, rating_p0=1, rating_p1=1, comment='',
|
| 86 |
-
timestamp='2025-01-30T21:25:13.854227'),
|
| 87 |
-
Response(config_id='c_p_2_pop_high_hard', rating_v=1, rating_p0=1, rating_p1=1, comment='',
|
| 88 |
-
timestamp='2025-01-30T21:25:15.581948'),
|
| 89 |
-
]
|
| 90 |
-
)
|
| 91 |
-
# Create (Ingest)
|
| 92 |
-
ingest(feedback)
|
| 93 |
|
| 94 |
# Read (Fetch)
|
| 95 |
-
feedback_data = read(
|
| 96 |
if feedback_data:
|
| 97 |
print(feedback_data)
|
| 98 |
|
| 99 |
# Update (Modify)
|
| 100 |
-
updated_feedback = Feedback(
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
)
|
| 109 |
-
update(1, updated_feedback)
|
| 110 |
-
|
| 111 |
-
# Delete (Remove)
|
| 112 |
-
delete(1)
|
| 113 |
|
|
|
|
| 25 |
|
| 26 |
|
| 27 |
# Read operation (Fetch feedback data from Firebase)
|
| 28 |
+
def read(user_id: str):
|
| 29 |
ref = db_setup()
|
| 30 |
+
feedback_ref = ref.child('feedback').order_by_child('user_id').equal_to(user_id).get()
|
| 31 |
|
| 32 |
if feedback_ref:
|
| 33 |
return feedback_ref
|
|
|
|
| 66 |
|
| 67 |
def test():
|
| 68 |
# Create a feedback object
|
| 69 |
+
# feedback = Feedback(
|
| 70 |
+
# id=2,
|
| 71 |
+
# user_id='tt',
|
| 72 |
+
# time_stamp=datetime(2025, 1, 30, 21, 25, 15, 581994),
|
| 73 |
+
# responses=[
|
| 74 |
+
# Response(config_id='c_p_0_pop_low_easy', rating_v=1, rating_p0=1, rating_p1=1, comment='',
|
| 75 |
+
# timestamp='2025-01-30T21:25:12.642038'),
|
| 76 |
+
# Response(config_id='c_p_1_pop_medium_medium', rating_v=1, rating_p0=1, rating_p1=1, comment='',
|
| 77 |
+
# timestamp='2025-01-30T21:25:13.854227'),
|
| 78 |
+
# Response(config_id='c_p_2_pop_high_hard', rating_v=1, rating_p0=1, rating_p1=1, comment='',
|
| 79 |
+
# timestamp='2025-01-30T21:25:15.581948'),
|
| 80 |
+
# ]
|
| 81 |
+
# )
|
| 82 |
+
# # Create (Ingest)
|
| 83 |
+
# ingest(feedback)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
# Read (Fetch)
|
| 86 |
+
feedback_data = read("cdSf")
|
| 87 |
if feedback_data:
|
| 88 |
print(feedback_data)
|
| 89 |
|
| 90 |
# Update (Modify)
|
| 91 |
+
# updated_feedback = Feedback(
|
| 92 |
+
# id=1,
|
| 93 |
+
# user_id="user123",
|
| 94 |
+
# time_stamp=datetime.now(),
|
| 95 |
+
# responses=[
|
| 96 |
+
# {"q_id": "q1", "ans": 4}, # Updated answer
|
| 97 |
+
# {"q_id": "q2", "ans": 3}
|
| 98 |
+
# ]
|
| 99 |
+
# )
|
| 100 |
+
# update(1, updated_feedback)
|
| 101 |
+
#
|
| 102 |
+
# # Delete (Remove)
|
| 103 |
+
# delete(1)
|
| 104 |
|