-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathadmin_user.py
More file actions
63 lines (56 loc) · 2.07 KB
/
admin_user.py
File metadata and controls
63 lines (56 loc) · 2.07 KB
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
import streamlit as st
import pandas as pd
from sql_connection import mysql_connect
def callback():
mycursor = st.session_state.mycursor_state
db = st.session_state.db_state
edited_rows = st.session_state["data_editor"]["edited_rows"]
print(edited_rows.items())
rows_to_delete = []
for idx, value in edited_rows.items():
if value["Delete User"] is True:
id = st.session_state["data"].iloc[idx]["User id"]
print(int(id))
mycursor.execute(f"DELETE FROM regular_users WHERE user_id = %s",(int(id),))
db.commit()
rows_to_delete.append(idx)
st.session_state["data"] = (
st.session_state["data"].drop(rows_to_delete, axis=0).reset_index(drop=True)
)
def admin_user():
if 'db_state' not in st.session_state or 'mycursor_state' not in st.session_state:
mysql_connect()
mycursor = st.session_state.mycursor_state
db = st.session_state.db_state
if "data" not in st.session_state:
mycursor.execute("SELECT user_id, username FROM regular_users")
r = mycursor.fetchall()
users = [user for user in r]
st.session_state.data = pd.DataFrame(
data=users,
columns=["User id", "Username"]
)
if 'userid_delete' not in st.session_state:
st.session_state.userid_delete = None
columns = st.session_state["data"].columns
column_config = {column: st.column_config.Column(disabled=True) for column in columns}
modified_df = st.session_state["data"].copy()
modified_df["Delete User"] = False
modified_df = modified_df[modified_df.columns.tolist()]
with st.sidebar:
if st.button("Logout"):
st.session_state.page='app'
st.session_state.loginbtns = True
st.rerun()
st.header("Admin Dashboard")
st.subheader("Users Table")
st.data_editor(
modified_df,
key="data_editor",
on_change=callback,
hide_index=True,
column_config=column_config,
width=500
)
if __name__ == "__main__":
admin_user()