Spaces:
Sleeping
Sleeping
import streamlit as st | |
import json | |
from quiz_gui_helper import _QuizGUI | |
# Initialize session state | |
if "page_access" not in st.session_state: | |
st.session_state.page_access = {"About": True, "Load Exam": True, "Take Exam": False, "View Results": False} | |
def load_quiz_from_file(): | |
uploaded_file = st.file_uploader("Upload a JSON quiz file", type="json") | |
if uploaded_file is not None: | |
try: | |
# Load the JSON content | |
quiz = json.load(uploaded_file) | |
# Extract metadata and content | |
metadata = quiz.get("metadata", {}) | |
quiz_data = quiz.get("content", []) | |
# Save the metadata and content to session state | |
st.session_state.metadata = metadata | |
st.session_state.quiz_data = quiz_data | |
# Return the extracted data | |
return metadata, quiz_data | |
except json.JSONDecodeError: | |
st.error("Invalid JSON file. Please upload a properly formatted JSON file.") | |
return None, None | |
def main(): | |
st.title("Load Exam") | |
metadata, quiz_data = load_quiz_from_file() | |
if metadata and quiz_data: | |
quiz_app = _QuizGUI(quiz_data) | |
quiz_app.load_quiz(metadata) | |
# update the session state | |
st.session_state.quiz_loaded = True | |
st.session_state.page_access["Take Exam"] = True | |
st.session_state.page_access["Load Exam"] = False | |
st.success("Exam loaded successfully!") | |
st.write("You can now proceed to the 'Take Exam' page to start the quiz.") | |
else: | |
st.info("Please upload a quiz JSON file to begin.") | |
if __name__ == "__main__": | |
main() |