Balaprime commited on
Commit
33972db
·
verified ·
1 Parent(s): 685f1b5

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +37 -66
src/streamlit_app.py CHANGED
@@ -1,78 +1,49 @@
1
  import streamlit as st
2
  import pandas as pd
3
  import sqlite3
 
4
  from database import initialize_database
5
 
6
- # Initialize DB on start
7
  initialize_database()
8
 
9
- # Function to fetch filtered students
10
- def get_students(department, year):
11
- conn = sqlite3.connect("college.db")
12
- query = "SELECT * FROM student WHERE 1=1"
13
- if department != "All":
14
- query += f" AND major = '{department}'"
15
- if year != "All":
16
- query += f" AND year_of_enrollment = {year}"
17
- df = pd.read_sql_query(query, conn)
18
- conn.close()
19
- return df
20
 
21
- # Function to fetch filtered courses
22
- def get_courses(department):
23
- conn = sqlite3.connect("college.db")
24
- query = "SELECT * FROM course"
25
- if department != "All":
26
- query += f" WHERE department = '{department}'"
27
- df = pd.read_sql_query(query, conn)
28
- conn.close()
29
- return df
30
 
31
- # Prepare filter options
32
- departments = [
33
- "All",
34
- "Computer Science",
35
- "Mechanical Engineering",
36
- "ECE",
37
- "Civil",
38
- "IT",
39
- "CSE",
40
- "AI"
41
- ]
42
 
43
- def get_years():
44
- conn = sqlite3.connect("college.db")
45
- df = pd.read_sql_query("SELECT DISTINCT year_of_enrollment FROM student ORDER BY year_of_enrollment", conn)
 
46
  conn.close()
47
- years = df["year_of_enrollment"].astype(str).tolist()
48
- years.insert(0, "All")
49
- return years
50
-
51
- # Streamlit app UI
52
-
53
- st.title("College Database Dashboard")
54
-
55
- tab1, tab2 = st.tabs(["Students", "Courses"])
56
-
57
- with tab1:
58
- st.header("Student Records")
59
- dept_filter = st.selectbox("Select Department", departments)
60
- year_filter = st.selectbox("Select Year of Enrollment", get_years())
61
-
62
- if st.button("Show Students"):
63
- students_df = get_students(dept_filter, year_filter)
64
- if students_df.empty:
65
- st.info("No students found with the selected filters.")
66
- else:
67
- st.dataframe(students_df)
68
 
69
- with tab2:
70
- st.header("Course Records")
71
- course_dept_filter = st.selectbox("Select Department", departments, key="course_dept")
72
-
73
- if st.button("Show Courses"):
74
- courses_df = get_courses(course_dept_filter)
75
- if courses_df.empty:
76
- st.info("No courses found with the selected department.")
77
- else:
78
- st.dataframe(courses_df)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
  import pandas as pd
3
  import sqlite3
4
+ import os
5
  from database import initialize_database
6
 
7
+ # Initialize DB
8
  initialize_database()
9
 
10
+ # Path
11
+ db_path = os.path.join(os.path.dirname(__file__), "../college.db")
 
 
 
 
 
 
 
 
 
12
 
13
+ # Title
14
+ st.title("📊 College Dashboard")
 
 
 
 
 
 
 
15
 
16
+ # Sidebar filter
17
+ table = st.sidebar.selectbox("Select Table", ["student", "employee", "course"])
 
 
 
 
 
 
 
 
 
18
 
19
+ # Load data
20
+ def load_data(table):
21
+ conn = sqlite3.connect(db_path)
22
+ df = pd.read_sql_query(f"SELECT * FROM {table}", conn)
23
  conn.close()
24
+ return df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
+ # Filter logic
27
+ def filter_data(df):
28
+ col = st.sidebar.selectbox("Select column to filter", df.columns)
29
+ unique_values = df[col].unique().tolist()
30
+ selected = st.sidebar.multiselect(f"Filter {col}", unique_values, default=unique_values)
31
+ return df[df[col].isin(selected)]
32
+
33
+ # Display
34
+ df = load_data(table)
35
+ st.subheader(f"{table.capitalize()} Table")
36
+ filtered_df = filter_data(df)
37
+ st.dataframe(filtered_df)
38
+
39
+ # Show SQL Query Results
40
+ st.markdown("### 🔍 Run Custom SQL Query")
41
+ sql_query = st.text_area("Enter your SQL Query below:")
42
+ if st.button("Execute Query"):
43
+ try:
44
+ conn = sqlite3.connect(db_path)
45
+ result_df = pd.read_sql_query(sql_query, conn)
46
+ st.dataframe(result_df)
47
+ conn.close()
48
+ except Exception as e:
49
+ st.error(f"Error: {e}")