import streamlit as st import requests import os BACKEND_URL = os.environ.get("BACKEND_URL", "http://localhost:8000") st.set_page_config(page_title="Notes", page_icon="📝") st.title("📝 Notes") # Add note with st.form("add_note", clear_on_submit=True): st.subheader("New Note") title = st.text_input("Title") content = st.text_area("Content", height=100) submitted = st.form_submit_button("Add Note", use_container_width=True) if submitted: if not title.strip(): st.error("Title is required.") else: try: r = requests.post( f"{BACKEND_URL}/notes", json={"title": title, "content": content}, timeout=5, ) if r.status_code == 200: st.success("Note added!") st.rerun() else: st.error(f"Backend error: {r.text}") except Exception as e: st.error(f"Could not reach backend: {e}") st.divider() st.subheader("All Notes") try: r = requests.get(f"{BACKEND_URL}/notes", timeout=5) notes = r.json() if not notes: st.info("No notes yet. Add one above.") else: for note in notes: with st.container(border=True): col1, col2 = st.columns([5, 1]) with col1: st.markdown(f"**{note['title']}**") if note.get("content"): st.write(note["content"]) st.caption(note["created_at"]) with col2: if st.button("Delete", key=f"del_{note['id']}"): requests.delete(f"{BACKEND_URL}/notes/{note['id']}", timeout=5) st.rerun() except Exception as e: st.error(f"Could not reach backend: {e}")