Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import pandas as pd | |
| import urllib.parse | |
| # ---------------- Page Config ---------------- | |
| st.set_page_config( | |
| page_title="Pharma Expiry Dashboard", | |
| page_icon="π", | |
| layout="wide" | |
| ) | |
| st.title("π Pharma Inventory Expiry Alert System") | |
| # ---------------- File Upload ---------------- | |
| uploaded_file = st.file_uploader( | |
| "π Choose your Datafile", | |
| type=["csv", "xlsx", "json"] | |
| ) | |
| if uploaded_file is None: | |
| st.info("β¬ Upload CSV / Excel / JSON file to continue") | |
| st.stop() | |
| # ---------------- Load Data ---------------- | |
| if uploaded_file.name.endswith(".csv"): | |
| data = pd.read_csv(uploaded_file) | |
| elif uploaded_file.name.endswith(".xlsx"): | |
| data = pd.read_excel(uploaded_file) | |
| else: | |
| data = pd.read_json(uploaded_file) | |
| # ---------------- Safe Date Conversion ---------------- | |
| data["Expiry_Date"] = pd.to_datetime(data["Expiry_Date"], errors="coerce") | |
| data["Purchase_Date"] = pd.to_datetime(data["Purchase_Date"], errors="coerce") | |
| today = pd.Timestamp.today() | |
| # ---------------- Sidebar Filters ---------------- | |
| st.sidebar.header("π Filters") | |
| category_filter = st.sidebar.multiselect( | |
| "Category", | |
| options=sorted(data["Category"].dropna().unique()) | |
| ) | |
| supplier_filter = st.sidebar.multiselect( | |
| "Supplier", | |
| options=sorted(data["Supplier"].dropna().unique()) | |
| ) | |
| expiry_filter = st.sidebar.selectbox( | |
| "Expiry Status", | |
| ["All", "Expired", "Expiring in 30 Days", "Safe"] | |
| ) | |
| # ---------------- Apply Filters ---------------- | |
| filtered_data = data.copy() | |
| if category_filter: | |
| filtered_data = filtered_data[filtered_data["Category"].isin(category_filter)] | |
| if supplier_filter: | |
| filtered_data = filtered_data[filtered_data["Supplier"].isin(supplier_filter)] | |
| if expiry_filter == "Expired": | |
| filtered_data = filtered_data[filtered_data["Expiry_Date"] < today] | |
| elif expiry_filter == "Expiring in 30 Days": | |
| filtered_data = filtered_data[ | |
| (filtered_data["Expiry_Date"] >= today) & | |
| (filtered_data["Expiry_Date"] <= today + pd.Timedelta(days=30)) | |
| ] | |
| elif expiry_filter == "Safe": | |
| filtered_data = filtered_data[filtered_data["Expiry_Date"] > today + pd.Timedelta(days=30)] | |
| # ---------------- KPI Metrics ---------------- | |
| expired_count = data[data["Expiry_Date"] < today].shape[0] | |
| expiring_count = data[ | |
| (data["Expiry_Date"] >= today) & | |
| (data["Expiry_Date"] <= today + pd.Timedelta(days=30)) | |
| ].shape[0] | |
| col1, col2, col3, col4 = st.columns(4) | |
| col1.metric("π§Ύ Total Medicines", data["Medicine_Name"].nunique()) | |
| col2.metric("π¦ Total Stock", data["Stock_Quantity"].sum()) | |
| col3.metric("π¨ Expired", expired_count) | |
| col4.metric("β Expiring (30 Days)", expiring_count) | |
| st.write(filtered_data) | |
| st.write(filtered_data['Category'].value_counts()) | |
| st.write(filtered_data['Supplier'].value_counts()) | |
| st.markdown(f"Resultant Medicine are {filtered_data['Category'].count()}") | |
| st.divider() | |
| st.subheader("π’ Notify Supplier") | |
| # Supplier selection | |
| selected_supplier = st.selectbox( | |
| "Select Supplier", | |
| sorted(filtered_data["Supplier"].dropna().unique()) | |
| ) | |
| # Contact inputs | |
| supplier_mobile = st.text_input("π± Supplier Mobile (with country code)", placeholder="91XXXXXXXXXX") | |
| # Data to send (expired + expiring) | |
| alert_data = filtered_data[ | |
| filtered_data["Expiry_Date"] <= today + pd.Timedelta(days=30) | |
| ] | |
| def generate_message(df, supplier): | |
| message = f"Expiry Alert for Supplier: {supplier}\n\n" | |
| # Details of each medicine | |
| for _, row in df.iterrows(): | |
| message += ( | |
| f"Medicine: {row['Medicine_Name']}\n" | |
| f"Batch: {row['Batch_No']}\n" | |
| f"Expiry: {row['Expiry_Date'].date()}\n" | |
| f"Stock: {row['Stock_Quantity']}\n" | |
| "----------------------\n" | |
| ) | |
| # π’ Totals | |
| total_expiry_medicines = len(df) | |
| unique_medicine_count = df['Medicine_Name'].unique() | |
| category_count = df['Category'].unique() | |
| message += ( | |
| "\n===== SUMMARY =====\n" | |
| f"Total Expiry Medicines: {total_expiry_medicines}\n" | |
| f"Total Unique Medicine Names: {unique_medicine_count}\n" | |
| f"Total Categories Affected: {category_count}\n" | |
| ) | |
| return message | |
| if st.button("π² Submit Number"): | |
| if supplier_mobile and not alert_data.empty: | |
| message = generate_message(alert_data, selected_supplier) | |
| encoded_msg = urllib.parse.quote(message) | |
| whatsapp_url = f"https://wa.me/{supplier_mobile}?text={encoded_msg}" | |
| # st.button(whatsapp_url) | |
| st.link_button("Send Order WhatsApp", whatsapp_url) | |
| else: | |
| st.error("β Please provide mobile number & ensure data exists") | |