study-app / app.py
shikharyashmaurya's picture
Rename study_app.py to app.py
8604a58 verified
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)