Spaces:
Sleeping
Sleeping
File size: 1,136 Bytes
01032e6 76b0c65 01032e6 76b0c65 01032e6 76b0c65 01032e6 76b0c65 01032e6 76b0c65 01032e6 76b0c65 01032e6 76b0c65 01032e6 76b0c65 01032e6 76b0c65 01032e6 76b0c65 01032e6 |
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 |
import streamlit as st
import json
import os
# --- Paths ---
COMMENTS_FILE = "data/comments.json"
os.makedirs("data", exist_ok=True)
# --- Load existing comments ---
if os.path.exists(COMMENTS_FILE):
with open(COMMENTS_FILE, "r") as f:
comments = json.load(f)
else:
comments = []
# --- Comments Section ---
st.subheader("Comments")
if comments:
for c in comments:
st.write(f"**{c['name']}**: {c['content']}")
else:
st.write("_No comments yet_")
# --- Add a new comment ---
with st.form("comment_form", clear_on_submit=True):
user_name = st.text_input("Your name", "")
user_content = st.text_area("Content", height=80)
submitted = st.form_submit_button("Submit")
if submitted and user_content.strip():
# --- Create new comment ---
new_comment = {
"name": user_name or "Anonymous",
"content": user_content
}
# --- Append and save to JSON ---
comments.append(new_comment)
with open(COMMENTS_FILE, "w") as f:
json.dump(comments, f, indent=2) # THIS line writes the JSON
st.success("Saved!")
|