|
|
import streamlit as st |
|
|
import pandas as pd |
|
|
|
|
|
def main(): |
|
|
st.title("Quiz - Bahria College") |
|
|
st.write("Create quizzes, collect responses, and view results!") |
|
|
|
|
|
|
|
|
menu = ["Create Quiz", "Take Quiz", "View Results"] |
|
|
choice = st.sidebar.selectbox("Menu", menu) |
|
|
|
|
|
if choice == "Create Quiz": |
|
|
create_quiz() |
|
|
elif choice == "Take Quiz": |
|
|
take_quiz() |
|
|
elif choice == "View Results": |
|
|
view_results() |
|
|
|
|
|
def create_quiz(): |
|
|
st.header("Create a New Quiz") |
|
|
|
|
|
|
|
|
quiz_name = st.text_input("Quiz Name") |
|
|
|
|
|
if "quizzes" not in st.session_state: |
|
|
st.session_state.quizzes = {} |
|
|
|
|
|
if quiz_name: |
|
|
if quiz_name not in st.session_state.quizzes: |
|
|
st.session_state.quizzes[quiz_name] = [] |
|
|
|
|
|
st.write(f"Editing quiz: {quiz_name}") |
|
|
|
|
|
|
|
|
with st.form(key="question_form"): |
|
|
question = st.text_input("Question") |
|
|
question_type = st.selectbox("Question Type", ["Multiple Choice", "Short Answer"]) |
|
|
|
|
|
if question_type == "Multiple Choice": |
|
|
options = st.text_area("Options (comma-separated)") |
|
|
correct_option = st.text_input("Correct Option") |
|
|
else: |
|
|
options = None |
|
|
correct_option = None |
|
|
|
|
|
submitted = st.form_submit_button("Add Question") |
|
|
|
|
|
if submitted and question: |
|
|
st.session_state.quizzes[quiz_name].append({ |
|
|
"question": question, |
|
|
"type": question_type, |
|
|
"options": options.split(",") if options else [], |
|
|
"correct": correct_option, |
|
|
}) |
|
|
st.success("Question added successfully!") |
|
|
|
|
|
|
|
|
st.subheader("Questions") |
|
|
for idx, q in enumerate(st.session_state.quizzes[quiz_name]): |
|
|
st.write(f"{idx + 1}. {q['question']}") |
|
|
|
|
|
else: |
|
|
st.warning("Please enter a quiz name to start.") |
|
|
|
|
|
def take_quiz(): |
|
|
st.header("Take a Quiz") |
|
|
|
|
|
if "quizzes" not in st.session_state or not st.session_state.quizzes: |
|
|
st.warning("No quizzes available. Please create a quiz first.") |
|
|
return |
|
|
|
|
|
quiz_name = st.selectbox("Select a Quiz", list(st.session_state.quizzes.keys())) |
|
|
responses = [] |
|
|
|
|
|
if quiz_name: |
|
|
st.write(f"Quiz: {quiz_name}") |
|
|
quiz = st.session_state.quizzes[quiz_name] |
|
|
|
|
|
for idx, q in enumerate(quiz): |
|
|
st.write(f"{idx + 1}. {q['question']}\n") |
|
|
|
|
|
if q['type'] == "Multiple Choice": |
|
|
answer = st.radio("", q['options'], key=f"question_{idx}") |
|
|
else: |
|
|
answer = st.text_input("Your Answer", key=f"question_{idx}") |
|
|
|
|
|
responses.append(answer) |
|
|
|
|
|
if st.button("Submit Quiz"): |
|
|
if "results" not in st.session_state: |
|
|
st.session_state.results = {} |
|
|
|
|
|
st.session_state.results[quiz_name] = responses |
|
|
st.success("Quiz submitted successfully!") |
|
|
|
|
|
def view_results(): |
|
|
st.header("View Results") |
|
|
|
|
|
if "results" not in st.session_state or not st.session_state.results: |
|
|
st.warning("No results available.") |
|
|
return |
|
|
|
|
|
quiz_name = st.selectbox("Select a Quiz", list(st.session_state.results.keys())) |
|
|
|
|
|
if quiz_name: |
|
|
st.write(f"Results for: {quiz_name}") |
|
|
results = st.session_state.results[quiz_name] |
|
|
|
|
|
quiz = st.session_state.quizzes[quiz_name] |
|
|
correct_count = 0 |
|
|
|
|
|
for idx, (response, question) in enumerate(zip(results, quiz)): |
|
|
st.write(f"Q{idx + 1}: {question['question']}") |
|
|
st.write(f"Your Answer: {response}") |
|
|
|
|
|
if question['type'] == "Multiple Choice" and response == question['correct']: |
|
|
correct_count += 1 |
|
|
elif question['type'] == "Short Answer" and response and question['correct'] and response.lower() == question['correct'].lower(): |
|
|
correct_count += 1 |
|
|
|
|
|
st.write(f"Score: {correct_count}/{len(quiz)}") |
|
|
|
|
|
if __name__ == "__main__": |
|
|
main() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|