# streamlit_health_dashboard.py
import msoffcrypto
import streamlit as st
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
from io import BytesIO
import os
# Create a session state variable for login status
# Initialize session state variables
if "authenticated" not in st.session_state:
st.session_state.authenticated = False
# Function to log out
def logout():
st.session_state.authenticated = False
st.rerun()
# If not authenticated, show login form
if not st.session_state.authenticated:
with st.form("login_form"):
password_input = st.text_input("Enter password to access the dashboard:", type="password")
submitted = st.form_submit_button("Login")
if submitted:
if password_input == os.environ.get("password"):
st.session_state.authenticated = True
st.success("Login successful!")
st.rerun()
else:
st.error("Incorrect password. Please try again.")
st.stop()
else:
# Show logout button at the top after login
st.sidebar.button("🚪 Logout", on_click=logout)
local_excel_path = "all.xlsx"
if os.path.exists(local_excel_path):
#st.success(f"File found locally")
try :
with open(local_excel_path, "rb") as f:
office_file = msoffcrypto.OfficeFile(f)
office_file.load_key(password=os.environ.get('password')) # Provide the password
decrypted = BytesIO()
office_file.decrypt(decrypted)
df = pd.read_excel(decrypted)
df.columns = [str(col).strip() for col in df.columns]
except :
df = pd.read_excel(local_excel_path)
df.columns = [str(col).strip() for col in df.columns]
# df = pd.read_excel(local_excel_path)
# Load dataset
else :
uploaded_file = st.file_uploader("Upload your Excel file", type=["xlsx"])
if uploaded_file is not None:
try :
# with open(uploaded_file, "rb") as f:
office_file = msoffcrypto.OfficeFile(uploaded_file)
office_file.load_key(password="Kemo3689") # Provide the password
decrypted = BytesIO()
office_file.decrypt(decrypted)
df = pd.read_excel(decrypted)
df.columns = [str(col).strip() for col in df.columns]
except :
df = pd.read_excel(uploaded_file)
df.columns = [str(col).strip() for col in df.columns]
# Data Cleaning
numeric_cols = ['Cholesterol', 'Triglycerides', 'HbA1c', 'UricAcid', 'Creatinine', 'SGOT(AST)', 'SGPT(ALT)']
for col in numeric_cols:
df[col] = pd.to_numeric(df[col], errors='coerce')
# Sidebar: Year Filter
if 'year' in df.columns:
available_years = sorted(df['year'].dropna().unique())
selected_year = st.sidebar.selectbox("Select Year", available_years)
df = df[df['year'] == selected_year]
dashboard_year = selected_year
else:
dashboard_year = 2024 # default if no year column exists
# Sidebar: Page selection
page = st.sidebar.selectbox("Select Page", ["Main Overview", "Detailed Analysis", "Advanced Analytics"])
# Sidebar: Thresholds
st.sidebar.header("Threshold Settings 🎯")
uric_acid_thresh = st.sidebar.slider("Uric Acid Threshold", 5.0, 10.0, 7.0)
creatinine_thresh = st.sidebar.slider("Creatinine Threshold", 0.8, 2.0, 1.2)
sgot_thresh = st.sidebar.slider("SGOT(AST) Threshold", 30, 100, 50)
sgpt_thresh = st.sidebar.slider("SGPT(ALT) Threshold", 30, 100, 50)
# Sidebar: Filters
st.sidebar.header("Apply Filters 🔎")
show_high_cholesterol = st.sidebar.checkbox("Show High Cholesterol Only (>220)")
show_high_tgs = st.sidebar.checkbox("Show High TGs Only (>150)")
show_high_hba1c = st.sidebar.checkbox("Show High HbA1c Only (>6.4)")
show_pre_diabetes = st.sidebar.checkbox("Show Pre-Diabetes (5.7-6.4)")
# Sidebar: Theme
theme = st.sidebar.radio("Choose Theme 🌗", ["Light", "Dark"])
if theme == "Dark":
st.markdown("""
""", unsafe_allow_html=True)
else:
st.markdown("""
""", unsafe_allow_html=True)
# Apply Filters
filtered_df = df.copy()
if show_high_cholesterol:
filtered_df = filtered_df[filtered_df['Cholesterol'] > 220]
if show_high_tgs:
filtered_df = filtered_df[filtered_df['Triglycerides'] > 150]
if show_high_hba1c:
filtered_df = filtered_df[filtered_df['HbA1c'] > 6.4]
if show_pre_diabetes:
filtered_df = filtered_df[(filtered_df['HbA1c'] >= 5.7) & (filtered_df['HbA1c'] <= 6.4)]
### ----------------- Main Overview Page -------------------
if page == "Main Overview":
st.title(f"Annual Health Checkup Dashboard (Data Year: {dashboard_year})")
st.header("Key Performance Indicators 📈")
# Calculate safe KPIs (dropna before mean)
high_uric_acid = (filtered_df['UricAcid'].dropna() > uric_acid_thresh).mean() * 100
high_creatinine = (filtered_df['Creatinine'].dropna() > creatinine_thresh).mean() * 100
elevated_sgot = (filtered_df['SGOT(AST)'].dropna() > sgot_thresh).mean() * 100
elevated_sgpt = (filtered_df['SGPT(ALT)'].dropna() > sgpt_thresh).mean() * 100
high_cholesterol = (filtered_df['Cholesterol'].dropna() > 220).mean() * 100
high_tgs = (filtered_df['Triglycerides'].dropna() > 150).mean() * 100
high_hba1c = (filtered_df['HbA1c'].dropna() > 6.2).mean() * 100
col1, col2, col3 = st.columns(3)
col1.metric("High Uric Acid %", f"{high_uric_acid:.1f}%")
col2.metric("High Creatinine %", f"{high_creatinine:.1f}%")
col3.metric("Elevated SGOT/SGPT %", f"{(elevated_sgot + elevated_sgpt)/2:.1f}%")
col4, col5 = st.columns(2)
col4.metric("High Cholesterol %", f"{high_cholesterol:.1f}%")
col5.metric("High Triglycerides %", f"{high_tgs:.1f}%")
st.header("Distribution Charts 📊")
fig = px.histogram(filtered_df, x='Cholesterol', nbins=30, title='Cholesterol Distribution')
st.plotly_chart(fig)
fig = px.histogram(filtered_df, x='Triglycerides', nbins=30, title='Triglycerides Distribution')
st.plotly_chart(fig)
fig = px.histogram(filtered_df, x='HbA1c', nbins=30, title='HbA1c Distribution')
st.plotly_chart(fig)
st.header("Pie Charts for Elevated Metrics 🥧")
metrics = {
'Cholesterol >220': filtered_df['Cholesterol'] > 220,
'Triglycerides >150': filtered_df['Triglycerides'] > 150,
'HbA1c >6.2': filtered_df['HbA1c'] > 6.4,
'Pre-Diabetic' : filtered_df['HbA1c'].between(5.7, 6.4),
'Uric Acid': filtered_df['UricAcid'] > uric_acid_thresh,
'Creatinine': filtered_df['Creatinine'] > creatinine_thresh,
'SGOT(AST)': filtered_df['SGOT(AST)'] > sgot_thresh,
'SGPT(ALT)': filtered_df['SGPT(ALT)'] > sgpt_thresh
}
for metric_name, condition in metrics.items():
fig = px.pie(names=["Above Threshold", "Normal"], values=[condition.sum(), (~condition).sum()], title=metric_name)
st.plotly_chart(fig)
### ----------------- Detailed Analysis Page -------------------
elif page == "Detailed Analysis":
st.title(f"Detailed Analysis (Data Year: {dashboard_year})")
st.header("Correlation Heatmap 🔥")
corr = filtered_df[numeric_cols].corr()
fig, ax = plt.subplots(figsize=(10, 8))
sns.heatmap(corr, annot=True, cmap='coolwarm', ax=ax)
st.pyplot(fig)
st.header("Risk Summary Table 📋")
risk_summary = pd.DataFrame({
'Category': ['Very High Risk', 'High Chol & TGs', 'High Chol Only', 'High TGs Only', 'Normal'],
'Count': [
((filtered_df['Cholesterol'] > 300) | (filtered_df['Triglycerides'] > 400)).sum(),
((filtered_df['Cholesterol'] > 220) & (filtered_df['Triglycerides'] > 150)).sum(),
((filtered_df['Cholesterol'] > 220) & ~(filtered_df['Triglycerides'] > 150)).sum(),
((filtered_df['Triglycerides'] > 150) & ~(filtered_df['Cholesterol'] > 220)).sum(),
((filtered_df['Cholesterol'] <= 220) & (filtered_df['Triglycerides'] <= 150)).sum()
]
})
st.dataframe(risk_summary)
st.header("Scatter Plot: Cholesterol vs TGs")
risk_category = filtered_df.apply(
lambda row: "Very High Risk" if (row['Cholesterol'] > 300 or row['Triglycerides'] > 400)
else ("High Chol & High TGs" if (row['Cholesterol'] > 220 and row['Triglycerides'] > 150)
else ("High Chol Only" if row['Cholesterol'] > 220 else ("High TGs Only" if row['Triglycerides'] > 150 else "Normal"))),
axis=1
)
fig = px.scatter(
filtered_df,
x="Cholesterol",
y="Triglycerides",
color=risk_category,
trendline="ols",
hover_data=["HbA1c", "UricAcid"]
)
st.plotly_chart(fig)
st.subheader("Other Scatter Plots")
fig1 = px.scatter(filtered_df, x="HbA1c", y="Cholesterol", trendline="ols", title="HbA1c vs Cholesterol")
st.plotly_chart(fig1)
fig2 = px.scatter(filtered_df, x="UricAcid", y="Creatinine", trendline="ols", title="Uric Acid vs Creatinine")
st.plotly_chart(fig2)
fig3 = px.scatter(filtered_df, x="SGPT(ALT)", y="SGOT(AST)", trendline="ols", title="SGPT(ALT) vs SGOT(AST)")
st.plotly_chart(fig3)
### ----------------- Advanced Analytics Page -------------------
elif page == "Advanced Analytics":
st.title("🚀 Advanced Analytics")
st.info("Coming Soon: Predictive modeling for health risks!")