-
Notifications
You must be signed in to change notification settings - Fork 4
/
blog.py
140 lines (126 loc) · 5.35 KB
/
blog.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
# Import streamlit and other libraries
import streamlit as st
import sqlite3
import pandas as pd
# Connect to the database
conn = sqlite3.connect('blog.db')
c = conn.cursor()
# Create a table if not exists
c.execute('CREATE TABLE IF NOT EXISTS posts (author TEXT, title TEXT, content TEXT, date DATE)')
# Define some functions for interacting with the database
def add_post(author, title, content, date):
c.execute('INSERT INTO posts (author, title, content, date) VALUES (?,?,?,?)', (author, title, content, date))
conn.commit()
def get_all_posts():
c.execute('SELECT * FROM posts')
data = c.fetchall()
return data
def get_post_by_title(title):
c.execute('SELECT * FROM posts WHERE title=?', (title,))
data = c.fetchone()
return data
def delete_post(title):
c.execute('DELETE FROM posts WHERE title=?', (title,))
conn.commit()
# 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.")
# Get all the posts from the database
posts = get_all_posts()
# Display each post as a card
for post in posts:
st.markdown(title_temp.format(post[1], post[0], post[2][:50] + "..."), unsafe_allow_html=True)
# Add a button to view the full post
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.")
# Create a form to get the post details
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 the form is submitted, add the post to the database
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.")
# Create a text input to get the search query
query = st.text_input("Enter your query")
# If the query is not empty, search for the matching posts
if query:
# Get all the posts from the database
posts = get_all_posts()
# Filter the posts by the query
results = [post for post in posts if query.lower() in post[0].lower() or query.lower() in post[1].lower()]
# Display the results
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)
# Add a button to view the full post
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.")
# Create a selectbox to choose a post to delete
titles = [post[1] for post in get_all_posts()]
title = st.selectbox("Select a post to delete", titles)
# Add a button to confirm the deletion
if st.button("Delete"):
delete_post(title)
st.success("Post deleted successfully")
# Create a checkbox to show some statistics
if st.checkbox("Show statistics"):
# Get all the posts from the database
posts = get_all_posts()
# Convert the posts to a dataframe
df = pd.DataFrame(posts, columns=["author", "title", "content", "date"])
# Display some basic statistics
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())
# Display a bar chart of posts by author
st.write("Posts by author:")
author_count = df["author"].value_counts()
st.bar_chart(author_count)