import streamlit as st
import sqlite3
# Function to initialize the database and tables
def init_db():
conn = [Link]('course_management.db')
c = [Link]()
# Create tables (courses, students)
[Link]('''
CREATE TABLE IF NOT EXISTS courses (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
description TEXT
)
''')
[Link]('''
CREATE TABLE IF NOT EXISTS students (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
email TEXT,
phone TEXT,
course_id INTEGER,
FOREIGN KEY (course_id) REFERENCES courses(id)
)
''')
[Link]()
[Link]()
# Function to add or update a course
def add_or_update_course(name, description, course_id=None):
conn = [Link]('course_management.db')
c = [Link]()
if course_id:
[Link]("UPDATE courses SET name = ?, description = ? WHERE id = ?",
(name, description, course_id))
else:
[Link](
"INSERT INTO courses (name, description) VALUES (?, ?)", (name,
description))
[Link]()
[Link]()
# Function to view all courses and the number of students joined each course
def view_courses():
conn = [Link]('course_management.db')
c = [Link]()
[Link]("SELECT [Link], [Link], [Link] FROM courses")
courses = [Link]()
[Link]()
return courses
# Function to delete a course
def delete_course(course_id):
conn = [Link]('course_management.db')
c = [Link]()
[Link]("DELETE FROM courses WHERE id = ?", (course_id,))
[Link]()
[Link]()
# Function to add or update a student
def add_or_update_student(name, email, phone, course_id, student_id=None):
conn = [Link]('course_management.db')
c = [Link]()
if student_id:
[Link]("UPDATE students SET name = ?, email = ?, phone = ?, course_id
= ? WHERE id = ?",
(name, email, phone, course_id, student_id))
else:
[Link]("INSERT INTO students (name, email, phone, course_id) VALUES
(?, ?, ?, ?)",
(name, email, phone, course_id))
[Link]()
[Link]()
# Function to view all students
def view_students():
conn = [Link]('course_management.db')
c = [Link]()
[Link]("SELECT * FROM students")
students = [Link]()
[Link]()
return students
# Function to delete a student
def delete_student(student_id):
conn = [Link]('course_management.db')
c = [Link]()
[Link]("DELETE FROM students WHERE id = ?", (student_id,))
[Link]()
[Link]()
# Streamlit app
[Link]("Online Course Management System")
# Initialize the database
init_db()
# Sidebar for selecting user type (Student or Teacher) - On the left side
user_type = [Link]("Login as", ("Teacher", "Student"))
# Display CRUD options based on the selected user type - On the right side
if user_type == "Student":
# [Link]( "Student operations: Update profile, View courses, Join a course,
Leave a course")
# Allow students to update their profile
[Link]("Student Profile")
student_name = st.text_input("Your Name")
student_email = st.text_input("Your Email")
student_phone = st.text_input("Your Phone")
student_course_id = st.text_input("Your Course ID")
if [Link]("Save"):
# Update student details
# Assuming students don't have a specific course yet
add_or_update_student(student_name, student_email,
student_phone, student_course_id)
[Link]("Profile Saved Successfully!")
# Display existing courses
[Link]("Available Courses")
courses = view_courses()
if courses:
[Link]("Courses:")
course_data = [(course[0], course[1], course[2]) for course in courses]
[Link](course_data)
# Allow students to join a course
[Link]("Join a Course:")
selected_course_id = st.text_input("Enter Course ID to join")
if [Link]("Join Course"):
add_or_update_student(None, None, None, selected_course_id)
[Link]("You have successfully joined the course!")
else:
[Link]("No courses available.")
# Allow students to leave a course
[Link]("Leave a Course")
[Link]("Leave a Course:")
course_id_to_leave = st.text_input("Enter Course ID to leave")
if [Link]("Leave Course"):
# Update student's course to None
add_or_update_student(None, None, None, None, course_id_to_leave)
[Link]("You have left the course.")
elif user_type == "Teacher":
#[Link]("Teacher operations: Add/Update/View/Delete courses")
# Teacher sidebar options
teacher_options = ["Manage Courses", "Manage Students"]
selected_option = [Link]("Select an option", teacher_options)
if selected_option == "Manage Courses":
# [Link]("Course operations: Add/Update/View/Delete courses")
# Sidebar for adding, updating, viewing, or deleting a course
[Link]("Course Actions")
action = [Link]("Select an action", [
"Add Course", "Update Course", "View
Courses", "Delete Course"])
if action == "Add Course":
course_name = [Link].text_input("Course Name")
course_description = [Link].text_area("Course Description")
if [Link](action):
add_or_update_course(course_name, course_description)
[Link]("Course added successfully!")
elif action == "Update Course":
course_id = [Link]("Select Course ID to update", [
course[2] for course in
view_courses()])
course_name = [Link].text_input("New Course Name")
course_description = [Link].text_area("New Course Description")
if [Link](action):
add_or_update_course(
course_name, course_description, course_id)
[Link]("Course updated successfully!")
elif action == "View Courses":
# Display existing courses
[Link]("Available Courses")
courses = view_courses()
if courses:
[Link]("Courses:")
course_data = [(course[0], course[1], course[2])
for course in courses]
[Link](course_data)
else:
[Link]("No courses available.")
elif action == "Delete Course":
course_id = [Link]("Select Course ID to delete", [
course[2] for course in
view_courses()])
if [Link](action):
delete_course(course_id)
[Link]("Course deleted successfully!")
elif selected_option == "Manage Students":
# [Link]("Student operations: Add/Update/View/Delete students")
# Sidebar for adding, updating, viewing, or deleting a student
[Link]("Teacher Actions")
student_name = [Link].text_input("Student Name")
student_email = [Link].text_input("Student Email")
student_phone = [Link].text_input("Student Phone")
student_course_id = [Link].text_input("Course ID")
action = [Link]("Select an action", [
"Add Student", "Update Student", "View
Students", "Delete Student"])
if action == "Add Student":
if [Link](action):
add_or_update_student(
student_name, student_email, student_phone, student_course_id)
[Link]("Student added successfully!")
elif action == "Update Student":
student_id = [Link]("Select Student ID to update", [
student[0] for student in
view_students()])
if [Link](action):
add_or_update_student(
student_name, student_email, student_phone, student_course_id,
student_id)
[Link]("Student updated successfully!")
elif action == "View Students":
# Display existing students
[Link]("Available Students")
students = view_students()
if students:
student_data = [
(student[1], student[2], student[3], student[4]) for student in
students]
[Link]("Students:")
[Link](student_data)
else:
[Link]("No students available.")
elif action == "Delete Student":
student_id = [Link]("Select Student ID to delete", [
student[0] for student in
view_students()])
if [Link](action):
delete_student(student_id)
[Link]("Student deleted successfully!")