77 lines
2.3 KiB
Python
77 lines
2.3 KiB
Python
|
import json
|
||
|
import random
|
||
|
import streamlit as st
|
||
|
from streamlit import session_state as ss
|
||
|
|
||
|
# Load the questions from the JSON file
|
||
|
if "loaded_questions" not in ss:
|
||
|
with open("donnees/questions.json") as file:
|
||
|
questions = json.load(file)["questions"]
|
||
|
ss.loaded_questions = questions
|
||
|
|
||
|
# Initialize session state
|
||
|
if "current_question" not in ss:
|
||
|
ss.current_question = None
|
||
|
ss.current_answers = None
|
||
|
ss.selected_answer = None
|
||
|
|
||
|
|
||
|
# Function to select a random question
|
||
|
def select_question():
|
||
|
ss.current_question = None
|
||
|
ss.current_answers = None
|
||
|
if len(ss.loaded_questions) > 0:
|
||
|
questions = ss.loaded_questions
|
||
|
random.shuffle(questions)
|
||
|
ss.current_question = questions.pop()
|
||
|
ss.selected_answer = None
|
||
|
# 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"])
|
||
|
|
||
|
|
||
|
# Main app
|
||
|
st.title("Culture Créative - Licences Creative Commons")
|
||
|
st.markdown("Découvre les licences Creative Commons, "
|
||
|
"qui permettent de partager ses contenus sur Internet "
|
||
|
"sous diverses conditions")
|
||
|
|
||
|
# 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)
|
||
|
# Display the correct answer
|
||
|
if st.form_submit_button():
|
||
|
check_answer()
|
||
|
# Reset button to load a new question
|
||
|
st.write("Il faut appuyer deux fois sur le bouton Reset pour avoir une nouvelle question.")
|
||
|
if st.button("Reset"):
|
||
|
select_question()
|