2023-06-18 16:10:02 +00:00
|
|
|
import json
|
2023-08-07 22:57:31 +00:00
|
|
|
import logging
|
2023-06-18 16:10:02 +00:00
|
|
|
import random
|
2023-06-18 17:58:59 +00:00
|
|
|
from pathlib import Path
|
|
|
|
|
2023-06-18 16:10:02 +00:00
|
|
|
import streamlit as st
|
|
|
|
from streamlit import session_state as ss
|
|
|
|
|
|
|
|
# Initialize session state
|
2023-08-07 22:57:31 +00:00
|
|
|
|
2023-06-18 16:10:02 +00:00
|
|
|
if "current_question" not in ss:
|
|
|
|
ss.current_question = None
|
|
|
|
|
2023-08-07 22:57:31 +00:00
|
|
|
if "questions_lues" not in ss:
|
|
|
|
ss.questions_lues = False
|
|
|
|
|
|
|
|
with open("donnees/licences.json") as file:
|
|
|
|
licences_data = json.load(file)["licences"]
|
|
|
|
licences_dict = {}
|
|
|
|
for licence_data in licences_data:
|
|
|
|
licences_dict[licence_data["licenceId"]] = licence_data
|
|
|
|
licences_dict = licences_dict
|
|
|
|
|
2023-06-18 16:10:02 +00:00
|
|
|
|
|
|
|
# Function to select a random question
|
|
|
|
def select_question():
|
2023-08-07 22:57:31 +00:00
|
|
|
"""
|
|
|
|
This function selects a random question from the list of questions and shuffles the answers for each question.
|
|
|
|
"""
|
|
|
|
if not ss.questions_lues:
|
|
|
|
with open("donnees/questions.json") as file:
|
|
|
|
ss.questions = json.load(file)["questions"]
|
|
|
|
random.shuffle(ss.questions)
|
|
|
|
ss.questions_lues = True
|
|
|
|
logging.info("Appel de la fonction select_question()")
|
|
|
|
ss.current_question = ss.questions.pop()
|
|
|
|
# Shuffle the answers
|
|
|
|
answers = ss.current_question["answers"].copy()
|
|
|
|
random.shuffle(answers)
|
|
|
|
ss.current_answers = answers
|
2023-06-18 16:10:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Function to check the selected answer
|
|
|
|
def check_answer():
|
2023-08-07 22:57:31 +00:00
|
|
|
"""
|
|
|
|
This function checks the selected answer.
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
logging.info("Appel de la fonction check_answer()")
|
|
|
|
correct_answer = ss.current_question["answers"][0]
|
2023-06-18 16:10:02 +00:00
|
|
|
selected_answer = ss.selected_answer
|
|
|
|
|
2023-08-07 22:57:31 +00:00
|
|
|
licence = ss.current_question["licence"]
|
|
|
|
nom = licences_dict[licence]["name"]
|
|
|
|
url = licences_dict[licence]["url"]
|
|
|
|
logo = licences_dict[licence]["logo"]
|
|
|
|
|
2023-06-18 16:10:02 +00:00
|
|
|
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"])
|
2023-08-07 22:57:31 +00:00
|
|
|
st.markdown(f"Cette licence se nomme [{nom}]({url})")
|
|
|
|
st.image(f"logos/{logo}")
|
2023-06-18 17:38:05 +00:00
|
|
|
ss.clear()
|
2023-06-18 16:10:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Main app
|
2023-06-18 17:38:05 +00:00
|
|
|
st.image("images/banniere.jpeg")
|
|
|
|
st.title("Culture Créative - Version Bêta !")
|
2023-06-18 17:58:59 +00:00
|
|
|
|
|
|
|
# Haut de page
|
|
|
|
header_content = Path("donnees/header.md").read_text()
|
|
|
|
st.markdown(header_content, unsafe_allow_html=True)
|
2023-06-18 16:10:02 +00:00
|
|
|
|
|
|
|
# Check if a question is already selected
|
|
|
|
if not ss.current_question:
|
|
|
|
select_question()
|
|
|
|
|
2023-08-07 22:57:31 +00:00
|
|
|
# Form for answer
|
2023-06-18 16:10:02 +00:00
|
|
|
with st.form("answer_form"):
|
2023-08-07 22:57:31 +00:00
|
|
|
logging.info("Afficher la question")
|
2023-06-18 16:10:02 +00:00
|
|
|
# Display the current question
|
|
|
|
st.subheader("Question")
|
|
|
|
st.write(ss.current_question["question"])
|
|
|
|
# Display the answer options
|
2023-08-07 22:57:31 +00:00
|
|
|
logging.info("Afficher les réponses possibles")
|
2023-06-18 16:10:02 +00:00
|
|
|
st.subheader("Réponses possibles")
|
2023-06-18 17:38:05 +00:00
|
|
|
ss.selected_answer = st.radio(f"Choisis une réponse", options=["---"] + ss.current_answers, key=0)
|
2023-06-18 16:10:02 +00:00
|
|
|
# Display the correct answer
|
2023-06-18 17:38:05 +00:00
|
|
|
if st.form_submit_button(label="Vérifier") and ss.selected_answer != "---":
|
2023-08-07 22:57:31 +00:00
|
|
|
logging.info("Vérifier la réponse")
|
2023-06-18 16:10:02 +00:00
|
|
|
check_answer()
|
2023-06-18 20:06:01 +00:00
|
|
|
if st.form_submit_button(label="Prochaine question"):
|
2023-08-07 22:57:31 +00:00
|
|
|
logging.info("Prochaine question")
|
2023-06-18 20:06:01 +00:00
|
|
|
ss.clear()
|
|
|
|
select_question()
|
2023-06-18 17:58:59 +00:00
|
|
|
# Pied de page
|
|
|
|
footer_content = Path("donnees/footer.md").read_text()
|
|
|
|
st.markdown(footer_content, unsafe_allow_html=True)
|