File size: 3,947 Bytes
5d5d8c6
 
 
 
 
 
 
 
a6b3728
5d5d8c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
113
114
115
116
117
118
119
120
121
122
123
124
125
import streamlit as st
import pandas as pd
import numpy as np
from cryptography.fernet import Fernet
import os
import io
from dotenv import load_dotenv


# Load environment variables from .env file
load_dotenv()

# Set page configuration
st.set_page_config(page_title="Student Grade Lookup", page_icon="๐Ÿ“š", layout="centered")

# Custom CSS to improve the app's appearance
st.markdown("""

<style>

    .reportview-container {

        background: #f0f2f6

    }

    .big-font {

        font-size:20px !important;

        font-weight: bold;

    }

    .stAlert > div {

        padding-top: 15px;

        padding-bottom: 15px;

    }

</style>

""", unsafe_allow_html=True)

# Load and decrypt the CSV file
@st.cache_data
def load_data():
    encryption_key = os.getenv('ENCRYPTION_KEY')
    if not encryption_key:
        st.error("Encryption key not found in environment variables.")
        return pd.DataFrame()

    f = Fernet(encryption_key.encode())
    try:
        with open('encrypted_grades.csv', 'rb') as file:
            encrypted_data = file.read()
        decrypted_data = f.decrypt(encrypted_data)
        return pd.read_csv(io.StringIO(decrypted_data.decode()))
    except Exception as e:
        st.error(f"Error decrypting file: {str(e)}")
        return pd.DataFrame()

# Main function to run the Streamlit app
def main():
    st.title('๐Ÿ“š Student Grade Lookup')
    st.markdown("---")

    # Load the data
    df = load_data()

    # Create two columns
    col1, col2 = st.columns([2, 1])

    with col1:
        # Create an input field for the student ID
        student_id = st.text_input('Enter Student ID:', placeholder="e.g., C21101100")

    with col2:
        st.write("")
        st.write("")
        search_button = st.button('Search', use_container_width=True)

    if student_id and search_button:
        # Search for the student in the dataframe
        student = df[df['ID'].astype(str) == student_id]

        if not student.empty:
            st.markdown("---")
            st.subheader('Student Information:')
            
            # Display student information in a more structured way
            col1, col2 = st.columns(2)
            
            with col1:
                st.markdown("<p class='big-font'>Name</p>", unsafe_allow_html=True)
                st.write(f"{student['NAME'].values[0]}")
                
                st.markdown("<p class='big-font'>ID</p>", unsafe_allow_html=True)
                st.write(f"{student_id}")

            with col2:
                st.markdown("<p class='big-font'>Grade</p>", unsafe_allow_html=True)
                # Check if grade is NA
                grade = student['GRADE'].values[0]
                if pd.isna(grade):
                    grade_display = "N/A"
                else:
                    grade_display = f"{grade:.2f}"
                st.write(grade_display)
                
                st.markdown("<p class='big-font'>Remarks</p>", unsafe_allow_html=True)
                remarks = student['REMARKS'].values[0]
                if remarks == "Passed":
                    st.success(remarks)
                elif remarks == "Conditional":
                    st.warning(remarks)
                else:
                    st.write(remarks)
        else:
            st.error('Student ID not found. Please try again.')

    # Display some statistics about the class
    st.markdown("---")
    st.subheader("Class Statistics")
    col1, col2, col3 = st.columns(3)
    with col1:
        st.metric("Total Students", len(df))
    with col2:
        avg_grade = df['GRADE'].mean()
        st.metric("Average Grade", f"{avg_grade:.2f}")
    with col3:
        passing_rate = (df['REMARKS'] == 'Passed').mean() * 100
        st.metric("Passing Rate", f"{passing_rate:.1f}%")

if __name__ == '__main__':
    main()