Spaces:
Running
Running
File size: 2,219 Bytes
0759822 |
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 |
import streamlit as st
from db.crud import read, reset_feedback_in_db
from views.questions_screen import questions_screen
def continue_survey_screen(data):
"""Screen for existing users to continue or restart their survey."""
st.title("Resume Your Survey")
st.write("Welcome back! Would you like to continue from where you left off or restart the survey?")
# Fetch user progress from Firebase
saved_state = read(st.session_state.username)
col1, col2 = st.columns(2)
with col1:
if st.button("Continue"):
if saved_state:
# Set session state values based on saved progress
st.session_state.current_index = saved_state.get("current_index", 0)
st.session_state.responses = saved_state.get("responses", [])
# Find the last answered config_id in responses
last_config_id = None
if st.session_state.responses:
last_config_id = st.session_state.responses[-1].get("config_id")
# Set the index to the matching config_id
if last_config_id is not None:
matching_index = data[data["config_id"] == last_config_id].index
if not matching_index.empty:
st.session_state.current_index = matching_index[0]+1
st.success("Resuming your survey!")
# Set survey_continued flag to True only when there's saved progress
st.session_state.survey_continued = True
print(st.session_state.current_index)
st.rerun()
# questions_screen(data)
else:
st.warning("No previous progress found. Starting a new survey.")
st.session_state.current_index = 0
st.session_state.responses = []
with col2:
if st.button("Restart"):
st.session_state.current_index = 0
st.session_state.responses = []
st.success("Survey restarted!")
# Reset Firebase data for the user
reset_feedback_in_db(st.session_state.username)
st.rerun()
|