/ dashboard / components / metrics.py
metrics.py
 1  """
 2  Reusable Metric Components
 3  
 4  Provides consistent metric card displays across the dashboard.
 5  """
 6  
 7  import streamlit as st
 8  from typing import List, Dict, Optional
 9  
10  
11  def display_metric_card(label: str, value: str | int | float, delta: Optional[str] = None, delta_color: str = "normal"):
12      """
13      Display a styled metric card.
14  
15      Args:
16          label: Metric label
17          value: Metric value
18          delta: Optional delta/change indicator
19          delta_color: Color for delta ("normal", "inverse", "off")
20      """
21      st.metric(
22          label=label,
23          value=value,
24          delta=delta,
25          delta_color=delta_color
26      )
27  
28  
29  def display_metric_grid(metrics: List[Dict]):
30      """
31      Display metrics in a responsive grid.
32  
33      Args:
34          metrics: List of metric dicts with keys: label, value, delta (optional), delta_color (optional)
35      """
36      cols = st.columns(len(metrics))
37      for col, metric in zip(cols, metrics):
38          with col:
39              col.metric(
40                  label=metric['label'],
41                  value=metric['value'],
42                  delta=metric.get('delta'),
43                  delta_color=metric.get('delta_color', 'normal')
44              )
45  
46  
47  def display_kpi_summary(title: str, metrics: List[Dict]):
48      """
49      Display a titled section with metric cards.
50  
51      Args:
52          title: Section title
53          metrics: List of metric dicts
54      """
55      st.subheader(title)
56      display_metric_grid(metrics)