-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
106 lines (90 loc) · 2.41 KB
/
main.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
import streamlit as st
import numpy as np
st.set_page_config(
page_title="CGPA Calculator",
page_icon="📊",
layout="centered",
initial_sidebar_state="auto",
)
grade_to_point = {
"O": 10,
"A+": 9,
"A": 8,
"B+": 7,
"B": 6,
"C": 5,
}
grades = list(grade_to_point.keys())
def calculate_cgpa(
grade: np.array,
credit: np.array,
previous_cgpa: float = 0,
previous_credit: float = 0,
):
# grade = np.array([grade_to_point[g] for g in grade])
total_credit = credit.sum() + previous_credit
total_grade = (grade * credit).sum() + previous_cgpa * previous_credit
return total_grade / total_credit
st.title("CGPA Calculator")
st.markdown(
"This is a simple CGPA calculator that calculates your CGPA based on your grades and credits"
)
st.latex(r"CGPA = \frac{\sum_{i=1}^{n} (grade_i * credit_i)}{\sum_{i=1}^{n} credit_i}")
cols = st.columns(2)
previous_cgpa = cols[0].number_input(
label="Previous CGPA",
help="Enter Your CGPA upto previous semester",
min_value=0.00,
value=0.00,
step=0.01,
)
previous_credit = cols[1].number_input(
label="Previous Credit",
help="Enter the total number of credits you have taken upto previous semester",
min_value=0.0,
value=0.0,
step=0.5,
)
number_of_subjects = st.number_input(
label="Number of Subjects",
help="Enter the number of subjects you are taking this semester",
min_value=1,
max_value=10,
value=5,
)
grade = np.array([0] * number_of_subjects)
credit = np.array([0] * number_of_subjects)
for i in range(number_of_subjects):
st.subheader(f"Subject #{i+1}")
cols = st.columns(2)
grade[i] = grade_to_point[
cols[0].selectbox(
label=f"Grade",
options=grades,
key=f"selectbox_{i}",
)
]
credit[i] = cols[1].number_input(
label=f"Credit",
min_value=1.0,
max_value=10.0,
value=4.0,
step=0.5,
key=f"number_input_{i}",
)
if st.button("Calculate"):
st.info(f"Your semester GPA is {calculate_cgpa(grade, credit):.2f}")
st.success(
f"Your Cumulative GPA is {calculate_cgpa(grade, credit, previous_cgpa, previous_credit):.2f}"
)
st.markdown("Made with ❤️ by [Ankit Yadav](https://github.com/Ankitsparinco)")
st.write(
"""
<style>
footer {
visibility: hidden;
}
</style>
""",
unsafe_allow_html=True,
)