-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcgpa_calculator.py
112 lines (97 loc) · 3.42 KB
/
cgpa_calculator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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
import streamlit as st
import json
from streamlit_lottie import st_lottie
import matplotlib.pyplot as plt
import io
def load_lottiefile(filepath: str):
with open(filepath, "r") as f:
return json.load(f)
def calculate_cgpa(credits, gpas):
total_credits = sum(credits)
weighted_gpa = sum(c * g for c, g in zip(credits, gpas))
if total_credits == 0:
return 0
return weighted_gpa / total_credits
def plot_gpas(gpas):
fig, ax = plt.subplots()
semesters = range(1, len(gpas) + 1)
ax.plot(semesters, gpas, marker='o', linestyle='-')
ax.set_xlabel('Semester')
ax.set_ylabel('GPA')
ax.set_title('GPA per Semester')
ax.set_xticks(semesters)
return fig
def main():
# Load Lottie animations from local files
if "lottie_json1" not in st.session_state:
lottie_file1 = "./animation1.json"
st.session_state.lottie_json1 = load_lottiefile(lottie_file1)
if "lottie_json2" not in st.session_state:
lottie_file2 = "./animation2.json"
st.session_state.lottie_json2 = load_lottiefile(lottie_file2)
# Custom CSS
st.markdown("""
<style>
div.stButton > button:first-child {
background-color: #4CAF50;
color: white;
}
[data-testid="stNumberInput"] > div > div > div:nth-child(2) {
display: none;
}
.title {
text-align: center;
font-size: 3em;
font-weight: bold;
padding-top: 20px;
padding-bottom: 10px;
margin-bottom: 0;
}
.tagline {
text-align: center;
font-size: 1.2em;
font-style: italic;
color: #666;
margin-top: 0;
padding-bottom: 20px;
}
.stDownloadButton > button {
background-color: #ff0000 !important;
color: white !important;
}
</style>
""", unsafe_allow_html=True)
st.markdown("<h1 class='title'>GradeGuru</h1>", unsafe_allow_html=True)
st.markdown("<p class='tagline'>Your Personal CGPA Calculator</p>", unsafe_allow_html=True)
if st.session_state.lottie_json1:
st_lottie(st.session_state.lottie_json1, height=200, key="lottie1")
num_semesters = st.number_input("Enter number of semesters", min_value=1, step=1, format="%d")
if num_semesters:
credits = []
gpas = []
for i in range(int(num_semesters)):
st.header(f"Semester {i + 1}")
credit = st.text_input(f"Enter credits for semester {i + 1}", value="", key=f"credit_{i}")
gpa = st.text_input(f"Enter GPA for semester {i + 1}", value="", key=f"gpa_{i}")
credits.append(float(credit) if credit else 0.0)
gpas.append(float(gpa) if gpa else 0.0)
if st.button("Calculate CGPA"):
cgpa = calculate_cgpa(credits, gpas)
if st.session_state.lottie_json2:
st_lottie(st.session_state.lottie_json2, height=200, key="lottie2", loop=False)
st.success(f"Your CGPA is: {cgpa:.2f}")
# Plot GPA graph
fig = plot_gpas(gpas)
st.pyplot(fig)
# Create a download button for the graph
buf = io.BytesIO()
fig.savefig(buf, format="png")
btn = st.download_button(
label="Download GPA Graph",
data=buf.getvalue(),
file_name="gpa_graph.png",
mime="image/png",
key="download_btn",
)
if __name__ == "__main__":
main()