Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
|
| 3 |
+
# Grade to points mapping
|
| 4 |
+
grade_points = {
|
| 5 |
+
'A+': 4.0,
|
| 6 |
+
'A': 4.0,
|
| 7 |
+
'A-': 3.7,
|
| 8 |
+
'B+': 3.3,
|
| 9 |
+
'B': 3.0,
|
| 10 |
+
'B-': 2.7,
|
| 11 |
+
'C+': 2.3,
|
| 12 |
+
'C': 2.0,
|
| 13 |
+
'C-': 1.7,
|
| 14 |
+
'D': 1.0,
|
| 15 |
+
'F': 0.0
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
# Initialize session state
|
| 19 |
+
if 'courses' not in st.session_state:
|
| 20 |
+
st.session_state.courses = []
|
| 21 |
+
|
| 22 |
+
st.title("🎓 GPA Calculator")
|
| 23 |
+
st.subheader("Calculate your Semester GPA")
|
| 24 |
+
|
| 25 |
+
# Course input form
|
| 26 |
+
with st.form("course_form"):
|
| 27 |
+
col1, col2, col3 = st.columns([3, 1, 1])
|
| 28 |
+
with col1:
|
| 29 |
+
course_name = st.text_input("Course Name (optional)", key=f"name_{len(st.session_state.courses)}")
|
| 30 |
+
with col2:
|
| 31 |
+
credits = st.number_input("Credits", min_value=1, max_value=5, value=3, key=f"credits_{len(st.session_state.courses)}")
|
| 32 |
+
with col3:
|
| 33 |
+
grade = st.selectbox("Grade", list(grade_points.keys()), key=f"grade_{len(st.session_state.courses)}")
|
| 34 |
+
|
| 35 |
+
submitted = st.form_submit_button("Add Course")
|
| 36 |
+
if submitted:
|
| 37 |
+
st.session_state.courses.append({
|
| 38 |
+
'name': course_name,
|
| 39 |
+
'credits': credits,
|
| 40 |
+
'grade': grade
|
| 41 |
+
})
|
| 42 |
+
|
| 43 |
+
# Display entered courses
|
| 44 |
+
if st.session_state.courses:
|
| 45 |
+
st.write("### Entered Courses")
|
| 46 |
+
for i, course in enumerate(st.session_state.courses, 1):
|
| 47 |
+
with st.expander(f"Course {i}: {course['name'] or 'Unnamed Course'}"):
|
| 48 |
+
col1, col2, col3 = st.columns([3, 1, 1])
|
| 49 |
+
col1.write(f"Course Name: {course['name'] or 'N/A'}")
|
| 50 |
+
col2.write(f"Credits: {course['credits']}")
|
| 51 |
+
col3.write(f"Grade: {course['grade']}")
|
| 52 |
+
|
| 53 |
+
# Calculate GPA
|
| 54 |
+
if st.button("Calculate GPA"):
|
| 55 |
+
total_points = 0
|
| 56 |
+
total_credits = 0
|
| 57 |
+
|
| 58 |
+
for course in st.session_state.courses:
|
| 59 |
+
points = grade_points[course['grade']]
|
| 60 |
+
total_points += points * course['credits']
|
| 61 |
+
total_credits += course['credits']
|
| 62 |
+
|
| 63 |
+
if total_credits == 0:
|
| 64 |
+
st.error("Please add at least one course with credits")
|
| 65 |
+
else:
|
| 66 |
+
gpa = total_points / total_credits
|
| 67 |
+
st.success(f"Your GPA is: **{gpa:.2f}**")
|
| 68 |
+
|
| 69 |
+
# Display detailed breakdown
|
| 70 |
+
st.write("### Breakdown")
|
| 71 |
+
st.write(f"Total Quality Points: {total_points:.2f}")
|
| 72 |
+
st.write(f"Total Credits: {total_credits}")
|
| 73 |
+
st.write(f"GPA: {gpa:.2f}")
|
| 74 |
+
|
| 75 |
+
# Reset button
|
| 76 |
+
if st.button("Reset All Courses"):
|
| 77 |
+
st.session_state.courses = []
|
| 78 |
+
st.experimental_rerun()
|