Altair

Declare a selection in Vega-Lite’s grammar and read it from Python as a reactive value – no JavaScript, no callbacks.

Altair is the Python API for Vega-Lite, a declarative grammar for interactive charts: describe data, encodings, and marks, and Vega-Lite compiles a complete, interactive visualization with selections built in. @render_altair turns a chart into a reactive input: read chart.selected() or chart.selected_rows() from any @render.* function, @reactive.calc, or @reactive.effect, the same way you’d read input.my_slider().

hero

Drag a box to brush the scatter plot; the empty selection filters to zero rows, not everything, so the table starts empty too. Try the app.

Installation

pip install 'shinywidgets[altair]'

This installs altair and narwhals. The example datasets (cars, seattle_weather) come from altair.datasets, bundled with altair itself.

Quick start

import altair as alt
from altair.datasets import data
from shiny.express import ui

from shinywidgets import render_altair

ui.page_opts(title="Cars", fillable=True)

cars = data.cars()


@render_altair
def plot():
    return (
        alt.Chart(cars)
        .mark_circle()
        .encode(x="Horsepower:Q", y="Miles_per_Gallon:Q", color="Origin:N")
    )

quickstart

No layout kwargs, no sizing calls: the chart fills its card because the output has no explicit height.

Overview

A look at what you can build. Click any of them to open the app, with its code, in Shinylive.

Every Altair chart type – marks, layers, facets, concatenated views – renders through render_altair the same way.

Reacting to interaction

A Vega-Lite selection is declared state, not a transient event: you name it when you add it to the chart (alt.selection_interval(name="brush"), alt.selection_point(name="pick")), and the chart always has a current value for it, starting out empty. Read chart.selected() or chart.selected_rows() from any reactive context, the same way you’d read input.my_slider().

Brush to filter a table

The empty selection filters to zero rows, not “everything,” so a @render.data_frame can consume selected_rows() directly without checking for None:

import altair as alt
from altair.datasets import data
from shiny.express import render, ui

from shinywidgets import render_altair

ui.page_opts(title="Interval (brush) selection", fillable=True)

cars = data.cars()

with ui.layout_columns(col_widths=[8, 4]):
    with ui.card(full_screen=True):
        ui.card_header("Drag a box to brush")

        @render_altair
        def scatter():
            brush = alt.selection_interval(name="brush")
            return (
                alt.Chart(cars)
                .mark_circle()
                .encode(
                    x="Horsepower:Q",
                    y="Miles_per_Gallon:Q",
                    color=alt.condition(brush, "Origin:N", alt.value("lightgray")),
                )
                .add_params(brush)
            )

    with ui.card(full_screen=True):
        ui.card_header("Rows inside the brush")

        @render.data_frame
        def brushed():
            return scatter.selected_rows("brush")[["Name", "Horsepower", "Miles_per_Gallon"]]

Click for details

A single-mark IndexSelection is a natural fit for a “detail card” pattern – the selected row’s other columns (not just the ones plotted) render as free-form UI.

import altair as alt
from altair.datasets import data
from shiny.express import render, ui

from shinywidgets import render_altair

ui.page_opts(title="Click a point for details", fillable=True)

cars = data.cars()

with ui.layout_columns(col_widths=[8, 4]):
    with ui.card(full_screen=True):
        ui.card_header("Click a car")

        @render_altair
        def scatter():
            pick = alt.selection_point(name="pick")
            return (
                alt.Chart(cars)
                .mark_circle()
                .encode(
                    x="Horsepower:Q",
                    y="Miles_per_Gallon:Q",
                    color=alt.condition(pick, alt.value("steelblue"), alt.value("lightgray")),
                )
                .add_params(pick)
            )

    with ui.card(full_screen=True):
        ui.card_header("Detail card")

        @render.ui
        def detail():
            rows = scatter.selected_rows("pick")
            if len(rows) == 0:
                return ui.p("Click a point to see its details.")
            car = rows.iloc[0]
            return ui.TagList(
                ui.h5(car["Name"]),
                ui.p(f"{car['Horsepower']:.0f} hp, {car['Miles_per_Gallon']:.1f} mpg"),
                ui.p(f"{car['Cylinders']} cylinders, {car['Origin']}"),
            )

