74 lines
2.2 KiB
Python
74 lines
2.2 KiB
Python
import json
|
|
import random
|
|
from pathlib import Path
|
|
|
|
import streamlit as st
|
|
from streamlit import session_state as ss
|
|
|
|
# Initialize session state
|
|
if "current_question" not in ss:
|
|
ss.current_question = None
|
|
|
|
|
|
# Function to select a random question
|
|
def select_question():
|
|
with open("donnees/questions.json") as file:
|
|
questions = json.load(file)["questions"]
|
|
random.shuffle(questions)
|
|
ss.current_question = questions.pop()
|
|
# Shuffle the answers
|
|
answers = [
|
|
ss.current_question["answer1"],
|
|
ss.current_question["answer2"],
|
|
ss.current_question["answer3"],
|
|
ss.current_question["answer4"]
|
|
]
|
|
random.shuffle(answers)
|
|
ss.current_answers = answers
|
|
|
|
|
|
# Function to check the selected answer
|
|
def check_answer():
|
|
correct_answer = ss.current_question["answer1"]
|
|
selected_answer = ss.selected_answer
|
|
|
|
if selected_answer == correct_answer:
|
|
st.success("Bonne réponse!")
|
|
else:
|
|
st.error(f"Mauvaise réponse! La bonne réponse est: {correct_answer}")
|
|
st.subheader("Explication")
|
|
st.write(ss.current_question["explanation"])
|
|
ss.clear()
|
|
|
|
|
|
# Main app
|
|
st.image("images/banniere.jpeg")
|
|
st.title("Culture Créative - Version Bêta !")
|
|
|
|
# Haut de page
|
|
header_content = Path("donnees/header.md").read_text()
|
|
st.markdown(header_content, unsafe_allow_html=True)
|
|
|
|
# Check if a question is already selected
|
|
if not ss.current_question:
|
|
select_question()
|
|
|
|
# Form for answerr
|
|
with st.form("answer_form"):
|
|
# Display the current question
|
|
st.subheader("Question")
|
|
st.write(ss.current_question["question"])
|
|
# Display the answer options
|
|
st.subheader("Réponses possibles")
|
|
ss.selected_answer = st.radio(f"Choisis une réponse", options=["---"] + ss.current_answers, key=0)
|
|
# Display the correct answer
|
|
if st.form_submit_button(label="Vérifier") and ss.selected_answer != "---":
|
|
check_answer()
|
|
# Reset button to load a new question
|
|
if st.button("Question suivante"):
|
|
ss.selected_answer = "---"
|
|
select_question()
|
|
|
|
# Pied de page
|
|
footer_content = Path("donnees/footer.md").read_text()
|
|
st.markdown(footer_content, unsafe_allow_html=True)
|