Updated streamlit app to include user login, creation, and deletion
Browse files
app.py
CHANGED
|
@@ -1,5 +1,4 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
|
| 3 |
from postly.clients.postly_client import PostlyClient
|
| 4 |
|
| 5 |
# Initialize the PostlyClient in Streamlit's session state
|
|
@@ -8,40 +7,68 @@ if "client" not in st.session_state:
|
|
| 8 |
|
| 9 |
client = st.session_state.client
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
def
|
| 13 |
-
st.title("
|
| 14 |
user_name = st.text_input("Enter user name")
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
def add_post():
|
| 21 |
st.title("Add Post")
|
| 22 |
-
users = client.get_users()
|
| 23 |
-
user_name = st.selectbox("Select user name", users)
|
| 24 |
post_text = st.text_area("Enter post text")
|
| 25 |
if st.button("Add Post"):
|
| 26 |
try:
|
| 27 |
-
client.add_post(
|
| 28 |
st.success("Post added successfully.")
|
| 29 |
except Exception as e:
|
| 30 |
st.error(f"Error: {e}")
|
| 31 |
|
| 32 |
-
|
| 33 |
-
def delete_user():
|
| 34 |
-
st.title("Delete User")
|
| 35 |
-
users = client.get_users()
|
| 36 |
-
user_name = st.selectbox("Select user name", users)
|
| 37 |
-
if st.button("Delete User"):
|
| 38 |
-
try:
|
| 39 |
-
client.delete_user(user_name)
|
| 40 |
-
st.success(f"User '{user_name}' deleted successfully.")
|
| 41 |
-
except KeyError as e:
|
| 42 |
-
st.error(f"Error: {e}")
|
| 43 |
-
|
| 44 |
-
|
| 45 |
def get_posts_for_user():
|
| 46 |
st.title("Get Posts for User")
|
| 47 |
users = client.get_users()
|
|
@@ -55,7 +82,6 @@ def get_posts_for_user():
|
|
| 55 |
except KeyError as e:
|
| 56 |
st.error(f"Error: {e}")
|
| 57 |
|
| 58 |
-
|
| 59 |
def get_posts_for_topic():
|
| 60 |
st.title("Get Posts for Topic")
|
| 61 |
topics = client.get_topics()
|
|
@@ -66,7 +92,6 @@ def get_posts_for_topic():
|
|
| 66 |
for post in posts:
|
| 67 |
st.write(post)
|
| 68 |
|
| 69 |
-
|
| 70 |
def get_trending_topics():
|
| 71 |
st.title("Get Trending Topics")
|
| 72 |
current_timestamp = client.get_current_timestamp()
|
|
@@ -82,8 +107,6 @@ def get_trending_topics():
|
|
| 82 |
st.write(topic)
|
| 83 |
except ValueError as e:
|
| 84 |
st.error(f"Error: {e}")
|
| 85 |
-
st.rerun()
|
| 86 |
-
|
| 87 |
|
| 88 |
def get_all_posts():
|
| 89 |
st.title("All Posts")
|
|
@@ -98,38 +121,43 @@ def get_all_posts():
|
|
| 98 |
st.markdown(f"{post.content}")
|
| 99 |
st.markdown("---")
|
| 100 |
|
| 101 |
-
|
| 102 |
def main():
|
| 103 |
st.sidebar.title("Postly\nSimple social media platform")
|
| 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 |
if __name__ == "__main__":
|
| 135 |
main()
|
|
|
|
| 1 |
import streamlit as st
|
|
|
|
| 2 |
from postly.clients.postly_client import PostlyClient
|
| 3 |
|
| 4 |
# Initialize the PostlyClient in Streamlit's session state
|
|
|
|
| 7 |
|
| 8 |
client = st.session_state.client
|
| 9 |
|
| 10 |
+
# Initialize user session state
|
| 11 |
+
if "logged_in" not in st.session_state:
|
| 12 |
+
st.session_state.logged_in = False
|
| 13 |
+
if "current_user" not in st.session_state:
|
| 14 |
+
st.session_state.current_user = None
|
| 15 |
|
| 16 |
+
def register():
|
| 17 |
+
st.title("Register")
|
| 18 |
user_name = st.text_input("Enter user name")
|
| 19 |
+
password = st.text_input("Enter password", type="password")
|
| 20 |
+
if st.button("Register"):
|
| 21 |
+
if user_name and password:
|
| 22 |
+
try:
|
| 23 |
+
client.add_user(user_name, password)
|
| 24 |
+
st.session_state.logged_in = True
|
| 25 |
+
st.session_state.current_user = user_name
|
| 26 |
+
st.success(f"User '{user_name}' registered and logged in successfully.")
|
| 27 |
+
st.rerun()
|
| 28 |
+
except ValueError as e:
|
| 29 |
+
st.error(f"Error: {e}")
|
| 30 |
+
else:
|
| 31 |
+
st.error("Please enter both user name and password.")
|
| 32 |
+
|
| 33 |
+
def login():
|
| 34 |
+
st.title("Login")
|
| 35 |
+
user_name = st.text_input("Enter user name")
|
| 36 |
+
password = st.text_input("Enter password", type="password")
|
| 37 |
+
if st.button("Login"):
|
| 38 |
+
if client.authenticate_user(user_name, password):
|
| 39 |
+
st.session_state.logged_in = True
|
| 40 |
+
st.session_state.current_user = user_name
|
| 41 |
+
st.success(f"User '{user_name}' logged in successfully.")
|
| 42 |
+
st.rerun()
|
| 43 |
+
else:
|
| 44 |
+
st.error("Invalid user name or password.")
|
| 45 |
+
|
| 46 |
+
def logout():
|
| 47 |
+
st.session_state.logged_in = False
|
| 48 |
+
st.session_state.current_user = None
|
| 49 |
+
st.success("Logged out successfully.")
|
| 50 |
+
st.rerun()
|
| 51 |
+
|
| 52 |
+
def delete_own_user():
|
| 53 |
+
st.title("Delete Account")
|
| 54 |
+
if st.button("Delete Account"):
|
| 55 |
+
try:
|
| 56 |
+
client.delete_user(st.session_state.current_user)
|
| 57 |
+
st.success(f"User '{st.session_state.current_user}' deleted successfully.")
|
| 58 |
+
logout()
|
| 59 |
+
except KeyError as e:
|
| 60 |
+
st.error(f"Error: {e}")
|
| 61 |
|
| 62 |
def add_post():
|
| 63 |
st.title("Add Post")
|
|
|
|
|
|
|
| 64 |
post_text = st.text_area("Enter post text")
|
| 65 |
if st.button("Add Post"):
|
| 66 |
try:
|
| 67 |
+
client.add_post(st.session_state.current_user, post_text)
|
| 68 |
st.success("Post added successfully.")
|
| 69 |
except Exception as e:
|
| 70 |
st.error(f"Error: {e}")
|
| 71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
def get_posts_for_user():
|
| 73 |
st.title("Get Posts for User")
|
| 74 |
users = client.get_users()
|
|
|
|
| 82 |
except KeyError as e:
|
| 83 |
st.error(f"Error: {e}")
|
| 84 |
|
|
|
|
| 85 |
def get_posts_for_topic():
|
| 86 |
st.title("Get Posts for Topic")
|
| 87 |
topics = client.get_topics()
|
|
|
|
| 92 |
for post in posts:
|
| 93 |
st.write(post)
|
| 94 |
|
|
|
|
| 95 |
def get_trending_topics():
|
| 96 |
st.title("Get Trending Topics")
|
| 97 |
current_timestamp = client.get_current_timestamp()
|
|
|
|
| 107 |
st.write(topic)
|
| 108 |
except ValueError as e:
|
| 109 |
st.error(f"Error: {e}")
|
|
|
|
|
|
|
| 110 |
|
| 111 |
def get_all_posts():
|
| 112 |
st.title("All Posts")
|
|
|
|
| 121 |
st.markdown(f"{post.content}")
|
| 122 |
st.markdown("---")
|
| 123 |
|
|
|
|
| 124 |
def main():
|
| 125 |
st.sidebar.title("Postly\nSimple social media platform")
|
| 126 |
+
if st.session_state.logged_in:
|
| 127 |
+
st.sidebar.write(f"Logged in as: {st.session_state.current_user}")
|
| 128 |
+
if st.sidebar.button("Logout"):
|
| 129 |
+
logout()
|
| 130 |
+
page = st.sidebar.selectbox(
|
| 131 |
+
"Choose an action",
|
| 132 |
+
[
|
| 133 |
+
"Add Post",
|
| 134 |
+
"Delete Account",
|
| 135 |
+
"Get Posts for User",
|
| 136 |
+
"Get Posts for Topic",
|
| 137 |
+
"Get Trending Topics",
|
| 138 |
+
"View All Posts",
|
| 139 |
+
],
|
| 140 |
+
index=5,
|
| 141 |
+
)
|
| 142 |
+
|
| 143 |
+
if page == "Add Post":
|
| 144 |
+
add_post()
|
| 145 |
+
elif page == "Delete Account":
|
| 146 |
+
delete_own_user()
|
| 147 |
+
elif page == "Get Posts for User":
|
| 148 |
+
get_posts_for_user()
|
| 149 |
+
elif page == "Get Posts for Topic":
|
| 150 |
+
get_posts_for_topic()
|
| 151 |
+
elif page == "Get Trending Topics":
|
| 152 |
+
get_trending_topics()
|
| 153 |
+
elif page == "View All Posts":
|
| 154 |
+
get_all_posts()
|
| 155 |
+
else:
|
| 156 |
+
page = st.sidebar.selectbox("Choose an action", ["Login", "Register"], index=0)
|
| 157 |
+
if page == "Login":
|
| 158 |
+
login()
|
| 159 |
+
elif page == "Register":
|
| 160 |
+
register()
|
| 161 |
|
| 162 |
if __name__ == "__main__":
|
| 163 |
main()
|