Skip to content

7. KPI indicators

Indicators show one number (and how it moved) as a small self-contained HTML widget, with no JavaScript and no Plotly.

step7_indicators.py
import polars as pl
from babel.support import NullTranslations
from step4_theme_and_colors import THEME
from tutorial_data import METADATA, SALES, run_query

from orama.indicators import ArcIndicatorStrategy
from orama.variables import Aggregation

this_year = SALES.filter(pl.col("sold_at").dt.year() == 2024)
last_year = SALES.filter(pl.col("sold_at").dt.year() == 2023)

# Indicators show one number (and optionally how it moved) as a self-contained HTML widget.
strategy = ArcIndicatorStrategy(
    fields_metadata=METADATA,
    current_aggregation=Aggregation(name="current", function="sum", field="revenue"),
    comparison_aggregation=Aggregation(name="previous", function="sum", field="revenue"),
    domain_min=0,
    domain_max=float(this_year["revenue"].sum()) * 1.5,
    title="Revenue",
    subtitle="2024 vs 2023",
)

# "current" and "previous" are two queries; here they run on different slices of the data.
view = strategy.build(run_query(strategy, this_year, comparison_df=last_year), NullTranslations(), THEME)

# `view.html` is a <figure> you can drop anywhere. Its look is driven by CSS custom properties,
# so a stylesheet (or a theme switch) restyles it without rebuilding anything:
PAGE_CSS = """
:root {
  --orama-indicator-thickness: 26px;      /* ring / bar thickness */
  --orama-indicator-track-color: #e8ecf5; /* the unfilled part */
  --orama-indicator-value-font-size: 44px;
}
"""
page = f"<style>{PAGE_CSS}</style>{view.html}"

An arc indicator

There are four shapes. Pick one with the strategy class.

The four indicator shapes

Strategy Shape
TextIndicatorStrategy the value and its delta
BarIndicatorStrategy a progress bar
CircleIndicatorStrategy a full ring
ArcIndicatorStrategy a 180° or 270° gauge

Restyle with CSS variables

The widget reads its look from --orama-indicator-* custom properties. Each one falls back to the value computed from your theme, so you only set what you want to change. In the example above the stylesheet at the bottom of the snippet turned the left image into the right one:

Before

Defaults

After

After the CSS above

The complete list is in the Indicators reference.

Theme switches need no rebuild

Because the colors are CSS variables, switching your page between light and dark restyles indicators instantly. Theme.to_css_variables() generates the variables for you; step 9 uses it.

Next: animated export →