Clicked car’s detail card shown

Updating in place

Selections are read-only from Python – there’s no way to set chart.widget.selections.brush = ... and have it move the brush. Params, by contrast, are meant to be set from Python: assign chart.widget.params.<name> = value inside a @reactive.effect, and the chart restyles in place, without resending its data or losing the user’s current selection/zoom state.

import altair as alt
from altair.datasets import data
from shiny import reactive
from shiny.express import input, render, ui

from shinywidgets import render_altair

ui.page_opts(title="Updating a param in place", fillable=True)

cars = data.cars()

with ui.sidebar():
    ui.input_slider("threshold", "Highlight above (mpg)", min=10, max=45, value=30)

with ui.card(full_screen=True):
    ui.card_header("Moving the slider restyles the chart without resending data")

    @render_altair
    def chart():
        with reactive.isolate():
            threshold = alt.param(name="threshold", value=input.threshold())
        return (
            alt.Chart(cars)
            .mark_circle()
            .encode(
                x="Horsepower:Q",
                y="Miles_per_Gallon:Q",
                color=alt.condition(
                    "datum.Miles_per_Gallon > threshold", alt.value("crimson"), alt.value("lightgray")
                ),
            )
            .add_params(threshold)
        )

    @reactive.effect
    def _():
        chart.widget.params.threshold = input.threshold()

Wrapping the initial read in reactive.isolate() keeps the render function from re-running on every slider move:

Use in-place param updates when you’re restyling an existing chart (thresholds, highlighted categories, a bound range). Re-render (just read the input in the @render_altair function itself) when the structure changes – different marks, a different data frame, added/removed encodings – since a param can’t add a layer that wasn’t already there. Avoid reassigning chart.widget.chart or chart.widget.spec wholesale as a shortcut for either.

Linking widgets

Vega-Lite can link views entirely on its own, with transform_filter(selection) – no server round trip, updates on every mouse-move. Shiny’s accessors let you link through Python instead, which costs a round trip but lets you do anything Python can do: pandas aggregation, calling a model, writing to a database, or feeding a completely different kind of output. The two aren’t mutually exclusive, as the next example shows.

Brush into another widget

The accessors work the same way regardless of what consumes them – including a different @render_* output entirely. Here, a brushed selection feeds a reactable table instead of @render.data_frame:

import altair as alt
from altair.datasets import data
from reactable import Reactable
from shiny.express import ui

from shinywidgets import render_altair, render_reactable

ui.page_opts(title="Brush an altair chart, render a reactable table", fillable=True)

cars = data.cars()

with ui.layout_columns(col_widths=[8, 4]):
    with ui.card(full_screen=True):
        ui.card_header("Drag a box to brush")

        @render_altair
        def scatter():
            brush = alt.selection_interval(name="brush")
            return (
                alt.Chart(cars)
                .mark_circle()
                .encode(
                    x="Horsepower:Q",
                    y="Miles_per_Gallon:Q",
                    color=alt.condition(brush, "Origin:N", alt.value("lightgray")),
                )
                .add_params(brush)
            )

    with ui.card(full_screen=True):
        ui.card_header("Selected rows, as a reactable table")

        @render_reactable
        def tbl():
            # reactable reads its data once per render, so a new selection
            # means a new table rather than an in-place update.
            rows = scatter.selected_rows("brush")[["Name", "Horsepower", "Miles_per_Gallon"]]
            return Reactable(rows, default_page_size=6)

Brushed rows in a reactable table

Accessor reference

Accessor Returns Before any interaction Across a re-render Default rate policy
selected(name=None) An IntervalSelection, PointSelection, or IndexSelection (altair.utils.selection) seeded: an empty selection, never None re-seeded – a new chart’s named selection starts empty again debounce, 250 ms
selected_rows(name=None, df=None) A frame of the matching rows, same type as df (pandas or polars) seeded: empty frame re-seeded (follows selected()) follows selected(); no separate rate limit

