-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstreamlit_app.py
More file actions
394 lines (315 loc) · 9.97 KB
/
streamlit_app.py
File metadata and controls
394 lines (315 loc) · 9.97 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
# import numpy as np
# import altair as alt
# import pandas as pd
# import streamlit as st
# from datetime import time, datetime
# st.markdown("*Streamlit* is **really** ***cool***.")
# st.header('st.write')
# code = '''def hello():
# print("Hello, Streamlit!")'''
# st.code(code, language="python")
# # Example 1
# st.write('Hello, *World!* :sunglasses:')
# # Example 2
# st.write(1234)
# # Example 3
# df = pd.DataFrame({
# 'first column': [1, 2, 3, 4],
# 'second column': [10, 20, 30, 40]
# })
# st.write(df)
# # Example 4
# st.write('Below is a DataFrame:', df, 'Above is a dataframe.')
# # Example 5
# df2 = pd.DataFrame(
# np.random.randn(200, 3),
# columns=['a', 'b', 'c'])
# c = alt.Chart(df2).mark_circle().encode(
# x='a', y='b', size='c', color='c', tooltip=['a', 'b', 'c'])
# st.write(c)
# # Example 1
# st.subheader('Slider')
# age = st.slider('How old are you?', 0, 130, 25)
# st.write("I'm ", age, 'years old')
# # Example 2
# st.subheader('Range slider')
# values = st.slider(
# 'Select a range of values',
# 0.0, 100.0, (25.0, 75.0))
# st.write('Values:', values)
# # Example 3
# st.subheader('Range time slider')
# appointment = st.slider(
# "Schedule your appointment:",
# value=(time(11, 30), time(12, 45)))
# st.write("You're scheduled for:", appointment)
# # Example 4
# st.subheader('Datetime slider')
# start_time = st.slider(
# "When do you start?",
# value=datetime(2020, 1, 1, 9, 30),
# format="MM/DD/YY - hh:mm")
# st.write("Start time:", start_time)
# st.header('st.selectbox')
# option = st.selectbox(
# 'What is your favorite color?',
# ('Blue', 'Red', 'Green'))
# st.write('Your favorite color is ', option)
# st.header('st.multiselect')
# options = st.multiselect(
# 'What are your favorite colors',
# ['Green', 'Yellow', 'Red', 'Blue'],
# ['Yellow', 'Red'])
# st.write('You selected:', options)
# st.header('st.checkbox')
# st.write ('What would you like to order?')
# icecream = st.checkbox('Ice cream')
# coffee = st.checkbox('Coffee')
# cola = st.checkbox('Cola')
# if icecream:
# st.write("Great! Here's some more 🍦")
# if coffee:
# st.write("Okay, here's some coffee ☕")
# if cola:
# st.write("Here you go 🥤")
# st.header('st.latex')
# st.latex(r'''
# a + ar + a r^2 + a r^3 + \cdots + a r^{n-1} =
# \sum_{k=0}^{n-1} ar^k =
# a \left(\frac{1-r^{n}}{1-r}\right)
# ''')
# st.title('Customizing the theme of Streamlit apps')
# st.write('Contents of the `.streamlit/config.toml` file of this app')
# st.code("""
# [theme]
# primaryColor="#F39C12"
# backgroundColor="#2E86C1"
# secondaryBackgroundColor="#AED6F1"
# textColor="#FFFFFF"
# font="monospace"
# """)
# number = st.sidebar.slider('Select a number:', 0, 10, 5)
# st.write('Selected number from slider widget is:', number)
# # st.set_page_config(layout="wide")
# st.title('How to layout your Streamlit app')
# with st.expander('About this app'):
# st.write('This app shows the various ways on how you can layout your Streamlit app.')
# st.image('https://streamlit.io/images/brand/streamlit-logo-secondary-colormark-darktext.png', width=250)
# st.sidebar.header('Input')
# user_name = st.sidebar.text_input('What is your name?')
# user_emoji = st.sidebar.selectbox('Choose an emoji', ['', '😄', '😆', '😊', '😍', '😴', '😕', '😱'])
# user_food = st.sidebar.selectbox('What is your favorite food?', ['', 'Tom Yum Kung', 'Burrito', 'Lasagna', 'Hamburger', 'Pizza'])
# st.header('Output')
# col1, col2, col3 = st.columns(3)
# with col1:
# if user_name != '':
# st.write(f'👋 Hello {user_name}!')
# else:
# st.write('👈 Please enter your **name**!')
# with col2:
# if user_emoji != '':
# st.write(f'{user_emoji} is your favorite **emoji**!')
# else:
# st.write('👈 Please choose an **emoji**!')
# with col3:
# if user_food != '':
# st.write(f'🍴 **{user_food}** is your favorite **food**!')
# else:
# st.write('👈 Please choose your favorite **food**!')
# st.title('st.form')
# # Full example of using the with notation
# st.header('1. Example of using `with` notation')
# st.subheader('Coffee machine')
# with st.form('my_form'):
# st.subheader('**Order your coffee**')
# # Input widgets
# coffee_bean_val = st.selectbox('Coffee bean', ['Arabica', 'Robusta'])
# coffee_roast_val = st.selectbox('Coffee roast', ['Light', 'Medium', 'Dark'])
# brewing_val = st.selectbox('Brewing method', ['Aeropress', 'Drip', 'French press', 'Moka pot', 'Siphon'])
# serving_type_val = st.selectbox('Serving format', ['Hot', 'Iced', 'Frappe'])
# milk_val = st.select_slider('Milk intensity', ['None', 'Low', 'Medium', 'High'])
# owncup_val = st.checkbox('Bring own cup')
# # Every form must have a submit button
# submitted = st.form_submit_button('Submit')
# if submitted:
# st.markdown(f'''
# ☕ You have ordered:
# - Coffee bean: `{coffee_bean_val}`
# - Coffee roast: `{coffee_roast_val}`
# - Brewing: `{brewing_val}`
# - Serving type: `{serving_type_val}`
# - Milk: `{milk_val}`
# - Bring own cup: `{owncup_val}`
# ''')
# else:
# st.write('☝️ Place your order!')
# # Short example of using an object notation
# st.header('2. Example of object notation')
# form = st.form('my_form_2')
# selected_val = form.slider('Select a value')
# form.form_submit_button('Submit')
# st.write('Selected value: ', selected_val)
import streamlit as st
import random
# ---------------- ASCII ART for each hangman stage ----------------
# Index == attempts_left
hangman_stages = [
"""
_______
|/ |
| ( )
| /|\\
| |
| / \\
|
_|___
""",
"""
_______
|/ |
| ( )
| /|\\
| |
| /
|
_|___
""",
"""
_______
|/ |
| ( )
| /|\\
| |
|
|
_|___
""",
"""
_______
|/ |
| ( )
| /|
| |
|
|
_|___
""",
"""
_______
|/ |
| ( )
| |
| |
|
|
_|___
""",
"""
_______
|/ |
| ( )
|
|
|
|
_|___
""",
"""
_______
|/ |
|
|
|
|
|
_|___
"""
]
# ---------------- WORD LIST ----------------
WORDS = [
"streamlit", "python", "hangman", "visualize", "session",
"development", "code", "function", "virtual", "environment"
]
# ---------------- INITIALIZE / RESET GAME ----------------
def initialize_game_state():
"""
Initializes (or resets) the game state in session_state.
"""
st.session_state.secret_word = random.choice(WORDS).upper()
st.session_state.guessed_letters = set()
st.session_state.attempts_left = 6
st.session_state.game_over = False
# ---------------- DISPLAY HANGMAN ----------------
def display_hangman():
"""
Displays the ASCII art for the current number of attempts left.
"""
index = st.session_state.attempts_left
# Show as a code block for clearer formatting
st.markdown(f"```{hangman_stages[index]}```")
# ---------------- DISPLAY CURRENT PROGRESS ----------------
def display_current_progress():
"""
Displays the current progress of the guessed word (underscores for unguessed letters).
"""
display_word = ""
for char in st.session_state.secret_word:
if char in st.session_state.guessed_letters:
display_word += char + " "
else:
display_word += "_ "
st.write("**Word:**", display_word.strip())
# ---------------- CHECK GAME STATUS ----------------
def check_game_status():
"""
Checks if the user has guessed the word or if attempts have run out.
"""
# If user guessed all letters
if all(letter in st.session_state.guessed_letters for letter in st.session_state.secret_word):
st.session_state.game_over = True
st.success("Congratulations! You guessed the word!")
# If no attempts left
if st.session_state.attempts_left <= 0:
st.session_state.game_over = True
st.error(f"Game Over! The word was: {st.session_state.secret_word}")
# ---------------- PROCESS A SINGLE GUESS ----------------
def guess_letter(letter):
"""
Processes a single letter guess:
- Deducts an attempt if the guess is wrong.
- Adds the guessed letter to the guessed_letters set.
- Checks the game status afterward.
"""
letter = letter.upper()
if letter in st.session_state.guessed_letters:
st.warning(f"You already guessed '{letter}'. Try a different letter.")
return
st.session_state.guessed_letters.add(letter)
if letter not in st.session_state.secret_word:
st.session_state.attempts_left -= 1
st.warning(f"'{letter}' is NOT in the word. Attempts left: {st.session_state.attempts_left}")
else:
st.success(f"Good guess! '{letter}' is in the word. You will see it with the next guess")
check_game_status()
# ---------------- STREAMLIT MAIN APP ----------------
def main():
st.title("Hangman in Streamlit")
# If game not started, or the user clicks 'Reset Game', initialize
if "secret_word" not in st.session_state or st.button("Reset Game"):
initialize_game_state()
# Show hangman ASCII and current word state
display_hangman()
display_current_progress()
# If the game is not yet over, show a form to guess a letter
if not st.session_state.game_over:
# Use a form so we don't manually overwrite session_state
with st.form("guess_form"):
letter_input = st.text_input("Guess a letter", max_chars=1)
guess_button = st.form_submit_button("Guess")
# When the user submits the form
if guess_button and letter_input:
guess_letter(letter_input)
else:
st.info("Click 'Reset Game' to start a new game.")
if __name__ == "__main__":
main()