> ## Documentation Index
> Fetch the complete documentation index at: https://prefab.prefect.io/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Server Dashboard

> Live-updating metrics with sparklines, status badges, and auto-refresh.

A simulated server dashboard that auto-refreshes every 400ms. The context window fills up, metrics tick, and the progress bar changes color as utilization climbs. Everything is driven by a single `SetInterval` and reactive expressions.

> [card]
>
> ```python icon="python" theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
> from prefab_ui import PrefabApp
> from prefab_ui.actions import SetInterval, SetState
> from prefab_ui.components import (
>     Badge, Card, CardContent, CardHeader, CardTitle,
>     Column, Grid, Metric, Muted, Progress, Row, Separator, Text,
> )
> from prefab_ui.components.charts import Sparkline
> from prefab_ui.rx import Rx
>
> tick = Rx("tick")
> cpu = tick % 20 * 4 + 15
> mem = tick % 30 * 2 + 40
> cpu_variant = (cpu > 80).then("destructive", (cpu > 50).then("warning", "success"))
>
> # Live-ticking footer stats
> requests = 12000 + tick % 50 * 30
> latency = 120 + tick % 30
>
> with PrefabApp(
>     state={"tick": 0},
>     on_mount=SetInterval(400, on_tick=SetState("tick", tick + 1)),
>     css_class="max-w-md p-0",
> ):
>     with Column(gap=4):
>         with Grid(columns=2, gap=4):
>             with Card(css_class="gap-0 pb-0 overflow-hidden"):
>                 with CardContent():
>                     Metric(label="CPU Usage", value=f"{cpu}%")
>                 Sparkline(
>                     data=[35, 42, 38, 55, 48, 62, 58, 70, 65, 45],
>                     variant="info", fill=True, css_class="h-10",
>                 )
>             with Card(css_class="gap-0 pb-0 overflow-hidden"):
>                 with CardContent():
>                     Metric(label="Memory", value=f"{mem}%")
>                 Sparkline(
>                     data=[60, 62, 58, 65, 63, 68, 64, 70, 66, 62],
>                     variant="success", fill=True, css_class="h-10",
>                 )
>
>         with Card():
>             with CardHeader():
>                 with Row(align="center", css_class="justify-between"):
>                     CardTitle("System Health")
>                     Badge(cpu_variant, variant=cpu_variant)
>             with CardContent():
>                 with Column(gap=4):
>                     with Row(gap=4, align="center", css_class="justify-between"):
>                         Text(f"CPU: {cpu}%")
>                         Muted(f"Memory: {mem}%")
>                     Progress(value=cpu, max=100, variant=cpu_variant)
>                     Separator()
>                     with Row(gap=6):
>                         Metric(label="Uptime", value=f"99.9{tick % 10}%")
>                         Metric(label="Requests/s", value=requests.number())
>                         Metric(label="p99 Latency", value=f"{latency}ms")
> ```
>
> ```json Protocol icon="brackets-curly" theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
> {
>   "view": {
>     "cssClass": "pf-app-root max-w-md p-0",
>     "onMount": {
>       "action": "setInterval",
>       "duration": 400,
>       "onTick": {"action": "setState", "key": "tick", "value": "{{ tick + 1 }}"}
>     },
>     "type": "Div",
>     "children": [
>       {
>         "cssClass": "gap-4",
>         "type": "Column",
>         "children": [
>           {
>             "cssClass": "gap-4 grid-cols-2",
>             "type": "Grid",
>             "children": [
>               {
>                 "cssClass": "gap-0 pb-0 overflow-hidden",
>                 "type": "Card",
>                 "children": [
>                   {
>                     "type": "CardContent",
>                     "children": [{"type": "Metric", "label": "CPU Usage", "value": "{{ tick % 20 * 4 + 15 }}%"}]
>                   },
>                   {
>                     "cssClass": "h-10",
>                     "type": "Sparkline",
>                     "data": [35, 42, 38, 55, 48, 62, 58, 70, 65, 45],
>                     "variant": "info",
>                     "fill": true,
>                     "curve": "linear",
>                     "strokeWidth": 1.5
>                   }
>                 ]
>               },
>               {
>                 "cssClass": "gap-0 pb-0 overflow-hidden",
>                 "type": "Card",
>                 "children": [
>                   {
>                     "type": "CardContent",
>                     "children": [{"type": "Metric", "label": "Memory", "value": "{{ tick % 30 * 2 + 40 }}%"}]
>                   },
>                   {
>                     "cssClass": "h-10",
>                     "type": "Sparkline",
>                     "data": [60, 62, 58, 65, 63, 68, 64, 70, 66, 62],
>                     "variant": "success",
>                     "fill": true,
>                     "curve": "linear",
>                     "strokeWidth": 1.5
>                   }
>                 ]
>               }
>             ]
>           },
>           {
>             "type": "Card",
>             "children": [
>               {
>                 "type": "CardHeader",
>                 "children": [
>                   {
>                     "cssClass": "items-center justify-between",
>                     "type": "Row",
>                     "children": [
>                       {"type": "CardTitle", "content": "System Health"},
>                       {
>                         "type": "Badge",
>                         "label": "{{ tick % 20 * 4 + 15 > 80 ? 'destructive' : (tick % 20 * 4 + 15 > 50 ? 'warning' : 'success') }}",
>                         "variant": "{{ tick % 20 * 4 + 15 > 80 ? 'destructive' : (tick % 20 * 4 + 15 > 50 ? 'warning' : 'success') }}"
>                       }
>                     ]
>                   }
>                 ]
>               },
>               {
>                 "type": "CardContent",
>                 "children": [
>                   {
>                     "cssClass": "gap-4",
>                     "type": "Column",
>                     "children": [
>                       {
>                         "cssClass": "gap-4 items-center justify-between",
>                         "type": "Row",
>                         "children": [
>                           {"content": "CPU: {{ tick % 20 * 4 + 15 }}%", "type": "Text"},
>                           {"content": "Memory: {{ tick % 30 * 2 + 40 }}%", "type": "Muted"}
>                         ]
>                       },
>                       {
>                         "type": "Progress",
>                         "value": "{{ tick % 20 * 4 + 15 }}",
>                         "max": 100.0,
>                         "variant": "{{ tick % 20 * 4 + 15 > 80 ? 'destructive' : (tick % 20 * 4 + 15 > 50 ? 'warning' : 'success') }}",
>                         "size": "default"
>                       },
>                       {"type": "Separator", "orientation": "horizontal"},
>                       {
>                         "cssClass": "gap-6",
>                         "type": "Row",
>                         "children": [
>                           {"type": "Metric", "label": "Uptime", "value": "99.9{{ tick % 10 }}%"},
>                           {
>                             "type": "Metric",
>                             "label": "Requests/s",
>                             "value": "{{ 12000 + tick % 50 * 30 | number }}"
>                           },
>                           {"type": "Metric", "label": "p99 Latency", "value": "{{ 120 + tick % 30 }}ms"}
>                         ]
>                       }
>                     ]
>                   }
>                 ]
>               }
>             ]
>           }
>         ]
>       }
>     ]
>   },
>   "state": {"tick": 0}
> }
> ```