name can be omitted when the chart has exactly one selection; with more than one, name the one you want. Always give your selections explicit names – Altair’s auto-generated ones (param_3, …) change on every re-render, which would silently break selected("param_3") the next time your render function runs.

Unlike renderers whose accessors start at None until the user interacts, selected() attaches already seeded (an empty selection) the moment the chart renders – there’s no warm-up and no None to guard against. You don’t need req() to wait for it the way you would for most other renderers’ accessors – it’s always safe to call inside a @render.* function. What you usually want to guard instead is the emptiness of the result, e.g. if len(rows) == 0: ....

Point and index selections

A point selection has three distinct flavors, depending on how you construct it:

  • alt.selection_point(name=..., encodings=["color"], bind="legend"): clicking a legend entry picks every mark whose value on that encoding channel matches. encodings= names a channel, so it resolves to whatever field happens to be mapped there.
  • alt.selection_point(name=..., fields=["Origin"]): the same idea, but naming the data column directly instead of a channel – useful when you want the test tied to a field regardless of which channel (if any) encodes it. Interchangeable with encodings=["color"] above when color is bound to Origin: both report [{'Origin': 'Europe'}] from the same legend click.
  • alt.selection_point(name=...) with neither encodings= nor fields=: clicking (or shift-clicking, for multiple) individual marks. This is an IndexSelection – .value is a list of 0-based row positions in the underlying data, so df.iloc[sel.value] (which is exactly what selected_rows() does for you) just works.
import altair as alt
from altair.datasets import data
from shiny.express import render, ui

from shinywidgets import render_altair

ui.page_opts(title="Point selections: legend vs. mark clicks", fillable=True)

cars = data.cars()

with ui.layout_columns():
    with ui.card(full_screen=True):
        ui.card_header("encodings=['color']: click a legend entry")

        @render_altair(height="320px")
        def by_legend():
            pick = alt.selection_point(name="origin_pick", encodings=["color"], bind="legend")
            return (
                alt.Chart(cars)
                .mark_circle()
                .encode(
                    x="Horsepower:Q",
                    y="Miles_per_Gallon:Q",
                    color=alt.condition(pick, "Origin:N", alt.value("lightgray")),
                )
                .add_params(pick)
            )

        @render.code
        def legend_value():
            return str(by_legend.selected("origin_pick").value)

    with ui.card(full_screen=True):
        ui.card_header("No encodings/fields: shift-click points")

        @render_altair(height="320px")
        def by_mark():
            pick = alt.selection_point(name="car_pick")
            return (
                alt.Chart(cars)
                .mark_circle()
                .encode(
                    x="Horsepower:Q",
                    y="Miles_per_Gallon:Q",
                    color=alt.condition(pick, alt.value("steelblue"), alt.value("lightgray")),
                )
                .add_params(pick)
            )

        @render.text
        def picked_count():
            return f"{len(by_mark.selected_rows('car_pick'))} cars picked (row positions: {by_mark.selected('car_pick').value})"

        @render.data_frame
        def picked_cars():
            return by_mark.selected_rows("car_pick")[["Name", "Horsepower"]]

Binned and temporal fields

selected_rows() evaluates the selection the way Vega-Lite does, which matters for two field types in particular:

  • Binned fields (alt.X("Horsepower:Q", bin=alt.Bin(step=25))): clicking a bar selects a half-open range ([lo, hi)), matching Vega-Lite’s own semantics – not the closed interval you’d get from a naive df.query("lo <= x <= hi").
  • Temporal fields: Vega sends dates as UTC; a time-zone-naive column is compared in the browser’s local time zone (via the widget’s local_tz), and a time-zone-aware column is compared in its own zone. You don’t have to do this conversion yourself.
import altair as alt
from altair.datasets import data
from shiny.express import render, ui

from shinywidgets import render_altair

ui.page_opts(title="Binned and temporal fields", fillable=True)

cars = data.cars()

