CoolFace
Apppublic

pedhapati/Enrollmentui

sourceHugging Faceupdated 11mo agoView on Hugging Face
0likes
streamlit_app.py207 linesDownload Raw Back to src
1import streamlit as st2import requests3import json4 5# Page configuration6st.set_page_config(7    page_title="Customer Management System",8    page_icon="๐Ÿ‘ฅ",9    layout="wide"10)11 12# API base URL13API_BASE_URL = "https://pedhapati-enrollmentapi.hf.space"       14 15# Helper function to make API calls16def make_api_call(method, endpoint, data=None):17    try:18        url = f"{API_BASE_URL}{endpoint}"19        if method == "GET":20            response = requests.get(url)21        elif method == "POST":22            response = requests.post(url, json=data)23        elif method == "PUT":24            response = requests.put(url, json=data)25        elif method == "DELETE":26            response = requests.delete(url)27        28        return response29    except requests.exceptions.ConnectionError:30        st.error("โŒ Cannot connect to API. Make sure FastAPI server is running on http://localhost:8000")31        return None32 33# Main title34st.title("๐Ÿ‘ฅ Customer Relationship Management System")35st.markdown("---")36 37# Sidebar for navigation38st.sidebar.title("Navigation")39page = st.sidebar.selectbox("Choose an action", [40    "View All Customers", 41    "Create New Customer", 42    "Update Customer", 43    "Delete Customer"44])45 46# View All Customers Page47if page == "View All Customers":48    st.header("๐Ÿ“‹ All Customers")49    50    if st.button("๐Ÿ”„ Refresh Customer List", type="primary"):51        response = make_api_call("GET", "/customers")52        if response and response.status_code == 200:53            customers = response.json()54            if customers:55                st.success(f"Found {len(customers)} customers")56                57                # Display customers in a nice format58                for i, customer in enumerate(customers, 1):59                    with st.expander(f"Customer {i}: {customer['name']} (ID: {customer['id']})"):60                        col1, col2 = st.columns(2)61                        with col1:62                            st.write(f"**Name:** {customer['name']}")63                            st.write(f"**Email:** {customer['email']}")64                        with col2:65                            st.write(f"**Phone:** {customer['phone'] or 'Not provided'}")66                            st.write(f"**Address:** {customer['address'] or 'Not provided'}")67            else:68                st.info("No customers found. Create your first customer!")69        else:70            st.error("Failed to fetch customers")71 72# Create New Customer Page73elif page == "Create New Customer":74    st.header("โž• Create New Customer")75    76    with st.form("create_customer_form"):77        col1, col2 = st.columns(2)78        79        with col1:80            name = st.text_input("Customer Name *", placeholder="Enter full name")81            email = st.text_input("Email Address *", placeholder="customer@example.com")82        83        with col2:84            phone = st.text_input("Phone Number", placeholder="+1 (555) 123-4567")85            address = st.text_area("Address", placeholder="Enter full address")86        87        submitted = st.form_submit_button("Create Customer", type="primary")88        89        if submitted:90            if name and email:91                # Get current customer count for ID generation92                response = make_api_call("GET", "/customers")93                if response and response.status_code == 200:94                    customer_count = len(response.json())95                    new_id = customer_count + 196                else:97                    new_id = 198                99                customer_data = {100                    "id": new_id,101                    "name": name,102                    "email": email,103                    "phone": phone if phone else None,104                    "address": address if address else None105                }106                107                response = make_api_call("POST", "/customers", customer_data)108                if response and response.status_code == 200:109                    st.success("โœ… Customer created successfully!")110                    st.json(response.json())111                else:112                    st.error("โŒ Failed to create customer")113            else:114                st.warning("โš ๏ธ Please fill in at least Name and Email fields")115 116# Update Customer Page117elif page == "Update Customer":118    st.header("โœ๏ธ Update Customer")119    120    # First, get the list of customers121    response = make_api_call("GET", "/customers")122    if response and response.status_code == 200:123        customers = response.json()124        125        if customers:126            # Create a dropdown to select customer127            customer_options = {f"{c['name']} (ID: {c['id']})": c for c in customers}128            selected_customer_display = st.selectbox("Select Customer to Update", list(customer_options.keys()))129            130            if selected_customer_display:131                selected_customer = customer_options[selected_customer_display]132                133                with st.form("update_customer_form"):134                    st.write("**Current Customer Information:**")135                    col1, col2 = st.columns(2)136                    137                    with col1:138                        new_name = st.text_input("Name", value=selected_customer['name'])139                        new_email = st.text_input("Email", value=selected_customer['email'])140                    141                    with col2:142                        new_phone = st.text_input("Phone", value=selected_customer['phone'] or "")143                        new_address = st.text_area("Address", value=selected_customer['address'] or "")144                    145                    submitted = st.form_submit_button("Update Customer", type="primary")146                    147                    if submitted:148                        if new_name and new_email:149                            updated_customer = {150                                "id": selected_customer['id'],151                                "name": new_name,152                                "email": new_email,153                                "phone": new_phone if new_phone else None,154                                "address": new_address if new_address else None155                            }156                            157                            response = make_api_call("PUT", f"/customers/{selected_customer['id']}", updated_customer)158                            if response and response.status_code == 200:159                                st.success("โœ… Customer updated successfully!")160                                st.json(response.json())161                            else:162                                st.error("โŒ Failed to update customer")163                        else:164                            st.warning("โš ๏ธ Please fill in at least Name and Email fields")165        else:166            st.info("No customers found. Create a customer first!")167    else:168        st.error("Failed to fetch customers")169 170# Delete Customer Page171elif page == "Delete Customer":172    st.header("๐Ÿ—‘๏ธ Delete Customer")173    174    # First, get the list of customers175    response = make_api_call("GET", "/customers")176    if response and response.status_code == 200:177        customers = response.json()178        179        if customers:180            # Create a dropdown to select customer181            customer_options = {f"{c['name']} (ID: {c['id']})": c for c in customers}182            selected_customer_display = st.selectbox("Select Customer to Delete", list(customer_options.keys()))183            184            if selected_customer_display:185                selected_customer = customer_options[selected_customer_display]186                187                st.warning("โš ๏ธ This action cannot be undone!")188                st.write(f"**Customer to delete:** {selected_customer['name']} (ID: {selected_customer['id']})")189                190                col1, col2 = st.columns([1, 1])191                with col1:192                    if st.button("๐Ÿ—‘๏ธ Delete Customer", type="primary"):193                        response = make_api_call("DELETE", f"/customers/{selected_customer['id']}")194                        if response and response.status_code == 200:195                            st.success("โœ… Customer deleted successfully!")196                            st.json(response.json())197                        else:198                            st.error("โŒ Failed to delete customer")199                200                with col2:201                    if st.button("โŒ Cancel"):202                        st.rerun()203        else:204            st.info("No customers found. Create a customer first!")205    else:206        st.error("Failed to fetch customers")207