| import gradio as gr |
| import json |
| import os |
|
|
| |
| DATA_FILE = "students.json" |
|
|
| def load_students(): |
| """Load students from JSON file""" |
| if os.path.exists(DATA_FILE): |
| try: |
| with open(DATA_FILE, 'r') as f: |
| return json.load(f) |
| except: |
| return {} |
| return {} |
|
|
| def save_students(students): |
| """Save students to JSON file""" |
| with open(DATA_FILE, 'w') as f: |
| json.dump(students, f, indent=2) |
|
|
| def add_student(uid, name, email, batch, mobile): |
| """Add a new student to the database""" |
| if not uid or not name or not email or not batch: |
| return "Error: UID, Name, Email, and Batch are required fields!", get_all_students_table() |
| |
| students = load_students() |
| |
| if uid in students: |
| return f"Error: Student with UID '{uid}' already exists!", get_all_students_table() |
| |
| students[uid] = { |
| 'UID': uid, |
| 'NAME': name, |
| 'EMAIL': email, |
| 'BATCH': batch, |
| 'MOBILE': mobile or 'N/A' |
| } |
| |
| save_students(students) |
| return f"Student '{name}' (UID: {uid}) added successfully!", get_all_students_table() |
|
|
| def delete_student(uid): |
| """Delete a student from the database""" |
| if not uid: |
| return "Error: Please enter a valid UID!", get_all_students_table() |
| |
| students = load_students() |
| |
| if uid not in students: |
| return f"Error: Student with UID '{uid}' does not exist!", get_all_students_table() |
| |
| name = students[uid]['NAME'] |
| del students[uid] |
| save_students(students) |
| |
| return f"Student '{name}' (UID: {uid}) deleted successfully!", get_all_students_table() |
|
|
| def update_student(uid, name, email, batch, mobile): |
| """Update an existing student's information""" |
| if not uid: |
| return "Error: Please enter a valid UID!", get_all_students_table() |
| |
| students = load_students() |
| |
| if uid not in students: |
| return f"Error: Student with UID '{uid}' does not exist!", get_all_students_table() |
| |
| |
| if name: |
| students[uid]['NAME'] = name |
| if email: |
| students[uid]['EMAIL'] = email |
| if batch: |
| students[uid]['BATCH'] = batch |
| if mobile: |
| students[uid]['MOBILE'] = mobile |
| |
| save_students(students) |
| return f"Student (UID: {uid}) updated successfully!", get_all_students_table() |
|
|
| def search_student(uid): |
| """Search for a student by UID""" |
| if not uid: |
| return "Please enter a UID to search" |
| |
| students = load_students() |
| |
| if uid not in students: |
| return f"No student found with UID: {uid}" |
| |
| student = students[uid] |
| return f""" |
| ### Student Found: |
| | Field | Value | |
| |-------|-------| |
| | **UID** | {student['UID']} | |
| | **Name** | {student['NAME']} | |
| | **Email** | {student['EMAIL']} | |
| | **Batch** | {student['BATCH']} | |
| | **Mobile** | {student['MOBILE']} | |
| """ |
|
|
| def get_all_students_table(): |
| """Get all students as a formatted table""" |
| students = load_students() |
| |
| if not students: |
| return "No students in the database yet. Add some students to get started!" |
| |
| |
| table = "| UID | Name | Email | Batch | Mobile |\n" |
| table += "|-----|------|-------|-------|--------|\n" |
| |
| for uid, student in students.items(): |
| table += f"| {student['UID']} | {student['NAME']} | {student['EMAIL']} | {student['BATCH']} | {student['MOBILE']} |\n" |
| |
| return table |
|
|
| def get_stats(): |
| """Get database statistics""" |
| students = load_students() |
| total = len(students) |
| |
| if total == 0: |
| return "**Statistics:** No students registered yet." |
| |
| |
| batches = {} |
| for student in students.values(): |
| batch = student.get('BATCH', 'Unknown') |
| batches[batch] = batches.get(batch, 0) + 1 |
| |
| batch_info = ", ".join([f"{k}: {v}" for k, v in batches.items()]) |
| |
| return f"**Statistics:** Total Students: {total} | Batches: {batch_info}" |
|
|
| def refresh_table(): |
| """Refresh the student table""" |
| return get_all_students_table(), get_stats() |
|
|
| |
| custom_css = """ |
| .gradio-container { |
| font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; |
| } |
| .gr-button-primary { |
| background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); |
| } |
| .gr-box { |
| border-radius: 10px; |
| } |
| """ |
|
|
| |
| with gr.Blocks(css=custom_css, title="Student Management System", theme=gr.themes.Soft()) as demo: |
| gr.Markdown(""" |
| # Student Management System |
| ### A simple and efficient system to manage student records |
| |
| *Originally built with Python & MongoDB, now running on Hugging Face Spaces!* |
| |
| --- |
| """) |
| |
| |
| stats_display = gr.Markdown(value=get_stats()) |
| |
| with gr.Tabs(): |
| |
| with gr.TabItem("View Students"): |
| gr.Markdown("### All Registered Students") |
| students_table = gr.Markdown(value=get_all_students_table()) |
| refresh_btn = gr.Button("Refresh", variant="secondary") |
| refresh_btn.click(fn=refresh_table, outputs=[students_table, stats_display]) |
| |
| |
| with gr.TabItem("Add Student"): |
| gr.Markdown("### Add a New Student") |
| gr.Markdown("*Fields marked with * are required*") |
| |
| with gr.Row(): |
| with gr.Column(): |
| add_uid = gr.Textbox(label="UID *", placeholder="Enter unique student ID") |
| add_name = gr.Textbox(label="Name *", placeholder="Enter student name") |
| add_email = gr.Textbox(label="Email *", placeholder="Enter email address") |
| |
| with gr.Column(): |
| add_batch = gr.Textbox(label="Batch *", placeholder="e.g., 2024, CS-A") |
| add_mobile = gr.Textbox(label="Mobile (Optional)", placeholder="Enter mobile number") |
| |
| add_btn = gr.Button("Add Student", variant="primary") |
| add_result = gr.Markdown() |
| add_table = gr.Markdown() |
| |
| add_btn.click( |
| fn=add_student, |
| inputs=[add_uid, add_name, add_email, add_batch, add_mobile], |
| outputs=[add_result, add_table] |
| ) |
| |
| |
| with gr.TabItem("Update Student"): |
| gr.Markdown("### Update Student Information") |
| gr.Markdown("*Enter the UID and only the fields you want to update*") |
| |
| with gr.Row(): |
| with gr.Column(): |
| update_uid = gr.Textbox(label="UID *", placeholder="Enter student UID to update") |
| update_name = gr.Textbox(label="New Name", placeholder="Leave empty to keep current") |
| update_email = gr.Textbox(label="New Email", placeholder="Leave empty to keep current") |
| |
| with gr.Column(): |
| update_batch = gr.Textbox(label="New Batch", placeholder="Leave empty to keep current") |
| update_mobile = gr.Textbox(label="New Mobile", placeholder="Leave empty to keep current") |
| |
| update_btn = gr.Button("Update Student", variant="primary") |
| update_result = gr.Markdown() |
| update_table = gr.Markdown() |
| |
| update_btn.click( |
| fn=update_student, |
| inputs=[update_uid, update_name, update_email, update_batch, update_mobile], |
| outputs=[update_result, update_table] |
| ) |
| |
| |
| with gr.TabItem("Delete Student"): |
| gr.Markdown("### Delete a Student Record") |
| gr.Markdown("*Warning: This action cannot be undone!*") |
| |
| delete_uid = gr.Textbox(label="UID", placeholder="Enter student UID to delete") |
| delete_btn = gr.Button("Delete Student", variant="stop") |
| delete_result = gr.Markdown() |
| delete_table = gr.Markdown() |
| |
| delete_btn.click( |
| fn=delete_student, |
| inputs=[delete_uid], |
| outputs=[delete_result, delete_table] |
| ) |
| |
| |
| with gr.TabItem("Search Student"): |
| gr.Markdown("### Search for a Student") |
| |
| search_uid = gr.Textbox(label="UID", placeholder="Enter student UID to search") |
| search_btn = gr.Button("Search", variant="primary") |
| search_result = gr.Markdown() |
| |
| search_btn.click( |
| fn=search_student, |
| inputs=[search_uid], |
| outputs=[search_result] |
| ) |
| |
| gr.Markdown(""" |
| --- |
| ### About |
| |
| This Student Management System was originally built with: |
| - **Python** & **Tkinter** for GUI |
| - **MongoDB** for database |
| |
| This web version uses **Gradio** and runs on **Hugging Face Spaces**. |
| |
| **Features:** |
| - Add new students |
| - View all students |
| - Update student information |
| - Delete students |
| - Search by UID |
| |
| [GitHub Repository](https://github.com/HarshShinde0/Student-Management-System) |
| """) |
|
|
| |
| if __name__ == "__main__": |
| demo.launch() |