-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
215 lines (185 loc) · 9.12 KB
/
app.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
94
95
96
97
98
99
100
101
102
103
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import streamlit as st
from tinydb import TinyDB, Query
# Initialize the databases
db_users = TinyDB('database_users.json')
db_posts = TinyDB('database_posts.json')
# Function to create or get user info from the database
def get_user(username):
User = Query()
user_info = db_users.table('users').get(User.username == username)
return user_info
# Function to check if the username exists
def username_exists(username):
User = Query()
return db_users.table('users').contains(User.username == username)
# Function to register a new user
def register_user(username, password):
if not username_exists(username):
db_users.table('users').insert({'username': username, 'password': password, 'profile_pic': None, 'description': None})
return True
else:
return False
# Function to authenticate the user
def authenticate_user(username, password):
User = Query()
user_info = db_users.table('users').get((User.username == username) & (User.password == password))
return user_info
# Function to update user profile
def update_profile(username, profile_pic, description):
User = Query()
db_users.table('users').update({'profile_pic': profile_pic, 'description': description}, User.username == username)
# Function to add a new post
def add_post(username, image_url, title, description):
db_posts.table('posts').insert({'username': username, 'image_url': image_url, 'title': title, 'description': description, 'likes': 0, 'dislikes': 0, 'comments': []})
# Function to get all posts for the feed
def get_all_posts():
return db_posts.table('posts').all()
# Function to react to a comment (like or dislike)
def react_to_comment(post_id, comment_id, reaction_type):
posts = db_posts.table('posts')
post = posts.get(doc_id=post_id)
if post:
comments = post['comments']
if 0 <= comment_id < len(comments):
comment = comments[comment_id]
if reaction_type == 'likes':
comment['likes'] += 1
elif reaction_type == 'dislikes':
comment['dislikes'] += 1
posts.update({'comments': comments}, doc_ids=[post_id])
# Function to react to a post (like or dislike)
def react_to_post(post_id, reaction_type):
posts = db_posts.table('posts')
post = posts.get(doc_id=post_id)
if post:
if reaction_type == 'likes':
post['likes'] += 1
elif reaction_type == 'dislikes':
post['dislikes'] += 1
posts.update(post, doc_ids=[post_id])
# Streamlit App
def main():
st.title('FussionLink')
# Toggle for dark and light modes
dark_mode = st.checkbox('Dark Mode', key='dark_mode')
# Apply dark mode if selected
if dark_mode:
st.markdown(
"""
<style>
body {
color: white;
background-color: #222;
}
</style>
""",
unsafe_allow_html=True
)
# Logout Button in the sidebar
if st.sidebar.button('Logout'):
st.session_state.pop('user')
st.experimental_rerun()
page = st.sidebar.radio('Select a page', ['Login', 'Register', 'Feed', 'Profile', 'Add Post'])
if page == 'Login':
st.subheader('Login')
username = st.text_input('Username', value='')
password = st.text_input('Password', type='password', value='')
if st.button('Login'):
user_info = authenticate_user(username, password)
if user_info:
st.success(f'Welcome, {username}! You are logged in.')
st.session_state.user = user_info
st.experimental_rerun()
# Clear input values
st.session_state.sync()
else:
st.error('Invalid username or password')
elif page == 'Register':
st.subheader('Register')
new_username = st.text_input('Username', value='')
new_password = st.text_input('Password', type='password', value='')
if st.button('Register'):
if register_user(new_username, new_password):
st.success(f'Account created for {new_username}! You can now log in.')
st.session_state.user = get_user(new_username)
st.experimental_rerun()
# Clear input values
st.session_state.sync()
else:
st.error('Username already exists. Please choose another username.')
elif page == 'Feed':
st.subheader('Feed')
if 'user' in st.session_state:
posts = get_all_posts()
for post in posts:
st.image(post['image_url'], caption=post['title'])
st.write(f"Posted by: {post['username']}")
st.write(f"Likes: {post['likes']}")
st.write(f"Dislikes: {post['dislikes']}")
st.write(f"Description: {post['description']}")
st.write("Comments:")
for comment_id, comment in enumerate(post['comments']):
st.write(f"Comment {comment_id + 1}: {comment}")
like_button_key = f"like_comment_{post.doc_id}_{comment_id}"
dislike_button_key = f"dislike_comment_{post.doc_id}_{comment_id}"
if st.button(f"Like Comment {comment_id + 1}", key=like_button_key):
react_to_comment(post.doc_id, comment_id, 'likes')
if st.button(f"Dislike Comment {comment_id + 1}", key=dislike_button_key):
react_to_comment(post.doc_id, comment_id, 'dislikes')
st.text_area(f"Add a Comment for Post {post.doc_id}", key=f"comment_input_{post.doc_id}")
comment_button_key = f"add_comment_{post.doc_id}"
if st.button(f"Submit Comment for Post {post.doc_id}", key=comment_button_key):
new_comment = st.session_state[f"comment_input_{post.doc_id}"]
if new_comment:
post['comments'].append(new_comment)
db_posts.update({'comments': post['comments']}, doc_ids=[post.doc_id])
like_button_key = f"like_post_{post.doc_id}"
dislike_button_key = f"dislike_post_{post.doc_id}"
if st.button(f"Like Post {post.doc_id}", key=like_button_key):
react_to_post(post.doc_id, 'likes')
if st.button(f"Dislike Post {post.doc_id}", key=dislike_button_key):
react_to_post(post.doc_id, 'dislikes')
if 'user' in st.session_state and st.session_state.user['username'] == post['username']:
edit_button_key = f"edit_post_{post.doc_id}"
if st.button(f"Edit Post {post.doc_id}", key=edit_button_key):
# Add functionality for editing post
new_title = st.text_input('Edit Title', value=post['title'])
new_description = st.text_input('Edit Description', value=post['description'])
if st.button('Save Edit'):
# Implement function to update post
post['title'] = new_title
post['description'] = new_description
db_posts.update(post, doc_ids=[post.doc_id])
st.success("Post updated successfully!")
elif page == 'Add Post':
st.subheader('Add New Post')
new_image_url = st.text_input("Image URL", key="new_image_url")
new_title = st.text_input('Title', key="new_title")
new_description = st.text_input('Description', key="new_description")
if st.button('Post', key='post_button'):
if 'user' in st.session_state:
add_post(st.session_state.user['username'], new_image_url, new_title, new_description)
st.success("Post added successfully!")
else:
st.warning('Please log in to post.')
elif page == 'Profile':
st.subheader('Profile')
if 'user' in st.session_state:
user_info = st.session_state.user
st.write(f"Username: {user_info['username']}")
st.write(f"Description: {user_info['description']}")
# Check if profile_pic is not None before displaying
if user_info['profile_pic']:
st.image(user_info['profile_pic'], caption='Profile Picture')
else:
st.image("https://upload.wikimedia.org/wikipedia/commons/thumb/0/0f/Grosser_Panda.JPG/1280px-Grosser_Panda.JPG", caption='Default Profile Picture')
st.subheader("Edit Profile:")
new_profile_pic = st.text_input("New Profile Picture URL", key="new_profile_pic")
new_description = st.text_input('New Description', value=user_info['description'], key="new_description_profile")
if st.button('Update Profile', key='update_profile_button'):
update_profile(user_info['username'], new_profile_pic, new_description)
st.success("Profile updated successfully!")
else:
st.error('Please login or register.')
if __name__ == '__main__':
main()