File size: 1,651 Bytes
9c3ca76
00c87b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9c3ca76
 
 
00c87b7
 
9c3ca76
00c87b7
 
 
 
 
 
 
9c3ca76
 
 
 
00c87b7
 
 
 
 
 
9c3ca76
 
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
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()