Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,30 +1,24 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import json
|
| 3 |
from io import StringIO
|
|
|
|
| 4 |
|
| 5 |
# Set the page configuration
|
| 6 |
-
st.set_page_config(page_title="Multiple Choice Test Creator", layout="wide")
|
| 7 |
-
|
| 8 |
-
# Initialize session state for questions
|
| 9 |
-
if 'questions' not in st.session_state:
|
| 10 |
-
st.session_state['questions'] = []
|
| 11 |
-
|
| 12 |
-
# Function to add a new question
|
| 13 |
-
def add_question():
|
| 14 |
-
if len(st.session_state['questions']) < 10:
|
| 15 |
-
st.session_state['questions'].append({
|
| 16 |
-
'question': '',
|
| 17 |
-
'options': {'A': '', 'B': '', 'C': '', 'D': ''},
|
| 18 |
-
'correct': 'A'
|
| 19 |
-
})
|
| 20 |
-
else:
|
| 21 |
-
st.warning("You can only add up to 10 questions.")
|
| 22 |
|
| 23 |
-
#
|
| 24 |
-
|
| 25 |
-
st.session_state['
|
| 26 |
|
| 27 |
-
# Function to
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
def generate_html(test_title, questions):
|
| 29 |
html_content = f"""<!DOCTYPE html>
|
| 30 |
<html>
|
|
@@ -73,68 +67,158 @@ def generate_html(test_title, questions):
|
|
| 73 |
"""
|
| 74 |
return html_content
|
| 75 |
|
| 76 |
-
#
|
| 77 |
-
st.
|
| 78 |
-
|
| 79 |
-
#
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
#
|
| 84 |
-
st.
|
| 85 |
-
|
| 86 |
-
#
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
else:
|
| 121 |
-
#
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
if
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
|
|
|
| 2 |
from io import StringIO
|
| 3 |
+
import json
|
| 4 |
|
| 5 |
# Set the page configuration
|
| 6 |
+
st.set_page_config(page_title="π Multiple Choice Test Creator", layout="wide")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
# Initialize session state for storing tests
|
| 9 |
+
if 'tests' not in st.session_state:
|
| 10 |
+
st.session_state['tests'] = []
|
| 11 |
|
| 12 |
+
# Function to add a new test
|
| 13 |
+
def add_test(test_title, questions):
|
| 14 |
+
test = {
|
| 15 |
+
'title': test_title,
|
| 16 |
+
'questions': questions
|
| 17 |
+
}
|
| 18 |
+
st.session_state['tests'].append(test)
|
| 19 |
+
st.success(f"Test '{test_title}' created successfully!")
|
| 20 |
+
|
| 21 |
+
# Function to generate HTML content for the test
|
| 22 |
def generate_html(test_title, questions):
|
| 23 |
html_content = f"""<!DOCTYPE html>
|
| 24 |
<html>
|
|
|
|
| 67 |
"""
|
| 68 |
return html_content
|
| 69 |
|
| 70 |
+
# Tabs for navigation
|
| 71 |
+
tabs = st.tabs(["π Create Test", "π Take Test"])
|
| 72 |
+
|
| 73 |
+
# -------------------- Create Test Tab -------------------- #
|
| 74 |
+
with tabs[0]:
|
| 75 |
+
st.header("Create a New Multiple-Choice Test")
|
| 76 |
+
|
| 77 |
+
# Input for Test Title
|
| 78 |
+
test_title = st.text_input("Enter Test Title", "")
|
| 79 |
+
|
| 80 |
+
# Container for questions
|
| 81 |
+
questions_container = st.container()
|
| 82 |
+
|
| 83 |
+
# Initialize session state for questions if not present
|
| 84 |
+
if 'current_questions' not in st.session_state:
|
| 85 |
+
st.session_state['current_questions'] = []
|
| 86 |
+
|
| 87 |
+
# Function to add a new question
|
| 88 |
+
def add_question():
|
| 89 |
+
if len(st.session_state['current_questions']) < 10:
|
| 90 |
+
st.session_state['current_questions'].append({
|
| 91 |
+
'question': '',
|
| 92 |
+
'options': {'A': '', 'B': '', 'C': '', 'D': ''},
|
| 93 |
+
'correct': 'A'
|
| 94 |
+
})
|
| 95 |
+
else:
|
| 96 |
+
st.warning("You can only add up to 10 questions.")
|
| 97 |
+
|
| 98 |
+
# Button to add a new question
|
| 99 |
+
st.button("β Add a Question", on_click=add_question)
|
| 100 |
+
|
| 101 |
+
# Display existing questions
|
| 102 |
+
for idx, q in enumerate(st.session_state['current_questions']):
|
| 103 |
+
with st.expander(f"Question {idx + 1}", expanded=True):
|
| 104 |
+
# Question Text
|
| 105 |
+
st.text_input("Question Text", value=q['question'], key=f"question_{idx}",
|
| 106 |
+
on_change=lambda i=idx: st.session_state['current_questions'].__setitem__(
|
| 107 |
+
i,
|
| 108 |
+
{'question': st.session_state[f"question_{i}"],
|
| 109 |
+
'options': st.session_state['current_questions'][i]['options'],
|
| 110 |
+
'correct': st.session_state['current_questions'][i]['correct']}
|
| 111 |
+
))
|
| 112 |
+
|
| 113 |
+
# Answer Options
|
| 114 |
+
for option in ['A', 'B', 'C', 'D']:
|
| 115 |
+
st.text_input(f"Answer {option}", value=q['options'][option], key=f"question_{idx}_option_{option}",
|
| 116 |
+
on_change=lambda o=option, i=idx: st.session_state['current_questions'].__setitem__(
|
| 117 |
+
i,
|
| 118 |
+
{
|
| 119 |
+
'question': st.session_state[f"question_{i}"],
|
| 120 |
+
'options': {**st.session_state['current_questions'][i]['options'], o: st.session_state[f"question_{i}_option_{o}"]},
|
| 121 |
+
'correct': st.session_state['current_questions'][i]['correct']
|
| 122 |
+
}
|
| 123 |
+
))
|
| 124 |
+
|
| 125 |
+
# Correct Answer Selector
|
| 126 |
+
correct_option = st.selectbox("Select Correct Answer", options=['A', 'B', 'C', 'D'],
|
| 127 |
+
index=['A','B','C','D'].index(q['correct']),
|
| 128 |
+
key=f"question_{idx}_correct",
|
| 129 |
+
on_change=lambda c='A', i=idx: st.session_state['current_questions'].__setitem__(
|
| 130 |
+
i,
|
| 131 |
+
{
|
| 132 |
+
'question': st.session_state[f"question_{i}"],
|
| 133 |
+
'options': st.session_state['current_questions'][i]['options'],
|
| 134 |
+
'correct': st.session_state[f"question_{i}_correct"]
|
| 135 |
+
}
|
| 136 |
+
))
|
| 137 |
+
# Update the correct answer in session_state
|
| 138 |
+
st.session_state['current_questions'][idx]['correct'] = correct_option
|
| 139 |
+
|
| 140 |
+
# Button to create the test
|
| 141 |
+
if st.button("β
Create Test"):
|
| 142 |
+
if not test_title:
|
| 143 |
+
st.error("Please enter a test title.")
|
| 144 |
+
elif len(st.session_state['current_questions']) == 0:
|
| 145 |
+
st.error("Please add at least one question.")
|
| 146 |
+
else:
|
| 147 |
+
# Validate that all questions have all fields filled
|
| 148 |
+
incomplete = False
|
| 149 |
+
for idx, q in enumerate(st.session_state['current_questions'], 1):
|
| 150 |
+
if not q['question'] or not all(q['options'][opt] for opt in ['A','B','C','D']):
|
| 151 |
+
st.error(f"Question {idx} is incomplete. Please fill in all fields.")
|
| 152 |
+
incomplete = True
|
| 153 |
+
break
|
| 154 |
+
if not incomplete:
|
| 155 |
+
add_test(test_title, st.session_state['current_questions'])
|
| 156 |
+
# Clear current questions after creation
|
| 157 |
+
st.session_state['current_questions'] = []
|
| 158 |
+
# Provide download button
|
| 159 |
+
html_content = generate_html(test_title, st.session_state['tests'][-1]['questions'])
|
| 160 |
+
st.download_button(
|
| 161 |
+
label="π Download Test as HTML",
|
| 162 |
+
data=html_content,
|
| 163 |
+
file_name=f"{test_title.replace(' ', '_')}.html",
|
| 164 |
+
mime="text/html"
|
| 165 |
+
)
|
| 166 |
+
# Optionally, display the test
|
| 167 |
+
st.markdown("---")
|
| 168 |
+
st.header("π Your Test")
|
| 169 |
+
st.markdown(html_content, unsafe_allow_html=True)
|
| 170 |
+
|
| 171 |
+
# -------------------- Take Test Tab -------------------- #
|
| 172 |
+
with tabs[1]:
|
| 173 |
+
st.header("Take a Multiple-Choice Test")
|
| 174 |
+
|
| 175 |
+
if len(st.session_state['tests']) == 0:
|
| 176 |
+
st.info("No tests available. Please create a test first.")
|
| 177 |
else:
|
| 178 |
+
# Select a test to take
|
| 179 |
+
test_titles = [test['title'] for test in st.session_state['tests']]
|
| 180 |
+
selected_test_title = st.selectbox("Select a Test to Take", options=test_titles)
|
| 181 |
+
|
| 182 |
+
# Retrieve the selected test
|
| 183 |
+
selected_test = next((test for test in st.session_state['tests'] if test['title'] == selected_test_title), None)
|
| 184 |
+
|
| 185 |
+
if selected_test:
|
| 186 |
+
# Initialize session state for user answers if not present
|
| 187 |
+
if 'user_answers' not in st.session_state:
|
| 188 |
+
st.session_state['user_answers'] = {}
|
| 189 |
+
|
| 190 |
+
with st.form("take_test_form"):
|
| 191 |
+
for idx, q in enumerate(selected_test['questions'], 1):
|
| 192 |
+
st.markdown(f"**{idx}. {q['question']}**")
|
| 193 |
+
answer = st.radio(
|
| 194 |
+
f"Select an answer for Question {idx}",
|
| 195 |
+
options=['A', 'B', 'C', 'D'],
|
| 196 |
+
key=f"test_{selected_test_title}_q{idx}"
|
| 197 |
+
)
|
| 198 |
+
st.session_state['user_answers'][idx] = answer
|
| 199 |
+
st.markdown("---")
|
| 200 |
+
|
| 201 |
+
# Submit button
|
| 202 |
+
submit = st.form_submit_button("Submit Test")
|
| 203 |
+
|
| 204 |
+
if submit:
|
| 205 |
+
# Grade the test
|
| 206 |
+
score = 0
|
| 207 |
+
total = len(selected_test['questions'])
|
| 208 |
+
for idx, q in enumerate(selected_test['questions'], 1):
|
| 209 |
+
if st.session_state['user_answers'][idx] == q['correct']:
|
| 210 |
+
score += 1
|
| 211 |
+
|
| 212 |
+
# Display the result
|
| 213 |
+
st.success(f"You scored {score} out of {total}.")
|
| 214 |
+
|
| 215 |
+
# Optionally, display correct answers
|
| 216 |
+
with st.expander("View Correct Answers"):
|
| 217 |
+
for idx, q in enumerate(selected_test['questions'], 1):
|
| 218 |
+
user_ans = st.session_state['user_answers'][idx]
|
| 219 |
+
correct_ans = q['correct']
|
| 220 |
+
is_correct = user_ans == correct_ans
|
| 221 |
+
result = "β
Correct" if is_correct else f"β Incorrect (Correct Answer: {correct_ans})"
|
| 222 |
+
st.markdown(f"**{idx}. {q['question']}**")
|
| 223 |
+
st.markdown(f"Your Answer: {user_ans} - {result}")
|
| 224 |
+
st.markdown("---")
|