ChinarQ-AI Uzaiir commited on
Commit
ac1ac12
Β·
verified Β·
1 Parent(s): 8606f08

Update src/streamlit_app.py (#3)

Browse files

- Update src/streamlit_app.py (884ae993c12a727e0fb1e57f999c3adaa1ebe875)


Co-authored-by: Khan <Uzaiir@users.noreply.huggingface.co>

Files changed (1) hide show
  1. 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
- # Welcome to Streamlit!
8
-
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
-
13
- In the meantime, below is an example of what you can do with just a few lines of code:
14
- """
15
-
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
18
-
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
-
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
-
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
-
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
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()