-
Notifications
You must be signed in to change notification settings - Fork 0
/
unstableCONNTRASTupdate
150 lines (127 loc) · 5.41 KB
/
unstableCONNTRASTupdate
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import streamlit as st
import sqlite3
# Uses SQLite as a database connection
conn = sqlite3.connect('assignments.db', check_same_thread=False)
cursor = conn.cursor()
# Creates a table to store assignments if it doesn't exist yet
cursor.execute('''
CREATE TABLE IF NOT EXISTS assignments (
id INTEGER PRIMARY KEY,
title TEXT,
description TEXT,
due_date DATE,
priority_level TEXT,
completed INTEGER DEFAULT 0
)
''')
# Alter the table to include the "priority_level" column
try:
cursor.execute('ALTER TABLE assignments ADD COLUMN priority_level TEXT')
cursor.execute('ALTER TABLE assignments ADD COLUMN completed INTEGER DEFAULT 0')
conn.commit()
except sqlite3.OperationalError as e:
print("Column already exists")
# Initialize session state variables
if "success_message_timer" not in st.session_state:
st.session_state.success_message_timer = 0
if "success_message" not in st.session_state:
st.session_state.success_message = ""
# Main function to insert assignments into the database
def insert_assignment(title, description, due_date, priority_level, completed):
try:
query = "INSERT INTO assignments (title, description, due_date, priority_level, completed) VALUES (?, ?, ?, ?, ?)"
params = (title, description, due_date, priority_level, completed)
cursor.execute(query, params)
conn.commit()
except Exception as e:
print(str(e))
# Function to fetch assignments from the database
def fetch_assignments():
cursor.execute("SELECT * FROM assignments ORDER BY due_date ASC")
return cursor.fetchall()
# Function to display assignments
def display_assignments(assignments):
# Displays the list of assignments
if len(assignments) > 0:
# Sort assignments by due date in ascending order
assignments.sort(key=lambda x: x[3])
# Display tasks in a matrix with three tasks side by side
for i in range(0, len(assignments), 3):
row_assignments = assignments[i:i + 3]
cols = st.columns(3)
for col, assignment in zip(cols, row_assignments):
title_style = f"color: {'#008000' if assignment[5] else 'white'}; font-weight: {'bold' if assignment[5] else 'normal'}"
col.markdown(
f"<p style='{title_style}'> {assignment[1]}</p>",
unsafe_allow_html=True
)
col.write(f"**Description:** {assignment[2]}")
col.write(f"**Due Date:** {assignment[3]}")
col.markdown(
f"**Priority Level:** <span style='background-color:{assignment[4]}; color:white; padding: 4px; border-radius: 4px;'> --- </span>",
unsafe_allow_html=True
)
if not assignment[5]: # Check if the assignment is not completed
completion_button = col.button(f"Mark as Completed {assignment[0]}", key=f"complete_{assignment[0]}")
if completion_button:
mark_assignment_completed(assignment[0])
st.success("Assignment marked as completed!")
col.write("-----------------------")
else:
st.info("No assignments available yet, add one now!")
# Function to mark an assignment as completed
def mark_assignment_completed(assignment_id):
try:
query = "UPDATE assignments SET completed = 1 WHERE id = ?"
params = (assignment_id,)
cursor.execute(query, params)
conn.commit()
except Exception as e:
print(str(e))
# Function to clear assignments
def clear_assignments():
cursor.execute("DELETE FROM assignments")
conn.commit()
# Streamlit app
st.title("Trakr ✒️")
tab1, tab2 = st.tabs(["Add", "Assignments"])
with tab1:
# Creates a form to add assignments
st.header("Add Assignment")
title = st.text_input("Title")
description = st.text_area("Description")
due_date = st.date_input("Due Date")
# Add priority selection using a selectbox with predefined colors
priority_level = st.selectbox("Select Priority Color", ["Red", "Orange", "Green"])
# Map the selected priority_level to corresponding color values
color_mapping = {
"Red": "#FF0000",
"Orange": "#FFA500",
"Green": "#008000"
}
if st.button("Add"):
if title and due_date:
selected_color = color_mapping.get(priority_level)
insert_assignment(title, description, due_date, selected_color, 0) # 'completed' initially set to 0
st.success("Assignment added successfully!")
# Button to clear assignments
if st.button("Clear Assignments."):
clear_assignments()
st.session_state.success_message = "All assignments have been cleared."
st.session_state.success_message_timer = 100 # 10 seconds
with tab2:
st.header("Assignments")
assignments = fetch_assignments()
# Button to clear assignments
if st.button("Clear Assignments"):
clear_assignments()
st.session_state.success_message = "All assignments have been cleared."
st.session_state.success_message_timer = 100 # 10 seconds
st.empty()
display_assignments(assignments)
# Fade away success message after a delay
if st.session_state.success_message_timer > 0:
st.session_state.success_message_timer -= 1
else:
st.session_state.success_message = ""
st.success(st.session_state.success_message)