| Function signature[source] | |
|---|---|
st.metric(label, value, delta=None, delta_color="normal", *, help=None, label_visibility="visible", border=False, width="stretch", height="content", chart_data=None, chart_type="line", delta_arrow="auto", format=None) | |
| Parameters | |
label (str) | The header or title for the metric. The label can optionally contain GitHub-flavored Markdown of the following types: Bold, Italics, Strikethroughs, Inline Code, Links, and Images. Images display like icons, with a max height equal to the font height. Unsupported Markdown elements are unwrapped so only their children (text contents) render. Display unsupported elements as literal characters by backslash-escaping them. E.g., "1\. Not an ordered list". See the body parameter of st.markdown for additional, supported Markdown directives. |
value (int, float, decimal.Decimal, str, or None) | Value of the metric. None is rendered as a long dash. The value can optionally contain GitHub-flavored Markdown, subject to the same limitations described in the label parameter. |
delta (int, float, decimal.Decimal, str, or None) | Amount or indicator of change in the metric. An arrow is shown next to the delta, oriented according to its sign:
You can modify the display, color, and orientation of the arrow using the delta_color and delta_arrow parameters. The delta can optionally contain GitHub-flavored Markdown, subject to the same limitations described in the label parameter. |
delta_color (str) | The color of the delta and chart. This can be one of the following:
|
help (str or None) | A tooltip that gets displayed next to the metric label. Streamlit only displays the tooltip when label_visibility="visible". If this is None (default), no tooltip is displayed. The tooltip can optionally contain GitHub-flavored Markdown, including the Markdown directives described in the body parameter of st.markdown. |
label_visibility ("visible", "hidden", or "collapsed") | The visibility of the label. The default is "visible". If this is "hidden", Streamlit displays an empty spacer instead of the label, which can help keep the widget aligned with other widgets. If this is "collapsed", Streamlit displays no label or spacer. |
border (bool) | Whether to show a border around the metric container. If this is False (default), no border is shown. If this is True, a border is shown. |
height ("content", "stretch", or int) | The height of the metric element. This can be one of the following:
|
width ("stretch", "content", or int) | The width of the metric element. This can be one of the following:
|
chart_data (Iterable or None) | A sequence of numeric values to display as a sparkline chart. If this is None (default), no chart is displayed. The sequence can be anything supported by st.dataframe, including a list or set. If the sequence is dataframe-like, the first column will be used. Each value will be cast to float internally by default. The chart uses the color of the delta indicator, which can be modified using the delta_color parameter. |
chart_type ("line", "bar", or "area") | The type of sparkline chart to display. This can be one of the following:
|
delta_arrow ("auto", "up", "down", or "off") | Controls the direction of the delta indicator arrow. This can be one of the following strings:
|
format (str or None) | A format string controlling how numbers are displayed for value and delta. The format is only applied if the value or delta is numeric. If the value or delta is a string with non-numeric characters, the format is ignored. The format can be one of the following values:
|
Examples
Example 1: Show a metric
PythonExample 2: Create a row of metrics
st.metric looks especially nice in combination with st.columns.
PythonExample 3: Modify the delta indicator
The delta indicator color can also be inverted or turned off.
PythonExample 4: Create a grid of metric cards
Add borders to your metrics to create a dashboard look.
PythonExample 5: Show sparklines
To show trends over time, add sparklines.
Python
Still have questions?
Our forums are full of helpful information and Streamlit experts.
