Spaces:
Sleeping
Sleeping
File size: 2,938 Bytes
d224cd9 ac1ac12 d224cd9 ac1ac12 |
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 |
import streamlit as st
from riddles import load_riddles, get_random_riddle
import os
os.environ["STREAMLIT_TELEMETRY_DISABLED"] = "1"
os.environ['STREAMLIT_DISABLE_WATCHDOG_WARNINGS'] = 'true'
# Load custom CSS
def load_css(file_name):
with open(file_name) as f:
css = f.read()
st.markdown(f"<style>{css}</style>", unsafe_allow_html=True)
load_css('src/css/style.css')
# Load riddles from CSV
riddles = load_riddles()
# Check if riddles loaded
if not riddles:
st.write("No riddles found. Please check your CSV file.")
else:
# Initialize session state
if "riddle" not in st.session_state:
st.session_state.riddle = get_random_riddle(riddles)
st.session_state.hint_shown = False
st.session_state.answer_shown = False
st.session_state.user_answer_key = 0
st.header("Let's Riddle!")
st.write(st.session_state.riddle['Riddle'])
# Input for user's answer
user_answer = st.text_input(
"Your answer is:",
placeholder="Type Your Answer Here",
key=f"user_answer_{st.session_state.user_answer_key}"
)
# Create empty containers to dynamically update content
feedback_container = st.empty()
hint_container = st.empty()
answer_container = st.empty()
# Layout: Submit, Hint, Reveal buttons
col1, col2, col3 = st.columns(3)
with col1:
if st.button("Submit"):
if user_answer.strip().lower() == st.session_state.riddle['Answer'].lower():
feedback_container.write("β
Correct! Well done!")
else:
feedback_container.write("β Incorrect. Try asking for a hint!")
with col2:
if st.button("Hint"):
if not st.session_state.hint_shown:
hint_container.write(f"π‘ Hint: {st.session_state.riddle['Hint']}")
st.session_state.hint_shown = True
else:
hint_container.write("Hint already shown.")
with col3:
if st.button("Reveal"):
# Clear the hint when answer is shown
if st.session_state.hint_shown:
hint_container.empty()
if not st.session_state.answer_shown:
answer_container.write(f"π’ The answer is: {st.session_state.riddle['Answer']}")
st.session_state.answer_shown = True
else:
answer_container.write("Answer already revealed.")
# 'Next' button centered
col_center = st.columns([1, 1, 1])[1]
with col_center:
if st.button("Next"):
st.session_state.riddle = get_random_riddle(riddles)
st.session_state.hint_shown = False
st.session_state.answer_shown = False
st.session_state.user_answer_key += 1
# Clear all previous outputs
feedback_container.empty()
hint_container.empty()
answer_container.empty()
st.rerun()
|