with ui.layout_columns():
    with ui.card(full_screen=True):
        ui.card_header("Click a bar: binned selection")

        @render_altair
        def histogram():
            pick = alt.selection_point(name="hp_bin", encodings=["x"])
            return (
                alt.Chart(cars)
                .mark_bar()
                .encode(
                    x=alt.X("Horsepower:Q", bin=alt.Bin(step=25)),
                    y="count()",
                    color=alt.condition(pick, alt.value("steelblue"), alt.value("lightgray")),
                )
                .add_params(pick)
            )

        @render.text
        def bin_count():
            n = len(histogram.selected_rows("hp_bin"))
            return f"{n} cars in the selected 25-hp bin" if n else "Click a bar above"

    with ui.card(full_screen=True):
        ui.card_header("Drag along the axis: temporal interval")

        @render_altair
        def timeline():
            brush = alt.selection_interval(name="years", encodings=["x"])
            return (
                alt.Chart(cars)
                .mark_tick()
                .encode(x="Year:T")
                .add_params(brush)
            )

        @render.text
        def year_count():
            n = len(timeline.selected_rows("years"))
            return f"{n} cars in the selected years" if n else "Drag along the axis above"

The right-hand chart’s gray brush overlay confirms the drag registered, and the count below it reflects the correct, Vega-Lite-equivalent range test. The left-hand chart’s bin_count also updates correctly, even though Vega-Lite doesn’t visually highlight the clicked bar.

Multiple named selections

A chart can declare more than one selection; read each by name. Two selections in the same view interact, though: in this example, dragging the brush clears the legend pick, and clicking the legend clears the brush (this is Vega-Lite’s own behavior for co-located selections, not something shinywidgets adds or could prevent).

import altair as alt
from altair.datasets import data
from shiny.express import render, ui

from shinywidgets import render_altair

ui.page_opts(title="Two named selections on one chart", fillable=True)

cars = data.cars()


@render_altair
def chart():
    brush = alt.selection_interval(name="brush", encodings=["x"])
    origin_pick = alt.selection_point(name="origin_pick", encodings=["color"], bind="legend")
    return (
        alt.Chart(cars)
        .mark_circle()
        .encode(
            x="Horsepower:Q",
            y="Miles_per_Gallon:Q",
            color=alt.condition(origin_pick, "Origin:N", alt.value("lightgray")),
            opacity=alt.condition(brush, alt.value(1.0), alt.value(0.3)),
        )
        .add_params(brush, origin_pick)
    )


with ui.layout_columns():

    @render.code
    def brush_state():
        return f"brush: {chart.selected('brush').value}"

    @render.code
    def legend_state():
        return f"origin_pick: {chart.selected('origin_pick').value}"

If you want two selections that don’t clear each other, put them in separate views – two chart outputs, or two layers that aren’t both interaction targets.

Polars frames

selected_rows() supports polars directly (via narwhals): a chart built from a polars.DataFrame filters to a polars.DataFrame, with no extra configuration.

import altair as alt
from altair.datasets import data
import polars as pl
from shiny.express import render, ui

from shinywidgets import render_altair

ui.page_opts(title="Polars data", fillable=True)

cars = pl.from_pandas(data.cars())

with ui.layout_columns(col_widths=[8, 4]):
    with ui.card(full_screen=True):
        ui.card_header("Chart built from a polars.DataFrame")

        @render_altair
        def scatter():
            brush = alt.selection_interval(name="brush")
            return (
                alt.Chart(cars)
                .mark_circle()
                .encode(
                    x="Horsepower:Q",
                    y="Miles_per_Gallon:Q",
                    color=alt.condition(brush, "Origin:N", alt.value("lightgray")),
                )
                .add_params(brush)
            )

    with ui.card(full_screen=True):
        ui.card_header("selected_rows() returns a polars.DataFrame")

        @render.code
        def kind():
            return type(scatter.selected_rows("brush")).__name__

        @render.data_frame
        def rows():
            return scatter.selected_rows("brush").select("Name", "Horsepower")

Brushed rows as a polars frame

df= defaults to the chart’s own data frame; pass it explicitly for charts whose data is a URL string, or a layered/concatenated chart whose layers weren’t built from the identical data frame object.

Tuning accessors

Reading on demand

Because selected()/selected_rows() are live and seeded from the moment the chart renders, they’re safe to read from somewhere that only runs occasionally, like a download handler – no prior read, no warm-up, and the value you get back is whatever the user last brushed, even on the handler’s first run:

