File size: 4,277 Bytes
d7f315f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import json
import os
import random

# Define the JSON file path for storing data
DATA_FILE = 'study_data.json'

# Initialize the JSON file if it doesn't exist
if not os.path.exists(DATA_FILE):
    with open(DATA_FILE, 'w') as f:
        json.dump({}, f)

# Load the data from the JSON file
with open(DATA_FILE, 'r') as f:
    data = json.load(f)

# Mode selection
mode = st.radio("Select Mode", ["Read", "Write"])

# Write Mode
if mode == "Write":
    # Select existing dictionary or create a new one
    dict_options = list(data.keys()) + ["Create new"]
    dict_choice = st.selectbox("Select dictionary or create new", dict_options)

    if dict_choice == "Create new":
        # Input for new dictionary name
        new_dict_name = st.text_input("Enter new dictionary name")
        if st.button("Create dictionary"):
            if new_dict_name and new_dict_name not in data:
                data[new_dict_name] = {}
                with open(DATA_FILE, 'w') as f:
                    json.dump(data, f)
                st.success(f"Dictionary '{new_dict_name}' created!")
            else:
                st.error("Please enter a valid name that doesn't already exist.")
    else:
        # Add question-answer pair to selected dictionary
        selected_dict = dict_choice
        question = st.text_area("Enter question", height=400)
        answer = st.text_area("Enter answer", height=400)
        if st.button("Add to dictionary"):
            if question and answer:
                if question in data[selected_dict]:
                    st.warning("Question already exists. Overwriting the answer.")
                data[selected_dict][question] = answer
                with open(DATA_FILE, 'w') as f:
                    json.dump(data, f)
                st.success("Question and answer added/updated successfully!")
            else:
                st.error("Both question and answer are required.")

# Read Mode
elif mode == "Read":
    if not data:
        st.write("No dictionaries available.")
    else:
        # Select a dictionary to read
        selected_dict = st.selectbox("Select dictionary", list(data.keys()))
        if selected_dict:
            reading_mode = st.radio("Select reading mode", ["Sequential", "Random"])
            questions = list(data[selected_dict].keys())

            if not questions:
                st.write("No questions in this dictionary.")
            else:
                if reading_mode == "Sequential":
                    # Initialize or maintain the sequential index in session state
                    if 'seq_index' not in st.session_state:
                        st.session_state.seq_index = 0

                    # Display current question and answer
                    current_question = questions[st.session_state.seq_index]
                    current_answer = data[selected_dict][current_question]
                    st.text_area("Question", current_question, height=400)
                    st.text_area("Answer", current_answer, height=400)

                    # Navigation buttons
                    col1, col2 = st.columns(2)
                    with col1:
                        if st.button("Previous"):
                            st.session_state.seq_index = (st.session_state.seq_index - 1) % len(questions)
                    with col2:
                        if st.button("Next"):
                            st.session_state.seq_index = (st.session_state.seq_index + 1) % len(questions)

                elif reading_mode == "Random":
                    # Initialize or update random question in session state
                    if 'random_question' not in st.session_state or st.button("Next random question"):
                        st.session_state.random_question = random.choice(questions)

                    # Display current random question and answer
                    current_question = st.session_state.random_question
                    current_answer = data[selected_dict][current_question]
                    st.text_area("Question", current_question, height=400)
                    st.text_area("Answer", current_answer, height=400)