Implemented simple streamlit app for testing postly
Browse files
app.py
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 'client' not in st.session_state:
|
| 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")
|
| 13 |
+
if st.button("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()
|
| 20 |
+
user_name = st.selectbox("Select user name", users)
|
| 21 |
+
post_text = st.text_area("Enter post text")
|
| 22 |
+
if st.button("Add Post"):
|
| 23 |
+
try:
|
| 24 |
+
client.add_post(user_name, post_text)
|
| 25 |
+
st.success("Post added successfully.")
|
| 26 |
+
except Exception as e:
|
| 27 |
+
st.error(f"Error: {e}")
|
| 28 |
+
|
| 29 |
+
def delete_user():
|
| 30 |
+
st.title("Delete User")
|
| 31 |
+
user_name = st.text_input("Enter user name")
|
| 32 |
+
if st.button("Delete User"):
|
| 33 |
+
try:
|
| 34 |
+
client.delete_user(user_name)
|
| 35 |
+
st.success(f"User '{user_name}' deleted successfully.")
|
| 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 |
+
user_name = st.text_input("Enter user name")
|
| 42 |
+
if st.button("Get Posts"):
|
| 43 |
+
try:
|
| 44 |
+
posts = client.get_posts_for_user(user_name)
|
| 45 |
+
st.write(f"Posts for user '{user_name}':")
|
| 46 |
+
for post in posts:
|
| 47 |
+
st.write(post)
|
| 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 |
+
topic = st.text_input("Enter topic")
|
| 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("Enter to timestamp", min_value=0, step=1)
|
| 64 |
+
if st.button("Get Trending Topics"):
|
| 65 |
+
try:
|
| 66 |
+
topics = client.get_trending_topics(int(from_timestamp), int(to_timestamp))
|
| 67 |
+
st.write("Trending topics:")
|
| 68 |
+
for topic in 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")
|
| 75 |
+
posts = client.get_posts()
|
| 76 |
+
all_posts = []
|
| 77 |
+
for user_name, user_posts in posts.items():
|
| 78 |
+
for post in user_posts:
|
| 79 |
+
all_posts.append((user_name, post))
|
| 80 |
+
sorted_posts = sorted(all_posts, key=lambda x: x[1].timestamp)
|
| 81 |
+
for user_name, post in sorted_posts:
|
| 82 |
+
st.markdown(f"**{user_name}**")
|
| 83 |
+
st.markdown(f"{post.content}")
|
| 84 |
+
st.markdown("---")
|
| 85 |
+
|
| 86 |
+
def main():
|
| 87 |
+
st.sidebar.title("Simple Twitter Clone")
|
| 88 |
+
page = st.sidebar.selectbox("Choose an action", ["Add User", "Add Post", "Delete User", "Get Posts for User", "Get Posts for Topic", "Get Trending Topics", "View All Posts"])
|
| 89 |
+
|
| 90 |
+
if page == "Add User":
|
| 91 |
+
add_user()
|
| 92 |
+
elif page == "Add Post":
|
| 93 |
+
add_post()
|
| 94 |
+
elif page == "Delete User":
|
| 95 |
+
delete_user()
|
| 96 |
+
elif page == "Get Posts for User":
|
| 97 |
+
get_posts_for_user()
|
| 98 |
+
elif page == "Get Posts for Topic":
|
| 99 |
+
get_posts_for_topic()
|
| 100 |
+
elif page == "Get Trending Topics":
|
| 101 |
+
get_trending_topics()
|
| 102 |
+
elif page == "View All Posts":
|
| 103 |
+
get_all_posts()
|
| 104 |
+
|
| 105 |
+
if __name__ == "__main__":
|
| 106 |
+
main()
|