Spaces:
Sleeping
Sleeping
| 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!") | |