liveBook · Manning

St.metric

As explained in Build Python Web Apps with Streamlit

st.metric is a Streamlit display function that presents a labeled metric value and optionally its change from a reference value.

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:

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

How Build Python Web Apps with Streamlit covers it

How deep it goes
Explains metric anatomy, placement, delta calculation, formatting, and color semantics without descending into widget internals.
Examples
Demonstrates unit conversion results, task completion counts, and filtered dashboard metrics compared across date ranges.
What it assumes
Assumes basic Streamlit widgets, columns, backend results, and data filtering while demonstrating the metric call itself.
What you can do after
Apply it via Streamlit to display labeled values, hide irrelevant deltas, arrange metrics, and show formatted comparisons.
How it connects
Connects metrics to dashboards, dataframes, comparison periods, percentage change, and Streamlit columns.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build Python Web Apps with Streamlit. The section links point to the parts of the book it draws on.