Update src/streamlit_app.py
Browse files- src/streamlit_app.py +169 -38
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,171 @@
|
|
| 1 |
-
import altair as alt
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
""
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
|
| 10 |
-
If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
|
| 11 |
-
forums](https://discuss.streamlit.io).
|
| 12 |
-
|
| 13 |
-
In the meantime, below is an example of what you can do with just a few lines of code:
|
| 14 |
-
"""
|
| 15 |
-
|
| 16 |
-
num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
|
| 17 |
-
num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
|
| 18 |
-
|
| 19 |
-
indices = np.linspace(0, 1, num_points)
|
| 20 |
-
theta = 2 * np.pi * num_turns * indices
|
| 21 |
-
radius = indices
|
| 22 |
-
|
| 23 |
-
x = radius * np.cos(theta)
|
| 24 |
-
y = radius * np.sin(theta)
|
| 25 |
-
|
| 26 |
-
df = pd.DataFrame({
|
| 27 |
-
"x": x,
|
| 28 |
-
"y": y,
|
| 29 |
-
"idx": indices,
|
| 30 |
-
"rand": np.random.randn(num_points),
|
| 31 |
-
})
|
| 32 |
-
|
| 33 |
-
st.altair_chart(alt.Chart(df, height=700, width=700)
|
| 34 |
-
.mark_point(filled=True)
|
| 35 |
-
.encode(
|
| 36 |
-
x=alt.X("x", axis=None),
|
| 37 |
-
y=alt.Y("y", axis=None),
|
| 38 |
-
color=alt.Color("idx", legend=None, scale=alt.Scale()),
|
| 39 |
-
size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
|
| 40 |
-
))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
import pandas as pd
|
| 4 |
+
from typing import Optional, Dict, Any
|
| 5 |
+
import json
|
| 6 |
+
|
| 7 |
+
# Constants
|
| 8 |
+
# API_URL = "http://localhost:8000" # Localhost api url
|
| 9 |
+
API_URL = "https://adwhytahumans-trial1b-crm-fastapi.hf.space" # Live huggingface space api link
|
| 10 |
+
COURSES = ["AI Filmmaking", "AI Music Composition", "AI Micro SaaS App Building"]
|
| 11 |
+
|
| 12 |
+
def fetch_all_learners() -> list:
|
| 13 |
+
"""Fetch all learners from the API"""
|
| 14 |
+
try:
|
| 15 |
+
response = requests.get(f"{API_URL}/learners")
|
| 16 |
+
response.raise_for_status()
|
| 17 |
+
return response.json()
|
| 18 |
+
except requests.exceptions.RequestException as e:
|
| 19 |
+
st.error(f"Error fetching learners: {str(e)}")
|
| 20 |
+
return []
|
| 21 |
+
|
| 22 |
+
def create_learner(learner_data: Dict[str, Any]) -> bool:
|
| 23 |
+
"""Create a new learner"""
|
| 24 |
+
try:
|
| 25 |
+
# Log the request data
|
| 26 |
+
st.write("Sending request with data:", learner_data)
|
| 27 |
+
response = requests.post(f"{API_URL}/learners", json=learner_data)
|
| 28 |
+
# Log the response
|
| 29 |
+
st.write("Response status:", response.status_code)
|
| 30 |
+
st.write("Response content:", response.text)
|
| 31 |
+
response.raise_for_status()
|
| 32 |
+
return True
|
| 33 |
+
except requests.exceptions.RequestException as e:
|
| 34 |
+
# Add detailed error logging
|
| 35 |
+
st.error(f"Error creating learner: {str(e)}")
|
| 36 |
+
if hasattr(e, 'response') and e.response is not None:
|
| 37 |
+
st.error(f"Response status code: {e.response.status_code}")
|
| 38 |
+
st.error(f"Response content: {e.response.text}")
|
| 39 |
+
return False
|
| 40 |
+
|
| 41 |
+
def update_learner(learner_id: int, learner_data: Dict[str, Any]) -> bool:
|
| 42 |
+
"""Update an existing learner"""
|
| 43 |
+
try:
|
| 44 |
+
response = requests.put(f"{API_URL}/learners/{learner_id}", json=learner_data)
|
| 45 |
+
response.raise_for_status()
|
| 46 |
+
return True
|
| 47 |
+
except requests.exceptions.RequestException as e:
|
| 48 |
+
st.error(f"Error updating learner: {str(e)}")
|
| 49 |
+
return False
|
| 50 |
+
|
| 51 |
+
def delete_learner(learner_id: int) -> bool:
|
| 52 |
+
"""Delete a learner"""
|
| 53 |
+
try:
|
| 54 |
+
response = requests.delete(f"{API_URL}/learners/{learner_id}")
|
| 55 |
+
response.raise_for_status()
|
| 56 |
+
return True
|
| 57 |
+
except requests.exceptions.RequestException as e:
|
| 58 |
+
st.error(f"Error deleting learner: {str(e)}")
|
| 59 |
+
return False
|
| 60 |
+
|
| 61 |
+
def learner_form(key: str, initial_data: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
|
| 62 |
+
"""Create a form for adding/editing learners"""
|
| 63 |
+
with st.form(key=key):
|
| 64 |
+
name = st.text_input("Name", value=initial_data.get("name", "") if initial_data else "")
|
| 65 |
+
email = st.text_input("Email", value=initial_data.get("email", "") if initial_data else "")
|
| 66 |
+
phone = st.text_input("Phone (optional)", value=initial_data.get("phone", "") if initial_data else "")
|
| 67 |
+
course = st.selectbox("Course", COURSES, index=COURSES.index(initial_data.get("course", COURSES[0])) if initial_data else 0)
|
| 68 |
+
|
| 69 |
+
submitted = st.form_submit_button("Submit")
|
| 70 |
+
if submitted:
|
| 71 |
+
return {
|
| 72 |
+
"name": name,
|
| 73 |
+
"email": email,
|
| 74 |
+
"phone": phone if phone else None,
|
| 75 |
+
"course": course
|
| 76 |
+
}
|
| 77 |
+
return None
|
| 78 |
+
|
| 79 |
+
def main():
|
| 80 |
+
st.title("100x Elite CRM")
|
| 81 |
+
|
| 82 |
+
# Initialize session state for managing popups
|
| 83 |
+
if "show_add_form" not in st.session_state:
|
| 84 |
+
st.session_state.show_add_form = False
|
| 85 |
+
if "show_edit_form" not in st.session_state:
|
| 86 |
+
st.session_state.show_edit_form = False
|
| 87 |
+
if "editing_learner" not in st.session_state:
|
| 88 |
+
st.session_state.editing_learner = None
|
| 89 |
+
if "show_delete_confirm" not in st.session_state:
|
| 90 |
+
st.session_state.show_delete_confirm = False
|
| 91 |
+
if "deleting_learner_id" not in st.session_state:
|
| 92 |
+
st.session_state.deleting_learner_id = None
|
| 93 |
+
|
| 94 |
+
# Add Learner button
|
| 95 |
+
if st.button("Add Learner"):
|
| 96 |
+
st.session_state.show_add_form = True
|
| 97 |
+
|
| 98 |
+
# Add Learner Form
|
| 99 |
+
if st.session_state.show_add_form:
|
| 100 |
+
st.subheader("Add New Learner")
|
| 101 |
+
learner_data = learner_form("add_learner")
|
| 102 |
+
if learner_data:
|
| 103 |
+
if create_learner(learner_data):
|
| 104 |
+
st.success("Learner added successfully!")
|
| 105 |
+
st.session_state.show_add_form = False
|
| 106 |
+
st.rerun()
|
| 107 |
+
|
| 108 |
+
# Edit Learner Form
|
| 109 |
+
if st.session_state.show_edit_form and st.session_state.editing_learner:
|
| 110 |
+
st.subheader("Edit Learner")
|
| 111 |
+
learner_data = learner_form("edit_learner", st.session_state.editing_learner)
|
| 112 |
+
if learner_data:
|
| 113 |
+
if update_learner(st.session_state.editing_learner["id"], learner_data):
|
| 114 |
+
st.success("Learner updated successfully!")
|
| 115 |
+
st.session_state.show_edit_form = False
|
| 116 |
+
st.session_state.editing_learner = None
|
| 117 |
+
st.rerun()
|
| 118 |
+
|
| 119 |
+
# Delete Confirmation
|
| 120 |
+
if st.session_state.show_delete_confirm and st.session_state.deleting_learner_id:
|
| 121 |
+
st.warning("Are you sure you want to delete this learner?")
|
| 122 |
+
col1, col2 = st.columns(2)
|
| 123 |
+
with col1:
|
| 124 |
+
if st.button("Yes, Delete"):
|
| 125 |
+
if delete_learner(st.session_state.deleting_learner_id):
|
| 126 |
+
st.success("Learner deleted successfully!")
|
| 127 |
+
st.session_state.show_delete_confirm = False
|
| 128 |
+
st.session_state.deleting_learner_id = None
|
| 129 |
+
st.rerun()
|
| 130 |
+
with col2:
|
| 131 |
+
if st.button("No, Cancel"):
|
| 132 |
+
st.session_state.show_delete_confirm = False
|
| 133 |
+
st.session_state.deleting_learner_id = None
|
| 134 |
+
st.rerun()
|
| 135 |
+
|
| 136 |
+
# Display Learners Table
|
| 137 |
+
st.subheader("Enrolled Learners")
|
| 138 |
+
learners = fetch_all_learners()
|
| 139 |
+
|
| 140 |
+
if not learners:
|
| 141 |
+
st.info("There are no learners enrolled.")
|
| 142 |
+
else:
|
| 143 |
+
# Create DataFrame for better display
|
| 144 |
+
df = pd.DataFrame(learners)
|
| 145 |
+
|
| 146 |
+
# Display the table with action buttons
|
| 147 |
+
for _, learner in df.iterrows():
|
| 148 |
+
col1, col2, col3, col4, col5 = st.columns([3, 3, 2, 2, 1])
|
| 149 |
+
with col1:
|
| 150 |
+
st.write(learner['name'])
|
| 151 |
+
with col2:
|
| 152 |
+
st.write(learner['email'])
|
| 153 |
+
with col3:
|
| 154 |
+
st.write(learner['phone'] if learner['phone'] else "N/A")
|
| 155 |
+
with col4:
|
| 156 |
+
st.write(learner['course'])
|
| 157 |
+
with col5:
|
| 158 |
+
col_edit, col_delete = st.columns(2)
|
| 159 |
+
with col_edit:
|
| 160 |
+
if st.button("✏️", key=f"edit_{learner['id']}"):
|
| 161 |
+
st.session_state.editing_learner = learner.to_dict()
|
| 162 |
+
st.session_state.show_edit_form = True
|
| 163 |
+
st.rerun()
|
| 164 |
+
with col_delete:
|
| 165 |
+
if st.button("🗑️", key=f"delete_{learner['id']}"):
|
| 166 |
+
st.session_state.deleting_learner_id = learner['id']
|
| 167 |
+
st.session_state.show_delete_confirm = True
|
| 168 |
+
st.rerun()
|
| 169 |
|
| 170 |
+
if __name__ == "__main__":
|
| 171 |
+
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|