import altair as alt
from altair.datasets import data
from shiny.express import render, ui

from shinywidgets import render_altair

ui.page_opts(title="Reading selected_rows() on demand", fillable=True)

cars = data.cars()

with ui.layout_columns(col_widths=[8, 4]):
    with ui.card(full_screen=True):
        ui.card_header("Drag a box to brush, then download -- no need to click anything first")

        @render_altair
        def scatter():
            brush = alt.selection_interval(name="brush")
            return (
                alt.Chart(cars)
                .mark_circle()
                .encode(
                    x="Horsepower:Q",
                    y="Miles_per_Gallon:Q",
                    color=alt.condition(brush, "Origin:N", alt.value("lightgray")),
                )
                .add_params(brush)
            )

    with ui.card():

        @render.download_button(
            filename="brushed.csv", label="Download brushed rows as CSV"
        )
        def download():
            # selected_rows() is seeded empty from the moment the chart
            # renders, so this never needs a req() or a prior interaction.
            yield scatter.selected_rows("brush").to_csv(index=False)

Rate limiting

Two independent rate limits are in play, and they solve different problems:

  • rate_policy / rate_delay on @render_altair control how often Python re-reads selected() (and reactive_read() on this widget). The default is a 250 ms debounce, which is what you want for a brush you’re dragging: Python only sees the final-ish value, not every intermediate frame. Pass rate_policy="throttle" for steady-interval updates instead of trailing-edge ones, or rate_delay=0 to see every message.
  • debounce_wait / max_wait on alt.JupyterChart (default debounce_wait=10, max_wait=True) control how often the browser sends a selection update to the kernel at all, independent of shinywidgets. Raising this (alt.JupyterChart(chart, debounce_wait=150)) cuts the number of messages that cross the wire while dragging, which matters more as your data or your downstream computation gets larger.

The default is enough for most apps; reach for debounce_wait when a selection drives an expensive computation and even the debounced Python read fires too often.

Bound params with reactive_read()

A param (alt.param(...)) isn’t a selection. You can bind one directly to an HTML control with alt.binding_range, alt.binding_select, alt.binding_checkbox, or alt.binding_radio, with no Shiny input and no Python round trip at all. chart.read("name") can’t reach a param, though – read one with reactive_read(chart.widget.params, "name") instead:

import altair as alt
from altair.datasets import data
from shiny.express import render, ui

from shinywidgets import reactive_read, render_altair

ui.page_opts(title="Reading a bound param with reactive_read", fillable=True)

cars = data.cars()

with ui.layout_columns(col_widths=[8, 4]):
    with ui.card(full_screen=True):
        ui.card_header("Model year is a param, not a selection")

        @render_altair
        def chart():
            year_slider = alt.binding_range(min=1970, max=1982, step=1, name="Model year: ")
            year = alt.param(name="year", value=1977, bind=year_slider)
            return (
                alt.Chart(cars)
                .mark_circle()
                .encode(
                    x="Horsepower:Q",
                    y="Miles_per_Gallon:Q",
                    opacity=alt.condition(
                        "year(datum.Year) == year", alt.value(0.9), alt.value(0.05)
                    ),
                )
                .add_params(year)
            )

    with ui.card(full_screen=True):
        ui.card_header("Python reacts to the slider too")

        @render.text
        def year_value():
            # params aren't selections, so there's no accessor for them;
            # reactive_read watches the trait directly.
            year = reactive_read(chart.widget.params, "year")
            return f"Showing model year {int(year)}" if year is not None else "..."

Slider’s model year read back into Python

local_tz and debounce_wait, by contrast, are traits of chart.widget itself, so reading them doesn’t need reactive_read() at all – chart.read("local_tz") (the browser’s IANA zone name used for naive temporal columns) and chart.read("debounce_wait").

Large data

