Real-Time Dashboards: Live Updates with Streamlit
Episode 16: Build A Personal AI Agent
A Practical Guide for Evaluators and M&E Professionals
Why Real-Time Dashboards Matter
Real-time dashboards provide instant visibility into program performance, enabling timely decisions and interventions. This tutorial shows you how to build a live-updating M&E dashboard using Streamlit that automatically refreshes with new data.
Part 1: Setting Up Your Dashboard Environment
Step 1: Install Required Libraries
pip install streamlit plotly pandas openpyxlStep 2: Data Connector
# data_connector.py
import pandas as pd
import os
from datetime import datetime
import numpy as np
class DataConnector:
"""Simple data connector with auto-refresh."""
def __init__(self, file_path='indicator_data.xlsx'):
self.file_path = file_path
self.last_refresh = None
self.cached_data = None
def load_data(self):
"""Load data with caching."""
if not os.path.exists(self.file_path):
self._create_sample_data()
df = pd.read_excel(self.file_path)
df = self._prepare_data(df)
self.cached_data = df
self.last_refresh = datetime.now()
return df
def _prepare_data(self, df):
"""Clean and prepare data."""
if 'date' in df.columns:
df['date'] = pd.to_datetime(df['date'])
if 'target' in df.columns and 'actual' in df.columns:
df['achievement'] = (df['actual'] / df['target'] * 100).round(1)
if 'status' not in df.columns and 'achievement' in df.columns:
df['status'] = pd.cut(
df['achievement'],
bins=[0, 70, 90, 100],
labels=['off_track', 'at_risk', 'on_track']
)
return df
def get_metrics(self, df):
"""Calculate key metrics."""
return {
'total': len(df),
'avg_achievement': df['achievement'].mean() if 'achievement' in df else 0,
'on_track': len(df[df['status'] == 'on_track']) if 'status' in df else 0,
'at_risk': len(df[df['status'] == 'at_risk']) if 'status' in df else 0,
'off_track': len(df[df['status'] == 'off_track']) if 'status' in df else 0,
'last_refresh': datetime.now().strftime('%H:%M:%S')
}
def _create_sample_data(self):
"""Create sample data."""
indicators = ['Vaccination', 'Education', 'Health', 'WASH', 'Nutrition']
categories = ['Health', 'Education', 'Health', 'WASH', 'Nutrition']
data = []
for i, indicator in enumerate(indicators):
target = np.random.randint(100, 500)
achievement = np.random.uniform(55, 98)
actual = int(target * achievement / 100)
data.append({
'indicator_name': indicator,
'target': target,
'actual': actual,
'achievement': round(achievement, 1),
'category': categories[i],
'date': '2024-01-15'
})
df = pd.DataFrame(data)
df.to_excel(self.file_path, index=False)Part 2: Streamlit Dashboard
# dashboard.py
import streamlit as st
import pandas as pd
import plotly.express as px
from data_connector import DataConnector
st.set_page_config(page_title="M&E Dashboard", layout="wide")
# Initialize connector
@st.cache_resource
def get_connector():
return DataConnector('indicator_data.xlsx')
connector = get_connector()
df = connector.load_data()
metrics = connector.get_metrics(df)
# Header
st.title("📊 M&E Performance Dashboard")
st.caption(f"Last updated: {metrics['last_refresh']}")
# Metrics Row
col1, col2, col3, col4, col5 = st.columns(5)
col1.metric("Total", metrics['total'])
col2.metric("On Track", metrics['on_track'], delta="✓")
col3.metric("At Risk", metrics['at_risk'], delta="⚠️")
col4.metric("Off Track", metrics['off_track'], delta="❌")
col5.metric("Avg Achievement", f"{metrics['avg_achievement']:.1f}%")
st.divider()
# Charts
col1, col2 = st.columns(2)
with col1:
st.subheader("Status Distribution")
status_counts = df['status'].value_counts()
fig = px.pie(
values=status_counts.values,
names=status_counts.index,
color=status_counts.index,
color_discrete_map={
'on_track': '#28a745',
'at_risk': '#ffc107',
'off_track': '#dc3545'
}
)
st.plotly_chart(fig, use_container_width=True)
with col2:
st.subheader("Category Performance")
if 'category' in df.columns:
cat_avg = df.groupby('category')['achievement'].mean().sort_values()
fig = px.bar(
x=cat_avg.values,
y=cat_avg.index,
orientation='h',
color=cat_avg.values,
color_continuous_scale='RdYlGn',
labels={'x': 'Achievement (%)', 'y': ''}
)
st.plotly_chart(fig, use_container_width=True)
# Full width charts
st.subheader("Target vs Actual")
if 'target' in df.columns and 'actual' in df.columns:
fig = px.bar(
df,
x='indicator_name',
y=['target', 'actual'],
barmode='group',
labels={'value': 'Values', 'variable': ''}
)
st.plotly_chart(fig, use_container_width=True)
# Data table
with st.expander("View Data"):
st.dataframe(df)
# Sidebar filters
st.sidebar.header("Filters")
status_filter = st.sidebar.multiselect(
"Status",
options=df['status'].unique(),
default=df['status'].unique()
)
if 'category' in df.columns:
cat_filter = st.sidebar.multiselect(
"Category",
options=df['category'].unique(),
default=df['category'].unique()
)
# Auto-refresh
if st.sidebar.button("Refresh Data"):
st.cache_data.clear()
st.rerun()
st.sidebar.markdown("---")
st.sidebar.caption("Data refreshes automatically on load")Part 3: Running the Dashboard
# run.py
import subprocess
import os
def main():
print("Starting M&E Dashboard...")
print("Open http://localhost:8501 in your browser")
subprocess.run(["streamlit", "run", "dashboard.py"])
if __name__ == "__main__":
main()
Deployment Options
| Platform | Cost | Setup Time | Best For |
|---|---|---|---|
| Streamlit Cloud | Free | 5 min | Public dashboards |
| Heroku | $5-25/month | 15 min | Production deployment |
| AWS EC2 | $10-50/month | 30 min | Enterprise deployment |
| Local Machine | Free | 2 min | Development and testing |
Troubleshooting
| Issue | Solution |
|---|---|
| Module not found | Run: pip install streamlit plotly pandas openpyxl |
| No data showing | Check file path and column names |
| Port already in use | streamlit run dashboard.py –server.port 8502 |
| Charts not rendering | Check data types and column existence |
Best Practices
- Cache Data: Use @st.cache_data to prevent reloading
- Limit Data: Aggregate large datasets for performance
- Mobile Responsive: Use st.columns() for layout
- Error Handling: Add try/except for data loading
- Refresh Button: Allow manual refresh alongside auto
Next Steps
- Add Filters: Enable filtering by region or date
- Export Options: Add download buttons for data
- Alerts: Display notifications for off-track indicators
- User Authentication: Add login for secure access
Master Real-Time Dashboard Development
The AI Agents for Evaluators Certificate teaches you to build complete M&E solutions including real-time dashboards, automated reporting, and stakeholder management.
What you will learn:
- Build real-time dashboards with Streamlit
- Implement live data updates
- Create interactive visualizations
- Deploy for stakeholder access
Course Features: 32 lectures · Lifetime access · Certificate included
Enroll Now – $249 Lifetime Access
Bundle with AI in M&E course and save 30%
This guide is part of the AI Agents for Evaluators series. Continue your learning journey with the full certificate course above.
