File size: 2,713 Bytes
e4fe207
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# instructor.py
import streamlit as st
import pandas as pd
from app5_selectbox.database_con import cursor, db_connection
from app5_selectbox.app5_selectbox_func import display_table, generate_unique_4


# Function to fetch all instructors with their assigned program codes
def get_instructors_with_programs():
    cursor.execute("""
        SELECT instructor.inst_id, instructor.inst_name, program.prog_id, program.prog_code
        FROM instructor
        INNER JOIN program ON instructor.prog_id = program.prog_id
    """)
    data = cursor.fetchall()
    df = pd.DataFrame(data, columns=['Instructor ID', 'Instructor Name', 'Program ID', 'Program code'])
    return df

def instructor(table_name):
    inst_id = generate_unique_4(cursor,"inst_id", table_name)
    inst_name = st.text_input("Instructor Name", key="inst_name").upper()

    # Get the available programs from the 'program' table
    cursor.execute("SELECT prog_id, prog_name FROM program")
    programs = cursor.fetchall()
    program_names = [f"{prog_id} - {prog_name}" for (prog_id, prog_name) in programs]
    prog_id = st.selectbox("Program", program_names, key="prog_id")

    if st.button("Insert Instructor Record"):
        # Check if the inst_name and prog_id are provided
        if not inst_name or not prog_id:
            st.error("Instructor Name and Program are required. Please provide values for both fields.")
        else:
            # Extract the selected program_id from the program_names
            selected_program = programs[program_names.index(prog_id)]
            selected_prog_id = selected_program[0]

            # Check for duplicates in the 'instructor' table
            cursor.execute("SELECT inst_id FROM instructor WHERE inst_name = %s AND prog_id = %s", (inst_name, selected_prog_id))
            existing_instructor = cursor.fetchone()
            if existing_instructor:
                st.error("Instructor with the same name and program already exists.")
            else:
                try:
                    # Insert a record into the instructor table
                    cursor.execute("INSERT INTO instructor (inst_id, inst_name, prog_id) VALUES (%s, %s, %s)",
                                    (inst_id, inst_name, selected_prog_id))
                    db_connection.commit()
                    st.success("Record inserted successfully.")
                except Exception as e:
                    st.error(f"An error occurred: {str(e)}")
    # display_table(cursor, table_name)


    # Streamlit code
    st.title("Instructors with Assigned Program Codes")
    instructor_df = get_instructors_with_programs()
    st.dataframe(instructor_df.style.set_properties(**{'text-align': 'center'}))