st.metric presents an important value as a labeled, prominent number. It can also show how that value changed compared with a reference value, making it useful for conversion results, progress indicators, and dashboard summaries. section 3.6.3 section 7.4.2
A metric element combines:
- A text label identifying the measure. section 3.6.3
- A prominently displayed value, such as a converted amount or completed-task count. section 3.6.3 section 4.5.8
- An optional delta showing change from a prior period or reference value. section 7.4.2
Show a conversion result
After calling the conversion backend, format both results with their unit abbreviations and hide the irrelevant delta: st.metric("From", from_display, delta=None) and st.metric("To", to_display, delta=None). Place the two metrics in columns to display them side by side. section 3.6.3
Compare dashboard periods
Calculate a percentage change from the comparison dataframe, format it, and pass it as the third argument: st.metric(metric.title, formatted_value, formatted_delta, "normal"). Use "inverse" when decreases are favorable, or "off" to show deltas in gray. section 7.4.2
st.metric("From", from_display, delta=None)
st.metric("To", to_display, delta=None)
Inspect the minimal conversion-result calls for labeled metrics with no delta. section 3.6.3
def metric_bar(main_df, compare_df):
with st.container(border=True):
metric_cols = st.columns(len(display_metrics))
for idx, metric_name in enumerate(display_metrics):
metric = metrics[metric_name]
with metric_cols[idx]:
value = get_metric(main_df, metric)
formatted_value = format_metric(value, metric.type)
formatted_delta = get_formatted_delta(value, compare_df, metric)
c1, c2, c3 = st.columns([1, 3, 1])
with c2:
st.metric(
metric.title, formatted_value, formatted_delta, "normal")
Inspect a complete metric-bar call that formats values, computes deltas, and arranges dashboard metrics. section 7.4.2
