Spaces:
Sleeping
Sleeping
| 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 | |
| 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() | |