Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -15,6 +15,10 @@ total_marks_list = [60, 60, 60, 60, 60, 80]
|
|
| 15 |
# Streamlit UI
|
| 16 |
st.set_page_config(page_title="Quiz Result Calculator", layout="centered")
|
| 17 |
st.title("π― Hacking Page β Quiz Marks Weighted Percentage Calculator")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
st.markdown("**Enter only the marks obtained below.**<br>Total marks are fixed: 60 for quizzes and 80 for the Grand Quiz.", unsafe_allow_html=True)
|
| 19 |
|
| 20 |
marks_obtained = []
|
|
@@ -29,13 +33,15 @@ for i in range(6):
|
|
| 29 |
marks_obtained.append(obtained)
|
| 30 |
|
| 31 |
# Function to create PDF
|
| 32 |
-
def generate_result_card_pdf(data, final_percentage):
|
| 33 |
pdf_path = "result_card.pdf"
|
| 34 |
doc = SimpleDocTemplate(pdf_path, pagesize=A4)
|
| 35 |
elements = []
|
| 36 |
styles = getSampleStyleSheet()
|
| 37 |
|
| 38 |
elements.append(Paragraph("π Quiz Result Card", styles['Title']))
|
|
|
|
|
|
|
| 39 |
elements.append(Spacer(1, 12))
|
| 40 |
|
| 41 |
table_data = [["Quiz", "Marks Obtained", "Total Marks", "Weightage (%)", "Section %", "Contribution (%)"]]
|
|
@@ -69,47 +75,50 @@ def generate_result_card_pdf(data, final_percentage):
|
|
| 69 |
|
| 70 |
# On button click
|
| 71 |
if st.button("π Calculate Result"):
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
# Streamlit UI
|
| 16 |
st.set_page_config(page_title="Quiz Result Calculator", layout="centered")
|
| 17 |
st.title("π― Hacking Page β Quiz Marks Weighted Percentage Calculator")
|
| 18 |
+
|
| 19 |
+
# Input for name
|
| 20 |
+
student_name = st.text_input("Enter your name:", max_chars=50)
|
| 21 |
+
|
| 22 |
st.markdown("**Enter only the marks obtained below.**<br>Total marks are fixed: 60 for quizzes and 80 for the Grand Quiz.", unsafe_allow_html=True)
|
| 23 |
|
| 24 |
marks_obtained = []
|
|
|
|
| 33 |
marks_obtained.append(obtained)
|
| 34 |
|
| 35 |
# Function to create PDF
|
| 36 |
+
def generate_result_card_pdf(data, final_percentage, student_name):
|
| 37 |
pdf_path = "result_card.pdf"
|
| 38 |
doc = SimpleDocTemplate(pdf_path, pagesize=A4)
|
| 39 |
elements = []
|
| 40 |
styles = getSampleStyleSheet()
|
| 41 |
|
| 42 |
elements.append(Paragraph("π Quiz Result Card", styles['Title']))
|
| 43 |
+
elements.append(Spacer(1, 8))
|
| 44 |
+
elements.append(Paragraph(f"<b>Name:</b> {student_name}", styles['Heading3']))
|
| 45 |
elements.append(Spacer(1, 12))
|
| 46 |
|
| 47 |
table_data = [["Quiz", "Marks Obtained", "Total Marks", "Weightage (%)", "Section %", "Contribution (%)"]]
|
|
|
|
| 75 |
|
| 76 |
# On button click
|
| 77 |
if st.button("π Calculate Result"):
|
| 78 |
+
if not student_name.strip():
|
| 79 |
+
st.error("Please enter your name to generate the result card.")
|
| 80 |
+
else:
|
| 81 |
+
data = []
|
| 82 |
+
total_weighted_percentage = 0
|
| 83 |
+
section_percentages = []
|
| 84 |
+
|
| 85 |
+
for i in range(6):
|
| 86 |
+
total = total_marks_list[i]
|
| 87 |
+
percent = (marks_obtained[i] / total) * 100
|
| 88 |
+
contribution = percent * (weightages[i] / 100)
|
| 89 |
+
total_weighted_percentage += contribution
|
| 90 |
+
section_percentages.append(percent)
|
| 91 |
+
|
| 92 |
+
data.append({
|
| 93 |
+
"Quiz": quiz_labels[i],
|
| 94 |
+
"Marks Obtained": marks_obtained[i],
|
| 95 |
+
"Total Marks": total,
|
| 96 |
+
"Weightage (%)": weightages[i],
|
| 97 |
+
"Section %": round(percent, 2),
|
| 98 |
+
"Contribution (%)": round(contribution, 2)
|
| 99 |
+
})
|
| 100 |
+
|
| 101 |
+
df = pd.DataFrame(data)
|
| 102 |
+
df.set_index("Quiz", inplace=True)
|
| 103 |
+
|
| 104 |
+
st.markdown("### π Results Table")
|
| 105 |
+
st.dataframe(df)
|
| 106 |
+
|
| 107 |
+
st.success(f"β
Final Weighted Percentage: **{total_weighted_percentage:.2f}%**")
|
| 108 |
+
|
| 109 |
+
# Plotting
|
| 110 |
+
fig, ax = plt.subplots()
|
| 111 |
+
ax.barh(quiz_labels, section_percentages, color='skyblue')
|
| 112 |
+
ax.set_xlabel("Percentage %")
|
| 113 |
+
ax.set_title("Marks Percentage per Quiz")
|
| 114 |
+
st.pyplot(fig)
|
| 115 |
+
|
| 116 |
+
# Generate PDF with student name
|
| 117 |
+
pdf_path = generate_result_card_pdf(data, total_weighted_percentage, student_name)
|
| 118 |
+
with open(pdf_path, "rb") as f:
|
| 119 |
+
st.download_button(
|
| 120 |
+
label="π Download Result Card (PDF)",
|
| 121 |
+
data=f,
|
| 122 |
+
file_name=f"{student_name.replace(' ', '_')}_result_card.pdf",
|
| 123 |
+
mime="application/pdf"
|
| 124 |
+
)
|