Further UX improvements to streamlit app
Browse files
app.py
CHANGED
|
@@ -1,12 +1,14 @@
|
|
| 1 |
import streamlit as st
|
|
|
|
| 2 |
from postly.clients.postly_client import PostlyClient
|
| 3 |
|
| 4 |
# Initialize the PostlyClient in Streamlit's session state
|
| 5 |
-
if
|
| 6 |
st.session_state.client = PostlyClient()
|
| 7 |
|
| 8 |
client = st.session_state.client
|
| 9 |
|
|
|
|
| 10 |
def add_user():
|
| 11 |
st.title("Add User")
|
| 12 |
user_name = st.text_input("Enter user name")
|
|
@@ -14,6 +16,7 @@ def add_user():
|
|
| 14 |
client.add_user(user_name)
|
| 15 |
st.success(f"User '{user_name}' added successfully.")
|
| 16 |
|
|
|
|
| 17 |
def add_post():
|
| 18 |
st.title("Add Post")
|
| 19 |
users = client.get_users()
|
|
@@ -26,9 +29,11 @@ def add_post():
|
|
| 26 |
except Exception as e:
|
| 27 |
st.error(f"Error: {e}")
|
| 28 |
|
|
|
|
| 29 |
def delete_user():
|
| 30 |
st.title("Delete User")
|
| 31 |
-
|
|
|
|
| 32 |
if st.button("Delete User"):
|
| 33 |
try:
|
| 34 |
client.delete_user(user_name)
|
|
@@ -36,9 +41,11 @@ def delete_user():
|
|
| 36 |
except KeyError as e:
|
| 37 |
st.error(f"Error: {e}")
|
| 38 |
|
|
|
|
| 39 |
def get_posts_for_user():
|
| 40 |
st.title("Get Posts for User")
|
| 41 |
-
|
|
|
|
| 42 |
if st.button("Get Posts"):
|
| 43 |
try:
|
| 44 |
posts = client.get_posts_for_user(user_name)
|
|
@@ -48,19 +55,25 @@ def get_posts_for_user():
|
|
| 48 |
except KeyError as e:
|
| 49 |
st.error(f"Error: {e}")
|
| 50 |
|
|
|
|
| 51 |
def get_posts_for_topic():
|
| 52 |
st.title("Get Posts for Topic")
|
| 53 |
-
|
|
|
|
| 54 |
if st.button("Get Posts"):
|
| 55 |
posts = client.get_posts_for_topic(topic)
|
| 56 |
st.write(f"Posts for topic '{topic}':")
|
| 57 |
for post in posts:
|
| 58 |
st.write(post)
|
| 59 |
|
|
|
|
| 60 |
def get_trending_topics():
|
| 61 |
st.title("Get Trending Topics")
|
|
|
|
| 62 |
from_timestamp = st.number_input("Enter from timestamp", min_value=0, step=1)
|
| 63 |
-
to_timestamp = st.number_input(
|
|
|
|
|
|
|
| 64 |
if st.button("Get Trending Topics"):
|
| 65 |
try:
|
| 66 |
topics = client.get_trending_topics(int(from_timestamp), int(to_timestamp))
|
|
@@ -69,6 +82,8 @@ def get_trending_topics():
|
|
| 69 |
st.write(topic)
|
| 70 |
except ValueError as e:
|
| 71 |
st.error(f"Error: {e}")
|
|
|
|
|
|
|
| 72 |
|
| 73 |
def get_all_posts():
|
| 74 |
st.title("All Posts")
|
|
@@ -83,9 +98,22 @@ def get_all_posts():
|
|
| 83 |
st.markdown(f"{post.content}")
|
| 84 |
st.markdown("---")
|
| 85 |
|
|
|
|
| 86 |
def main():
|
| 87 |
st.sidebar.title("Postly\nSimple social media platform")
|
| 88 |
-
page = st.sidebar.selectbox(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
if page == "Add User":
|
| 91 |
add_user()
|
|
@@ -102,5 +130,6 @@ def main():
|
|
| 102 |
elif page == "View All Posts":
|
| 103 |
get_all_posts()
|
| 104 |
|
|
|
|
| 105 |
if __name__ == "__main__":
|
| 106 |
main()
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
|
| 3 |
from postly.clients.postly_client import PostlyClient
|
| 4 |
|
| 5 |
# Initialize the PostlyClient in Streamlit's session state
|
| 6 |
+
if "client" not in st.session_state:
|
| 7 |
st.session_state.client = PostlyClient()
|
| 8 |
|
| 9 |
client = st.session_state.client
|
| 10 |
|
| 11 |
+
|
| 12 |
def add_user():
|
| 13 |
st.title("Add User")
|
| 14 |
user_name = st.text_input("Enter user name")
|
|
|
|
| 16 |
client.add_user(user_name)
|
| 17 |
st.success(f"User '{user_name}' added successfully.")
|
| 18 |
|
| 19 |
+
|
| 20 |
def add_post():
|
| 21 |
st.title("Add Post")
|
| 22 |
users = client.get_users()
|
|
|
|
| 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)
|
|
|
|
| 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()
|
| 48 |
+
user_name = st.selectbox("Select user name", users)
|
| 49 |
if st.button("Get Posts"):
|
| 50 |
try:
|
| 51 |
posts = client.get_posts_for_user(user_name)
|
|
|
|
| 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()
|
| 62 |
+
topic = st.selectbox("Enter topic", topics)
|
| 63 |
if st.button("Get Posts"):
|
| 64 |
posts = client.get_posts_for_topic(topic)
|
| 65 |
st.write(f"Posts for topic '{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()
|
| 73 |
from_timestamp = st.number_input("Enter from timestamp", min_value=0, step=1)
|
| 74 |
+
to_timestamp = st.number_input(
|
| 75 |
+
"Enter to timestamp", min_value=0, max_value=current_timestamp, step=1, value=current_timestamp
|
| 76 |
+
)
|
| 77 |
if st.button("Get Trending Topics"):
|
| 78 |
try:
|
| 79 |
topics = client.get_trending_topics(int(from_timestamp), int(to_timestamp))
|
|
|
|
| 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 |
st.markdown(f"{post.content}")
|
| 99 |
st.markdown("---")
|
| 100 |
|
| 101 |
+
|
| 102 |
def main():
|
| 103 |
st.sidebar.title("Postly\nSimple social media platform")
|
| 104 |
+
page = st.sidebar.selectbox(
|
| 105 |
+
"Choose an action",
|
| 106 |
+
[
|
| 107 |
+
"Add User",
|
| 108 |
+
"Add Post",
|
| 109 |
+
"Delete User",
|
| 110 |
+
"Get Posts for User",
|
| 111 |
+
"Get Posts for Topic",
|
| 112 |
+
"Get Trending Topics",
|
| 113 |
+
"View All Posts",
|
| 114 |
+
],
|
| 115 |
+
index=6,
|
| 116 |
+
)
|
| 117 |
|
| 118 |
if page == "Add User":
|
| 119 |
add_user()
|
|
|
|
| 130 |
elif page == "View All Posts":
|
| 131 |
get_all_posts()
|
| 132 |
|
| 133 |
+
|
| 134 |
if __name__ == "__main__":
|
| 135 |
main()
|