More than 5,000 rows raises MaxRowsError on a bare DataFrame unless you opt into a transformer that stops inlining raw rows into the spec. VegaFusion (alt.data_transformers.enable("vegafusion")) runs bin/aggregate transforms in Python via DataFusion instead of the browser, and selected()/selected_rows() keep working through it – but it only pays off when the chart aggregates. A binned histogram’s spec stays small no matter how many rows feed it, while a raw per-row mark_circle scatter’s spec keeps growing with row count – VegaFusion moves where the transform runs, it doesn’t shrink a raw-point payload.

import altair as alt
import numpy as np
import pandas as pd
from shiny.express import render, ui

from shinywidgets import render_altair

# Above ~5,000 rows, Altair raises MaxRowsError on a bare DataFrame unless you
# opt into a transformer that stops inlining raw rows into the spec. VegaFusion
# computes bin/aggregate transforms in Python (via DataFusion) instead of the
# browser, so only the aggregated result crosses the wire -- not the million
# rows behind it. Assign the return value: Shiny Express auto-displays any
# bare top-level expression, including the enabler object `enable()` returns.
_ = alt.data_transformers.enable("vegafusion")

rng = np.random.default_rng(0)
n = 1_000_000
df = pd.DataFrame({"value": rng.normal(loc=50, scale=15, size=n)})

ui.page_opts(title="VegaFusion at scale", fillable=True)

with ui.layout_columns(col_widths=[8, 4]):
    with ui.card(full_screen=True):
        ui.card_header(f"Click a bar ({n:,} rows, aggregated with VegaFusion)")

        @render_altair
        def histogram():
            pick = alt.selection_point(name="bin", encodings=["x"])
            return (
                alt.Chart(df)
                .mark_bar()
                .encode(x=alt.X("value:Q", bin=alt.Bin(maxbins=40)), y="count()")
                .add_params(pick)
            )

    with ui.card(full_screen=True):
        ui.card_header("histogram.selected_rows('bin')")

        @render.code
        def count():
            rows = histogram.selected_rows("bin")
            return f"{len(rows):,} of {len(df):,} rows in the selected bin"

Clicking a bar still updates selected_rows() correctly, even on a million-row chart. Needs both vegafusion and vl-convert-python installed – the altair extra pulls in neither. This is the “summarize on the server” approach from Large data & performance.

Layout and sizing

A chart without an explicit width/height is sized to "container" and fills its output – cards, sidebars, full-screen toggles, and window resizes all just work, with no code beyond the usual ui.page_opts(fillable=True) / ui.card(full_screen=True). This applies to a plain alt.Chart and to LayerChart.

Concatenated, faceted, and repeated charts keep their natural size instead. render_altair warns when it sees an alt.concat() for exactly this reason, suggesting shiny.ui.layout_column_wrap() (one chart per card) instead, which does fill; the same limitation applies to |/& (hconcat/vconcat), facet(), and repeat(), which don’t currently trigger the warning. Use a vconcat/hconcat (as in “Two ways to link” above) only when you specifically want several views that share one Vega-Lite spec and its selections; otherwise prefer one chart per card.

An explicit size always wins: .properties(width=..., height=...) (as used for facet() in the layers-and-small- multiples example above, and for the two stacked views in “Two ways to link”) pins a chart or sub-view to a fixed size regardless of its container.

Gotchas

  • selected_rows() needs a real data frame on the chart. Layered or concatenated charts only hoist one to the top level when every layer was built from the identical frame object (by identity, not content) – building each layer from a fresh filter call raises TypeError. Compute the frame once and reuse the same object for every layer, or pass selected_rows(df=...) explicitly.
  • Selections are read-only from Python – there’s no way to move a brush or set a legend pick from code. Use a param (alt.param) for anything you want to drive from the server side.
  • Replacing chart.widget.chart (or .spec) as a whole resends all the chart’s data and resets its selections. Prefer a param update or a full re-render.
  • More than 5,000 rows raises MaxRowsError unless you call alt.data_transformers.disable_max_rows() or enable the VegaFusion transformer (alt.data_transformers.enable("vegafusion"), which also requires pyarrow).
  • JupyterChart loads its JavaScript from a CDN at runtime. For an offline or firewalled deployment, call alt.JupyterChart.enable_offline() once at import time (requires vl-convert-python, not in the altair extra).

Learn more