vocab-quiz / app.py
PongsakornSET's picture
Update app.py
1bf19e9 verified
import streamlit as st
import random
import pandas as pd
# Load the vocabulary data
df = pd.read_csv("toeic_vocal_1.csv")
vocab_dict = {row['vocab']: row['meaning'] for _, row in df.iterrows()}
# Function to generate a new question
def generate_question():
thai_word, correct_answer = random.choice(list(vocab_dict.items()))
wrong_answers = random.sample([v for v in vocab_dict.values() if v != correct_answer], 4)
options = wrong_answers + [correct_answer]
random.shuffle(options)
return thai_word, correct_answer, options
# Initialize session state
if 'question' not in st.session_state:
st.session_state['question'] = generate_question()
st.session_state['answered'] = False
# Display question
thai_word, correct_answer, options = st.session_state['question']
st.header("ฝึกคำศัพท์ภาษาอังกฤษ")
st.write(f"คำว่า '{thai_word}' คือคำว่าอะไร ?")
# Display answer options as buttons
if st.session_state['answered'] == False:
for option in options:
if st.button(option):
if option == correct_answer:
st.success("ถูกต้อง!")
else:
st.error("ผิด!")
st.info(f"คำตอบที่ถูกต้องคือ: {correct_answer}")
st.session_state['answered'] = True
# Next button for a new question
if st.session_state['answered']:
if st.button("Next"):
st.session_state['question'] = generate_question()
st.session_state['answered'] = False