Spaces:
Sleeping
Sleeping
Create App.py
Browse files
App.py
ADDED
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
|
4 |
+
# Grade to grade point mapping
|
5 |
+
grade_points = {
|
6 |
+
"A": 4.0, "A-": 3.7,
|
7 |
+
"B+": 3.3, "B": 3.0, "B-": 2.7,
|
8 |
+
"C+": 2.3, "C": 2.0, "C-": 1.7,
|
9 |
+
"D+": 1.3, "D": 1.0, "F": 0.0
|
10 |
+
}
|
11 |
+
|
12 |
+
# Configure page
|
13 |
+
st.set_page_config(page_title="GPA/CGPA Calculator", layout="wide")
|
14 |
+
|
15 |
+
# Title and description
|
16 |
+
st.markdown("""
|
17 |
+
<h1 style='text-align: center; color: #4CAF50;'>π GPA / CGPA Calculator</h1>
|
18 |
+
<p style='text-align: center;'>Add your current subjects and optionally previous semester GPA to calculate your CGPA</p>
|
19 |
+
""", unsafe_allow_html=True)
|
20 |
+
|
21 |
+
# Initialize session state
|
22 |
+
if "subjects" not in st.session_state:
|
23 |
+
st.session_state.subjects = []
|
24 |
+
|
25 |
+
# Optional previous semester data
|
26 |
+
with st.expander("π Add Previous Semester Data (Optional)", expanded=False):
|
27 |
+
prev_credits = st.number_input("Total Credit Hours from Previous Semesters",
|
28 |
+
min_value=0.0, step=0.5, value=0.0)
|
29 |
+
prev_gpa = st.number_input("Cumulative GPA from Previous Semesters",
|
30 |
+
min_value=0.0, max_value=4.0, step=0.01, value=0.0)
|
31 |
+
|
32 |
+
# Subject form
|
33 |
+
with st.form("subject_form", clear_on_submit=True):
|
34 |
+
col1, col2, col3 = st.columns(3)
|
35 |
+
with col1:
|
36 |
+
subject = st.text_input("Subject Name", placeholder="e.g., Mathematics")
|
37 |
+
with col2:
|
38 |
+
grade = st.selectbox("Grade", list(grade_points.keys()))
|
39 |
+
with col3:
|
40 |
+
credit = st.number_input("Credit Hours", min_value=0.5, max_value=6.0, step=0.5, value=3.0)
|
41 |
+
|
42 |
+
submitted = st.form_submit_button("β Add Subject")
|
43 |
+
|
44 |
+
if submitted:
|
45 |
+
if not subject.strip():
|
46 |
+
st.error("Please enter a subject name")
|
47 |
+
elif credit <= 0:
|
48 |
+
st.error("Credit hours must be positive")
|
49 |
+
else:
|
50 |
+
st.session_state.subjects.append({
|
51 |
+
"Subject": subject.strip(),
|
52 |
+
"Grade": grade,
|
53 |
+
"Credit": credit
|
54 |
+
})
|
55 |
+
st.success(f"Added {subject.strip()} ({grade}, {credit} credits)")
|
56 |
+
|
57 |
+
# Display current subjects
|
58 |
+
if st.session_state.subjects:
|
59 |
+
st.markdown("### π§Ύ Current Semester Subjects")
|
60 |
+
df = pd.DataFrame(st.session_state.subjects)
|
61 |
+
st.dataframe(df, use_container_width=True, hide_index=True)
|
62 |
+
|
63 |
+
# Calculate current semester GPA
|
64 |
+
try:
|
65 |
+
current_points = sum(grade_points[row["Grade"]] * row["Credit"] for row in st.session_state.subjects)
|
66 |
+
current_credits = sum(row["Credit"] for row in st.session_state.subjects)
|
67 |
+
current_gpa = round(current_points / current_credits, 2)
|
68 |
+
|
69 |
+
# Calculate CGPA (if previous data exists)
|
70 |
+
if prev_credits > 0:
|
71 |
+
total_credits = prev_credits + current_credits
|
72 |
+
total_points = (prev_gpa * prev_credits) + current_points
|
73 |
+
cgpa = round(total_points / total_credits, 2)
|
74 |
+
else:
|
75 |
+
cgpa = current_gpa
|
76 |
+
|
77 |
+
# Display results in a nice card
|
78 |
+
st.markdown(f"""
|
79 |
+
<div style="background-color:#f0f2f6;padding:20px;border-radius:10px;margin-top:20px;">
|
80 |
+
<h3>π Current Semester GPA: <span style="color:#2196F3;">{current_gpa:.2f}</span></h3>
|
81 |
+
<h3>π Cumulative GPA (CGPA): <span style="color:#4CAF50;">{cgpa:.2f}</span></h3>
|
82 |
+
<p>Total Credits: {current_credits + prev_credits}</p>
|
83 |
+
</div>
|
84 |
+
""", unsafe_allow_html=True)
|
85 |
+
|
86 |
+
except ZeroDivisionError:
|
87 |
+
st.error("Error: Cannot calculate GPA with zero credit hours")
|
88 |
+
|
89 |
+
else:
|
90 |
+
st.info("βΉοΈ Add at least one subject to calculate your GPA")
|
91 |
+
|
92 |
+
# Clear button (only shown when there are subjects)
|
93 |
+
if st.session_state.subjects:
|
94 |
+
if st.button("ποΈ Clear All Subjects", type="primary"):
|
95 |
+
st.session_state.subjects = []
|
96 |
+
st.rerun()
|
97 |
+
|
98 |
+
# Footer
|
99 |
+
st.markdown("---")
|
100 |
+
st.markdown("""
|
101 |
+
<div style="text-align: center; color: #777; font-size: 0.9em;">
|
102 |
+
<p>GPA Calculator β’ Grades: A (4.0) to F (0.0)</p>
|
103 |
+
</div>
|
104 |
+
""", unsafe_allow_html=True)
|