## How It Works

The whole dashboard runs off a single integer in [state](https://prefab.prefect.io/docs/concepts/state.md): `tick`, which starts at `0`. The [PrefabApp](https://prefab.prefect.io/docs/reference/app.md) declares `on_mount=SetInterval(400, on_tick=SetState("tick", tick + 1))`, so once the view mounts, a timer fires every 400 milliseconds and writes `tick + 1` back into state. Nothing else changes — every animated number on the screen is a [reactive expression](https://prefab.prefect.io/docs/concepts/expressions.md) derived from that one counter.

Those derivations are built in Python using [`Rx`](https://prefab.prefect.io/docs/concepts/expressions). `tick = Rx("tick")` is a handle to the state key, and ordinary arithmetic on it produces an expression rather than a value: `cpu = tick % 20 * 4 + 15` compiles to the string `{{ tick % 20 * 4 + 15 }}` that the renderer evaluates on every frame. Because `tick` climbs forever, the modulo keeps `cpu` cycling through a bounded range, which is what makes the metrics oscillate instead of running off the chart. `mem`, `requests`, and `latency` are the same trick with different constants.

The color changes come from `cpu_variant = (cpu > 80).then("destructive", (cpu > 50).then("warning", "success"))`. The `.then()` method builds a nested ternary — green below 50, amber below 80, red above — and the result feeds three separate props at once: the [Badge](https://prefab.prefect.io/docs/components/badge.md) variant, the [Progress](https://prefab.prefect.io/docs/components/progress.md) bar variant, and (implicitly) the badge label. They stay in lockstep because they read the same expression, so the badge and the bar can never disagree about system health.

Notice what is *not* here: no callback, no re-render loop, no server. `SetInterval` and `SetState` are client-side [actions](https://prefab.prefect.io/docs/concepts/actions.md) the renderer runs on its own. The Python script executes once to emit the component tree and its embedded expressions; from then on the browser owns the clock. The [Sparkline](https://prefab.prefect.io/docs/components/sparkline.md) data is static — the live feel comes entirely from the expressions reacting to `tick`.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.