Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
#3
by
Uzaiir
- opened
- src/streamlit_app.py +84 -38
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,86 @@
|
|
| 1 |
-
import altair as alt
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from riddles import load_riddles, get_random_riddle
|
| 3 |
+
import os
|
| 4 |
+
os.environ["STREAMLIT_TELEMETRY_DISABLED"] = "1"
|
| 5 |
+
os.environ['STREAMLIT_DISABLE_WATCHDOG_WARNINGS'] = 'true'
|
| 6 |
|
| 7 |
+
# Load custom CSS
|
| 8 |
+
def load_css(file_name):
|
| 9 |
+
with open(file_name) as f:
|
| 10 |
+
css = f.read()
|
| 11 |
+
st.markdown(f"<style>{css}</style>", unsafe_allow_html=True)
|
| 12 |
+
|
| 13 |
+
load_css('src/css/style.css')
|
| 14 |
+
|
| 15 |
+
# Load riddles from CSV
|
| 16 |
+
riddles = load_riddles()
|
| 17 |
+
|
| 18 |
+
# Check if riddles loaded
|
| 19 |
+
if not riddles:
|
| 20 |
+
st.write("No riddles found. Please check your CSV file.")
|
| 21 |
+
else:
|
| 22 |
+
# Initialize session state
|
| 23 |
+
if "riddle" not in st.session_state:
|
| 24 |
+
st.session_state.riddle = get_random_riddle(riddles)
|
| 25 |
+
st.session_state.hint_shown = False
|
| 26 |
+
st.session_state.answer_shown = False
|
| 27 |
+
st.session_state.user_answer_key = 0
|
| 28 |
+
|
| 29 |
+
st.header("Let's Riddle!")
|
| 30 |
+
st.write(st.session_state.riddle['Riddle'])
|
| 31 |
+
|
| 32 |
+
# Input for user's answer
|
| 33 |
+
user_answer = st.text_input(
|
| 34 |
+
"Your answer is:",
|
| 35 |
+
placeholder="Type Your Answer Here",
|
| 36 |
+
key=f"user_answer_{st.session_state.user_answer_key}"
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
# Create empty containers to dynamically update content
|
| 40 |
+
feedback_container = st.empty()
|
| 41 |
+
hint_container = st.empty()
|
| 42 |
+
answer_container = st.empty()
|
| 43 |
+
|
| 44 |
+
# Layout: Submit, Hint, Reveal buttons
|
| 45 |
+
col1, col2, col3 = st.columns(3)
|
| 46 |
+
with col1:
|
| 47 |
+
if st.button("Submit"):
|
| 48 |
+
if user_answer.strip().lower() == st.session_state.riddle['Answer'].lower():
|
| 49 |
+
feedback_container.write("β
Correct! Well done!")
|
| 50 |
+
else:
|
| 51 |
+
feedback_container.write("β Incorrect. Try asking for a hint!")
|
| 52 |
+
|
| 53 |
+
with col2:
|
| 54 |
+
if st.button("Hint"):
|
| 55 |
+
if not st.session_state.hint_shown:
|
| 56 |
+
hint_container.write(f"π‘ Hint: {st.session_state.riddle['Hint']}")
|
| 57 |
+
st.session_state.hint_shown = True
|
| 58 |
+
else:
|
| 59 |
+
hint_container.write("Hint already shown.")
|
| 60 |
+
|
| 61 |
+
with col3:
|
| 62 |
+
if st.button("Reveal"):
|
| 63 |
+
# Clear the hint when answer is shown
|
| 64 |
+
if st.session_state.hint_shown:
|
| 65 |
+
hint_container.empty()
|
| 66 |
+
if not st.session_state.answer_shown:
|
| 67 |
+
answer_container.write(f"π’ The answer is: {st.session_state.riddle['Answer']}")
|
| 68 |
+
st.session_state.answer_shown = True
|
| 69 |
+
else:
|
| 70 |
+
answer_container.write("Answer already revealed.")
|
| 71 |
+
|
| 72 |
+
# 'Next' button centered
|
| 73 |
+
col_center = st.columns([1, 1, 1])[1]
|
| 74 |
+
with col_center:
|
| 75 |
+
if st.button("Next"):
|
| 76 |
+
st.session_state.riddle = get_random_riddle(riddles)
|
| 77 |
+
st.session_state.hint_shown = False
|
| 78 |
+
st.session_state.answer_shown = False
|
| 79 |
+
st.session_state.user_answer_key += 1
|
| 80 |
+
|
| 81 |
+
# Clear all previous outputs
|
| 82 |
+
feedback_container.empty()
|
| 83 |
+
hint_container.empty()
|
| 84 |
+
answer_container.empty()
|
| 85 |
+
|
| 86 |
+
st.rerun()
|