-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
360 lines (300 loc) · 11.1 KB
/
main.py
File metadata and controls
360 lines (300 loc) · 11.1 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
import os
import yaml
import streamlit as st
from pathlib import Path
from plots import create_radar_chart, create_bar_chart
def load_yaml_file(filepath):
with open(filepath, "r", encoding="utf-8") as file:
return yaml.safe_load(file)
# Configurazione pagina
st.set_page_config(
page_title="D&P Test",
page_icon="🐍",
layout="centered",
initial_sidebar_state="collapsed"
)
hide_streamlit_style = """
<style>
/* Nascondi menu principale */
#MainMenu {visibility: hidden !important;}
/* Nascondi footer */
footer {visibility: hidden !important;}
/* Nascondi header e altri elementi di Streamlit */
header {visibility: hidden !important;}
/* Nascondi il pulsante deploy */
.stDeployButton {display: none !important;}
/* Nascondi la barra degli strumenti */
.stToolbar {display: none !important;}
/* Nascondi decorazioni Streamlit */
.stDecoration {display: none !important;}
/* Nascondi elementi di Streamlit Community Cloud */
.viewerBadge_container__1QSob {display: none !important;}
.viewerBadge_link__1S137 {display: none !important;}
.stApp a:first-child {display: none !important;}
.styles_viewerBadge__CvC9N {display: none !important;}
/* Nascondi altri possibili elementi */
div[data-testid="stDecoration"] {display: none !important;}
div[data-testid="stToolbar"] {display: none !important;}
div[data-testid="stStatusWidget"] {display: none !important;}
</style>
"""
st.markdown(hide_streamlit_style, unsafe_allow_html=True)
# Stili CSS personalizzati
st.markdown("""
<style>
body {
font-size: 14px;
}
.main-title {
font-size: 2.6rem !important;
font-weight: bold;
text-align: center;
margin-bottom: 1.3rem;
color: #306998;
}
.class-title {
font-size: 2rem !important;
font-weight: bold;
color: #306998;
margin-top: 1.3rem;
}
.stats-box {
background-color: #f0f2f6;
padding: 1.3rem;
border-radius: 0.7rem;
margin: 1.3rem 0;
}
.special-move {
padding: 0.7rem;
background-color: #ffd43b;
margin: 0.7rem 0;
border-radius: 0.4rem;
font-weight: bold;
font-size: 1.1rem;
}
.description-text {
font-size: 1.2rem !important;
line-height: 1.6;
margin: 1.3rem 0;
}
.python-zen {
font-style: italic;
text-align: center;
padding: 1.3rem;
background-color: #306998;
color: white;
border-radius: 0.6rem;
margin: 1.3rem 0;
font-size: 1.1rem;
}
.stButton button {
font-size: 1.1rem;
padding: 0.6rem 1.2rem;
font-weight: bold;
}
h1, h2, h3 {
font-size: 1.6rem !important;
}
.stRadio label {
font-size: 1.2rem !important;
}
</style>
""", unsafe_allow_html=True)
class PythonDevTest:
def __init__(self):
self.scores = {
"barbarian": 0,
"rogue": 0,
"wizard": 0,
"cleric": 0,
"druid": 0,
"monk": 0
}
self.questions = load_yaml_file("copy/questions.yaml")
# Dictionary comprehension con pathlib
character_dir = Path("copy/character")
self.results = {path.stem: load_yaml_file(path) for path in character_dir.glob("*.yaml")}
def reset_scores(self):
"""Reimposta tutti i punteggi a zero"""
for class_name in self.scores:
self.scores[class_name] = 0
def intro_page():
"""Mostra la pagina introduttiva del test"""
st.markdown('<div class="main-title">🐍 Dungeons & Pythons 🐍</div>', unsafe_allow_html=True)
st.markdown("""
<div class="description-text">
Welcome, <b>adventurer</b> of Python code!
This test will analyze your skills and preferences to determine
which class of Python developer best matches your style.
Are you a <b>Barbarian</b> who solves problems with brute force?<br>
A <b>Rogue</b> who “borrows” code from others?<br>
A <b>Wizard</b> who masters the arcane arts of metaclasses?<br>
A <b>Cleric</b> devoted to testing and best practices?<br>
A <b>Druid</b> in harmony with hardware and data?<br>
Or a <b>Monk</b> who has attained the enlightenment of simplicity?
</div>
""", unsafe_allow_html=True)
col1, col2, col3 = st.columns([1, 2, 1])
with col2:
if st.button("Start the Test", use_container_width=True):
st.session_state.page = "questions"
st.session_state.question_index = 0
st.rerun()
def question_page(test):
"""Mostra la pagina con le domande del test"""
question_index = st.session_state.question_index
total_questions = len(test.questions)
# Mostra la barra di progresso
progress = question_index / total_questions
st.progress(progress)
st.markdown(f"<h3 style='text-align: center;'>Question {question_index + 1} of {total_questions}</h3>",
unsafe_allow_html=True)
# Mostra la domanda corrente
question = test.questions[question_index]
st.markdown(f"### {question['text']}", unsafe_allow_html=True)
# Crea una lista di opzioni per il radio button
options = [opt[0] for opt in question['options']]
# Mostra le opzioni come radio button con stile personalizzato
st.markdown("""
<style>
div.row-widget.stRadio > div {
flex-direction: column;
gap: 8px;
}
div.row-widget.stRadio > div[role="radiogroup"] > label {
padding: 12px;
background-color: #f0f2f6;
border-radius: 6px;
margin-bottom: 8px;
font-size: 16px;
box-shadow: 0 1px 2px rgba(0,0,0,0.1);
transition: all 0.2s;
}
div.row-widget.stRadio > div[role="radiogroup"] > label:hover {
background-color: #e6e9ef;
transform: translateY(-1px);
box-shadow: 0 2px 4px rgba(0,0,0,0.15);
}
div.row-widget.stRadio > div[role="radiogroup"] > label > div:first-child {
transform: scale(1.2);
}
</style>
""", unsafe_allow_html=True)
choice = st.radio("Choose the option that best represents you:", options, index=None, key=f"q_{question_index}")
col1, col2, col3 = st.columns([1, 2, 1])
with col2:
# Mostra pulsante per continuare solo se è stata selezionata un'opzione
if choice is not None:
choice_index = options.index(choice)
selected_class = question['options'][choice_index][1]
if st.button("Continue", use_container_width=True, key="continua_btn", help="Next question"):
# Aggiorna il punteggio
test.scores[selected_class] += 1
# Passa alla prossima domanda o ai risultati
if question_index < total_questions - 1:
st.session_state.question_index += 1
else:
st.session_state.page = "results"
st.rerun()
def results_page(test):
"""Mostra la pagina dei risultati del test"""
# Determina la classe con il punteggio più alto
# Inizializza variabili
max_score = 0
result_class = None
max_count = 0
classes_with_max = []
# Trova il punteggio massimo
for class_name, score in test.scores.items():
if score > max_score:
max_score = score
# Conta quante classi hanno questo punteggio massimo
for class_name, score in test.scores.items():
if score == max_score:
classes_with_max.append(class_name)
# Determina il risultato in base al conteggio
if len(classes_with_max) > 1:
# Se due classi hanno lo stesso punteggio, scegli casualmente
result_class = "master"
else:
# Se una sola classe ha il punteggio massimo
result_class = classes_with_max[0]
result = test.results[result_class]
# Mostra l'intestazione con il risultato
st.markdown(f'<div class="main-title">Your result</div>', unsafe_allow_html=True)
st.markdown(f'<div class="class-title">{result["title"]}</div>', unsafe_allow_html=True)
# Layout per immagine e descrizione
col1, col2 = st.columns([1, 2])
with col1:
# Mostra l'immagine del personaggio
try:
# Ottieni il percorso assoluto della directory corrente
current_dir = Path(__file__).parent.absolute()
image_path = os.path.join(current_dir, result["image"])
# Carica l'immagine se esiste
if os.path.exists(image_path):
st.markdown("<div style='padding-top: 20px;'></div>", unsafe_allow_html=True)
st.image(image_path, use_container_width=True)
else:
# Gestisci silenziosamente il caso in cui l'immagine non esiste
st.markdown("⚠️ *Character image not available*")
except Exception:
# Ignora l'errore e continua senza l'immagine
pass
with col2:
# Mostra la descrizione
st.markdown(f'<div class="description-text">{result["description"]}</div>', unsafe_allow_html=True)
col3, col4 = st.columns([2, 3])
if result_class != "master":
with col3:
# Mostra le statistiche come grafico radar
st.subheader("Statistics")
radar_chart = create_radar_chart(result["stats"], result["color"])
st.pyplot(radar_chart)
with col4:
# Mostra il grafico a barre con tutti i punteggi
bar_chart = create_bar_chart(test.scores, result["color"])
st.altair_chart(bar_chart, use_container_width=True)
# Mostra un messaggio di saggezza personalizzato in base alla classe
st.markdown("""
<style>
.python-zen {
background-color: #f8f8f8;
border-left: 6px solid #4B8BBE;
padding: 1em;
font-family: 'Courier New', monospace;
font-size: 15px;
white-space: pre-line;
line-height: 1.6;
border-radius: 5px;
color: #333;
}
</style>
""", unsafe_allow_html=True)
st.markdown(f"""<div class="python-zen">{result["message"]}</div>""", unsafe_allow_html=True)
col1, col2, col3 = st.columns([1, 2, 1])
with col2:
if st.button("Retry", use_container_width=True):
# Resetta i punteggi
test.reset_scores()
st.session_state.page = "intro"
st.rerun()
# Aggiungi un easter egg in base alla classe risultante
if "easter_egg" in result.keys():
if st.button(result["easter_egg"]["name_button"]):
st.code(result["easter_egg"]["text"], language="python")
def main():
# Inizializza la sessione
if 'page' not in st.session_state:
st.session_state.page = "intro"
if 'test' not in st.session_state:
st.session_state.test = PythonDevTest()
# Gestisci le pagine
if st.session_state.page == "intro":
intro_page()
elif st.session_state.page == "questions":
question_page(st.session_state.test)
elif st.session_state.page == "results":
results_page(st.session_state.test)
if __name__ == "__main__":
main()