forked from amankhandelwaal/Python_mini_project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
categories.py
93 lines (67 loc) · 2.95 KB
/
categories.py
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
import sqlite3
import streamlit as st
def add_category(conn,username):
cursor = conn.cursor()
st.header("Add Category")
new_category = st.text_input("New Category")
default_color = "#000000"
new_color = st.color_picker("Color", value=default_color)
add_category_button = st.button("Add Category")
success_message_shown = False
if add_category_button and new_category and new_color:
# Use a separate table for each user
category_table_name = f"categories_{username}"
# Create the table if it doesn't exist
cursor.execute(f'''CREATE TABLE IF NOT EXISTS {category_table_name}
(name TEXT PRIMARY KEY, color TEXT)''')
cursor.execute(f"INSERT OR REPLACE INTO {category_table_name} (name, color) VALUES (?, ?)", (new_category, new_color))
conn.commit()
success_message_shown = True
if success_message_shown:
st.success(f"Category '{new_category}' added or updated successfully!")
st.rerun()
def display_categories(conn, username):
cursor = conn.cursor()
st.header("Existing Categories")
# Use a separate table for each user
category_table_name = f"categories_{username}"
categories = cursor.execute(f"SELECT name, color FROM {category_table_name}").fetchall()
buttons_html = []
for category, color in categories:
button_html = (
f'<button style="background-color: {color}; color: white; padding: 10px; margin: 5px; border: none; border-radius: 5px;">'
f'{category}'
'</button>'
)
buttons_html.append(button_html)
st.markdown(" ".join(buttons_html), unsafe_allow_html=True)
def delete_category(conn, username):
cursor = conn.cursor()
st.header("Delete Category")
delete_category = st.text_input("Delete Category")
delete_category_button = st.button("Delete Category")
if delete_category_button and delete_category:
# Use a separate table for each user
category_table_name = f"categories_{username}"
cursor.execute(f"DELETE FROM {category_table_name} WHERE name=?", (delete_category,))
conn.commit()
st.success(f"Category '{delete_category}' deleted successfully!")
def main():
# Get the current username from session_state
username = st.session_state.username
# Check if the username is available
if username is None:
st.warning("Please log in to access this page.")
return
conn = sqlite3.connect("expense_db.db")
# You might not need this table creation here
# Use a separate table for each user
category_table_name = f"categories_{username}"
conn.execute(f'''CREATE TABLE IF NOT EXISTS {category_table_name}
(name TEXT PRIMARY KEY, color TEXT)''')
conn.commit()
add_category(conn, username)
display_categories(conn, username)
delete_category(conn, username)
if __name__ == "__main__":
main()