import json import logging 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 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 # Function to select a random question def select_question(): """ 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 # Function to check the selected answer def check_answer(): """ This function checks the selected answer. :return: """ logging.info("Appel de la fonction check_answer()") correct_answer = ss.current_question["answers"][0] selected_answer = ss.selected_answer licence = ss.current_question["licence"] nom = licences_dict[licence]["name"] url = licences_dict[licence]["url"] logo = licences_dict[licence]["logo"] 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"]) st.markdown(f"Cette licence se nomme [{nom}]({url})") st.image(f"logos/{logo}") 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 answer with st.form("answer_form"): logging.info("Afficher la question") # Display the current question st.subheader("Question") st.write(ss.current_question["question"]) # Display the answer options logging.info("Afficher les réponses possibles") 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 != "---": logging.info("Vérifier la réponse") check_answer() if st.form_submit_button(label="Prochaine question"): logging.info("Prochaine question") ss.clear() select_question() # Pied de page footer_content = Path("donnees/footer.md").read_text() st.markdown(footer_content, unsafe_allow_html=True)