A modern simplistic Tech Blog
Drop files here
or click to upload
import streamlit as st
from datetime import datetime
from sqlalchemy import create_engine, Column, Integer, String, Text, DateTime
from sqlalchemy.orm import Session, DeclarativeBase
import pandas as pd
# Database setup
class Base(DeclarativeBase):
pass
class BlogPost(Base):
__tablename__ = 'posts'
id = Column(Integer, primary_key=True)
title = Column(String(200), nullable=False)
content = Column(Text, nullable=False)
author = Column(String(100), nullable=False)
created_at = Column(DateTime, default=datetime.utcnow)
engine = create_engine('sqlite:///blog.sqlite')
Base.metadata.create_all(bind=engine)
# Page Config
st.set_page_config(
page_title="Modern Tech Blog",
page_icon="💻",
layout="centered"
)
# Title and Description
st.title("💻 Modern Tech Blog")
st.markdown("*A place for tech enthusiasts to share their thoughts*")
# Sidebar
st.sidebar.title("Navigation")
page = st.sidebar.radio("Go to", ["Read Posts", "Create Post"])
if page == "Read Posts":
st.header("Latest Posts")
with Session(engine) as session:
posts = session.query(BlogPost).order_by(BlogPost.created_at.desc()).all()
if not posts:
st.info("No posts yet. Be the first to create one!")
for post in posts:
with st.container():
st.subheader(post.title)
st.caption(f"By {post.author} | {post.created_at.strftime('%B %d, %Y')}")
st.markdown("---")
st.write(post.content)
st.markdown("---")
elif page == "Create Post":
st.header("Create New Post")
with st.form("new_post", clear_on_submit=True):
title = st.text_input("Title")
author = st.text_input("Author")
content = st.text_area("Content")
submit = st.form_submit_button("Publish")
if submit:
if title and author and content:
with Session(engine) as session:
new_post = BlogPost(
title=title,
author=author,
content=content
)
session.add(new_post)
session.commit()
st.success("Post published successfully!")
st.balloons()
else:
st.error("Please fill in all fields")
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?