-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
193 lines (178 loc) · 6.43 KB
/
app.py
File metadata and controls
193 lines (178 loc) · 6.43 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
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
# Import sqlite3 and pandas
import sqlite3
import pandas as pd
import streamlit as st
# Connect to the database and create table if not exists
def init_db():
conn = sqlite3.connect('blog.db')
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS posts (author TEXT NOT NULL, title TEXT NOT NULL, content TEXT NOT NULL, date DATE NOT NULL)''')
c.close()
conn.close()
# Define a function to add a new post
def add_post(author, title, content, date):
conn = None
c = None
try:
conn = sqlite3.connect('blog.db')
c = conn.cursor()
c.execute('INSERT INTO posts (author, title, content, date) VALUES (?,?,?,?)', (author, title, content, date))
conn.commit()
except sqlite3.Error as e:
print(e)
finally:
if c:
c.close()
if conn:
conn.close()
# Define a function to get all the posts
def get_all_posts():
conn = None
c = None
data = []
try:
conn = sqlite3.connect('blog.db')
c = conn.cursor()
c.execute('SELECT * FROM posts')
data = c.fetchall()
except sqlite3.Error as e:
print(e)
finally:
if c:
c.close()
if conn:
conn.close()
return data
# Define a function to get a post by title
def get_post_by_title(title):
conn = None
c = None
data = None
try:
conn = sqlite3.connect('blog.db')
c = conn.cursor()
c.execute('SELECT * FROM posts WHERE title=?', (title,))
data = c.fetchone()
except sqlite3.Error as e:
print(e)
finally:
if c:
c.close()
if conn:
conn.close()
return data
# Define a function to delete a post
def delete_post(title):
conn = None
c = None
try:
conn = sqlite3.connect('blog.db')
c = conn.cursor()
c.execute('DELETE FROM posts WHERE title=?', (title,))
conn.commit()
except sqlite3.Error as e:
print(e)
finally:
if c:
c.close()
if conn:
conn.close()
# Initialize the database
init_db()
# Streamlit app
# Define some HTML templates for displaying the posts
title_temp = """
<div style="background-color:#464e5f;padding:10px;border-radius:10px;margin:10px;">
<h4 style="color:white;text-align:center;">{}</h4>
<img src="https://www.w3schools.com/howto/img_avatar.png" alt="Avatar" style="vertical-align: middle;float:left;width: 50px;height: 50px;border-radius: 50%;">
<h6>Author: {}</h6>
<br/>
<br/>
<p style="text-align:justify"> {}</p>
</div>
"""
post_temp = """
<div style="background-color:#464e5f;padding:10px;border-radius:5px;margin:10px;">
<h4 style="color:white;text-align:center;">{}</h4>
<h6>Author: {}</h6>
<h6>Date: {}</h6>
<img src="https://www.w3schools.com/howto/img_avatar.png" alt="Avatar" style="vertical-align: middle;width: 50px;height: 50px;border-radius: 50%;">
<br/>
<br/>
<p style="text-align:justify"> {}</p>
</div>
"""
# Create a sidebar menu with different options
menu = ["Home", "View Posts", "Add Post", "Search", "Manage"]
choice = st.sidebar.selectbox("Menu", menu)
# Display the selected option
if choice == "Home":
st.title("Welcome to my blog")
st.write("This is a simple blog app built with streamlit and python.")
st.write("You can view, add, search, and manage posts using the sidebar menu.")
st.write("Enjoy!")
elif choice == "View Posts":
st.title("View Posts")
st.write("Here you can see all the posts in the blog.")
posts = get_all_posts()
for post in posts:
st.markdown(title_temp.format(post[1], post[0], post[2][:50] + "..."), unsafe_allow_html=True)
if st.button("Read More", key=post[1]):
st.markdown(post_temp.format(post[1], post[0], post[3], post[2]), unsafe_allow_html=True)
elif choice == "Add Post":
st.title("Add Post")
st.write("Here you can add a new post to the blog.")
with st.form(key="add_form"):
author = st.text_input("Author")
title = st.text_input("Title")
content = st.text_area("Content")
date = st.date_input("Date")
submit = st.form_submit_button("Submit")
if submit:
add_post(author, title, content, date)
st.success("Post added successfully")
elif choice == "Search":
st.title("Search")
st.write("Here you can search for a post by title or author.")
query = st.text_input("Enter your query")
if query:
posts = get_all_posts()
results = [post for post in posts if query.lower() in post[0].lower() or query.lower() in post[1].lower()]
if results:
st.write(f"Found {len(results)} matching posts:")
for result in results:
st.markdown(title_temp.format(result[1], result[0], result[2][:50] + "..."), unsafe_allow_html=True)
if st.button("Read More", key=result[1]):
st.markdown(post_temp.format(result[1], result[0], result[3], result[2]), unsafe_allow_html=True)
else:
st.write("No matching posts found")
elif choice == "Manage":
st.title("Manage")
st.write("Here you can delete posts or view some statistics.")
titles = [post[1] for post in get_all_posts()]
title = st.selectbox("Select a post to delete", titles)
if st.button("Delete"):
delete_post(title)
st.success("Post deleted successfully")
if st.checkbox("Show statistics"):
posts = get_all_posts()
df = pd.DataFrame(posts, columns=["author", "title", "content", "date"])
st.write("Number of posts:", len(posts))
st.write("Number of authors:", len(df["author"].unique()))
st.write("Most recent post:", df["date"].max())
st.write("Oldest post:", df["date"].min())
st.write("Posts by author:")
author_count = df["author"].value_counts()
st.bar_chart(author_count)
# Add initial test posts only if this script is run as the main module
if __name__ == "__main__":
# add_post('Jyoti', 'Django vs Flask', 'This post is about Django vs Flask', '2023-12-05')
# add_post('Jaimin', 'Artificial Intelligence', 'This post is about Artificial Intelligence', '2024-01-02')
# add_post('Jeevansh', 'TalkPython', 'This post is about TalkPython', '2024-01-23')
# print(get_all_posts())
# print(get_post_by_title('Artificial Intelligence'))
# delete_post('TalkPython')
# print(get_all_posts())
pass
# To run the Streamlit app, execute the following command in the terminal:
# streamlit run app.py