-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathui.py
More file actions
55 lines (46 loc) · 1.73 KB
/
ui.py
File metadata and controls
55 lines (46 loc) · 1.73 KB
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
import streamlit as st
import requests
API_URL = "http://localhost:8000/todos/"
st.set_page_config(page_title="Todo App", layout="centered")
st.title("📝 Todo App (Streamlit UI)")
# --- Add new Todo ---
st.header("Add a new Todo")
with st.form("todo_form"):
title = st.text_input("Title")
description = st.text_input("Description")
submitted = st.form_submit_button("Create Todo")
if submitted:
res = requests.post(API_URL, json={
"title": title,
"description": description,
"completed": False
})
if res.status_code == 200:
st.success("✅ Todo created!")
st.experimental_rerun()
else:
st.error("❌ Failed to create todo.")
# --- Display Todos ---
st.header("Current Todos")
try:
todos = requests.get(API_URL).json()
if not todos:
st.info("No todos yet. Add one above!")
else:
for todo in todos:
col1, col2, col3 = st.columns([5, 1, 1])
status = "✅" if todo["completed"] else "❌"
col1.markdown(f"**{todo['title']}** — {todo['description']} {status}")
if not todo["completed"]:
if col2.button("✅", key=f"complete_{todo['id']}"):
requests.put(f"{API_URL}{todo['id']}", json={
"title": todo["title"],
"description": todo["description"],
"completed": True
})
st.experimental_rerun()
if col3.button("❌", key=f"delete_{todo['id']}"):
requests.delete(f"{API_URL}{todo['id']}")
st.experimental_rerun()
except Exception as e:
st.error(f"Error fetching todos: {e}")