import streamlit as st import pandas as pd import numpy as np import matplotlib.pyplot as plt import seaborn as sns import plotly.express as px from datetime import datetime, timedelta # Set page config st.set_page_config(page_title="Healthcare Dashboard", layout="wide") # Load sample data (replace this with your actual data loading logic) @st.cache_data def load_data(): # Generate sample data np.random.seed(42) dates = pd.date_range(start="2022-01-01", end="2023-04-30", freq="D") departments = ["Cardiology", "Neurology", "Oncology", "Pediatrics", "Emergency"] diagnoses = ["Hypertension", "Stroke", "Cancer", "Diabetes", "Pneumonia", "Fracture", "Appendicitis", "Asthma", "Heart Disease", "Influenza"] data = { "Date": np.random.choice(dates, 1000), "Patient ID": np.arange(1, 1001), "Age": np.random.randint(1, 100, 1000), "Gender": np.random.choice(["Male", "Female"], 1000), "Department": np.random.choice(departments, 1000), "Diagnosis": np.random.choice(diagnoses, 1000), "Length of Stay": np.random.randint(1, 30, 1000) } return pd.DataFrame(data) df = load_data() # Title and introduction st.title("Healthcare Dashboard") st.write("Welcome to the healthcare dashboard. This interactive tool provides insights into patient data, hospital admissions, and key healthcare metrics.") # Sidebar st.sidebar.header("Filters") department = st.sidebar.multiselect("Select Department", options=df["Department"].unique(), default=df["Department"].unique()) date_range = st.sidebar.date_input("Select Date Range", value=(df["Date"].min().date(), df["Date"].max().date())) # Convert date_range to datetime start_date = datetime.combine(date_range[0], datetime.min.time()) end_date = datetime.combine(date_range[1], datetime.max.time()) # Filter data based on user selection filtered_df = df[(df["Department"].isin(department)) & (df["Date"] >= start_date) & (df["Date"] <= end_date)] # Main content col1, col2 = st.columns(2) # Patient Demographics with col1: st.subheader("Patient Demographics") fig_gender = px.pie(filtered_df, names="Gender", title="Gender Distribution") st.plotly_chart(fig_gender, use_container_width=True) # Hospital Admissions Over Time with col2: st.subheader("Hospital Admissions Over Time") admissions_over_time = filtered_df.groupby("Date").size().reset_index(name="Admissions") fig_admissions = px.line(admissions_over_time, x="Date", y="Admissions", title="Daily Hospital Admissions") st.plotly_chart(fig_admissions, use_container_width=True) # Top 10 Diagnoses st.subheader("Top 10 Diagnoses") top_diagnoses = filtered_df["Diagnosis"].value_counts().nlargest(10) fig_diagnoses = px.bar(top_diagnoses, x=top_diagnoses.index, y=top_diagnoses.values, title="Top 10 Diagnoses") st.plotly_chart(fig_diagnoses, use_container_width=True) # Average Length of Stay by Department st.subheader("Average Length of Stay by Department") avg_los = filtered_df.groupby("Department")["Length of Stay"].mean().sort_values(ascending=True) fig_los = px.bar(avg_los, x=avg_los.values, y=avg_los.index, orientation="h", title="Average Length of Stay (Days)") st.plotly_chart(fig_los, use_container_width=True) # Age Distribution st.subheader("Age Distribution") fig_age = px.histogram(filtered_df, x="Age", nbins=20, title="Age Distribution of Patients") st.plotly_chart(fig_age, use_container_width=True) # Interactive Data Table st.subheader("Patient Data") st.dataframe(filtered_df) # Add a download button for the filtered data csv = filtered_df.to_csv(index=False).encode('utf-8') st.download_button( label="Download data as CSV", data=csv, file_name="healthcare_data.csv", mime="text/csv", ) # Key Metrics st.subheader("Key Metrics") col1, col2, col3, col4 = st.columns(4) col1.metric("Total Patients", len(filtered_df)) col2.metric("Avg. Length of Stay", f"{filtered_df['Length of Stay'].mean():.2f} days") col3.metric("Most Common Diagnosis", filtered_df["Diagnosis"].mode()[0]) col4.metric("Busiest Department", filtered_df["Department"].value_counts().index[0])