Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
# Function to remove a question
|
| 24 |
+
def remove_question(index):
|
| 25 |
+
st.session_state['questions'].pop(index)
|
| 26 |
+
|
| 27 |
+
# Function to generate HTML content
|
| 28 |
+
def generate_html(test_title, questions):
|
| 29 |
+
html_content = f"""<!DOCTYPE html>
|
| 30 |
+
<html>
|
| 31 |
+
<head>
|
| 32 |
+
<meta charset="UTF-8">
|
| 33 |
+
<title>{test_title}</title>
|
| 34 |
+
<style>
|
| 35 |
+
body {{
|
| 36 |
+
font-family: Arial, sans-serif;
|
| 37 |
+
margin: 40px;
|
| 38 |
+
background-color: #f9f9f9;
|
| 39 |
+
}}
|
| 40 |
+
.test-title {{
|
| 41 |
+
text-align: center;
|
| 42 |
+
font-size: 2em;
|
| 43 |
+
margin-bottom: 50px;
|
| 44 |
+
}}
|
| 45 |
+
.question {{
|
| 46 |
+
margin-bottom: 30px;
|
| 47 |
+
}}
|
| 48 |
+
.options label {{
|
| 49 |
+
display: block;
|
| 50 |
+
margin: 5px 0;
|
| 51 |
+
}}
|
| 52 |
+
</style>
|
| 53 |
+
</head>
|
| 54 |
+
<body>
|
| 55 |
+
<div class="test-title">{test_title}</div>
|
| 56 |
+
"""
|
| 57 |
+
|
| 58 |
+
for idx, q in enumerate(questions, 1):
|
| 59 |
+
html_content += f"""
|
| 60 |
+
<div class="question">
|
| 61 |
+
<div class="question-text"><strong>Question {idx}:</strong> {q['question']}</div>
|
| 62 |
+
<div class="options">
|
| 63 |
+
<label><input type="radio" name="q{idx}" value="A"> A. {q['options']['A']}</label>
|
| 64 |
+
<label><input type="radio" name="q{idx}" value="B"> B. {q['options']['B']}</label>
|
| 65 |
+
<label><input type="radio" name="q{idx}" value="C"> C. {q['options']['C']}</label>
|
| 66 |
+
<label><input type="radio" name="q{idx}" value="D"> D. {q['options']['D']}</label>
|
| 67 |
+
</div>
|
| 68 |
+
</div>
|
| 69 |
+
"""
|
| 70 |
+
html_content += """
|
| 71 |
+
</body>
|
| 72 |
+
</html>
|
| 73 |
+
"""
|
| 74 |
+
return html_content
|
| 75 |
+
|
| 76 |
+
# Title of the app
|
| 77 |
+
st.title("π Multiple Choice Test Creator")
|
| 78 |
+
|
| 79 |
+
# Input for Test Title
|
| 80 |
+
st.header("Create a New Test")
|
| 81 |
+
test_title = st.text_input("Test Title", "")
|
| 82 |
+
|
| 83 |
+
# Button to add a new question
|
| 84 |
+
st.button("β Add a Question", on_click=add_question)
|
| 85 |
+
|
| 86 |
+
# Display existing questions
|
| 87 |
+
for idx, q in enumerate(st.session_state['questions']):
|
| 88 |
+
with st.expander(f"Question {idx + 1}", expanded=True):
|
| 89 |
+
col1, col2 = st.columns([4,1])
|
| 90 |
+
with col1:
|
| 91 |
+
st.text_input(f"Question {idx + 1} Text", value=q['question'], key=f"question_{idx}", on_change=lambda i=idx: st.session_state['questions'].__setitem__(i, {'question': st.session_state[f'question_{i}'], 'options': st.session_state['questions'][i]['options'], 'correct': st.session_state['questions'][i]['correct']}))
|
| 92 |
+
with col2:
|
| 93 |
+
if st.button("ποΈ Remove", key=f"remove_{idx}"):
|
| 94 |
+
remove_question(idx)
|
| 95 |
+
st.experimental_rerun()
|
| 96 |
+
|
| 97 |
+
# Options for answers
|
| 98 |
+
for option in ['A', 'B', 'C', 'D']:
|
| 99 |
+
st.text_input(f"Answer {option}", value=q['options'][option], key=f"question_{idx}_option_{option}", on_change=lambda o=option, i=idx: st.session_state['questions'].__setitem__(i, {
|
| 100 |
+
'question': st.session_state[f'question_{i}'],
|
| 101 |
+
'options': {**st.session_state['questions'][i]['options'], o: st.session_state[f'question_{i}_option_{o}']},
|
| 102 |
+
'correct': st.session_state['questions'][i]['correct']
|
| 103 |
+
}))
|
| 104 |
+
|
| 105 |
+
# Correct answer selector
|
| 106 |
+
correct_option = st.selectbox("Select Correct Answer", options=['A', 'B', 'C', 'D'], index=['A','B','C','D'].index(q['correct']), key=f"question_{idx}_correct", on_change=lambda c='A', i=idx: st.session_state['questions'].__setitem__(i, {
|
| 107 |
+
'question': st.session_state[f'question_{i}'],
|
| 108 |
+
'options': st.session_state['questions'][i]['options'],
|
| 109 |
+
'correct': st.session_state[f'question_{i}_correct']
|
| 110 |
+
}))
|
| 111 |
+
# Update the correct answer in session_state
|
| 112 |
+
st.session_state['questions'][idx]['correct'] = correct_option
|
| 113 |
+
|
| 114 |
+
# Button to generate and download the test
|
| 115 |
+
if st.button("π₯ Generate and Download Test"):
|
| 116 |
+
if not test_title:
|
| 117 |
+
st.error("Please enter a test title.")
|
| 118 |
+
elif len(st.session_state['questions']) == 0:
|
| 119 |
+
st.error("Please add at least one question.")
|
| 120 |
+
else:
|
| 121 |
+
# Validate that all questions have all fields filled
|
| 122 |
+
incomplete = False
|
| 123 |
+
for idx, q in enumerate(st.session_state['questions'], 1):
|
| 124 |
+
if not q['question'] or not all(q['options'][opt] for opt in ['A','B','C','D']):
|
| 125 |
+
st.error(f"Question {idx} is incomplete. Please fill in all fields.")
|
| 126 |
+
incomplete = True
|
| 127 |
+
break
|
| 128 |
+
if not incomplete:
|
| 129 |
+
html_content = generate_html(test_title, st.session_state['questions'])
|
| 130 |
+
# Provide download button
|
| 131 |
+
st.download_button(
|
| 132 |
+
label="π Download Test as HTML",
|
| 133 |
+
data=html_content,
|
| 134 |
+
file_name=f"{test_title.replace(' ', '_')}.html",
|
| 135 |
+
mime="text/html"
|
| 136 |
+
)
|
| 137 |
+
# Optionally, display the test
|
| 138 |
+
st.markdown("---")
|
| 139 |
+
st.header("π Your Test")
|
| 140 |
+
st.markdown(html_content, unsafe_allow_html=True)
|