render.table

render.table(self, _fn=None, *, index=False, classes='table shiny-table w-auto', border=0, **kwargs)

Reactively render a pandas DataFrame object (or similar) as a basic HTML table.

Consider using data_frame instead of this renderer, as it provides high performance virtual scrolling, built-in filtering and sorting, and a better default appearance. This renderer may still be helpful if you use pandas styling features that are not currently supported by data_frame.

Parameters

index: bool = False

Whether to print index (row) labels. (Ignored for pandas Styler objects; call style.hide(axis="index") from user code instead.)

classes: str = ‘table shiny-table w-auto’

CSS classes (space separated) to apply to the resulting table. By default, we use table shiny-table w-auto which is designed to look reasonable with Bootstrap 5. (Ignored for pandas Styler objects; call style.set_table_attributes('class="dataframe table shiny-table w-auto"') from user code instead.)

**kwargs: object = {}

Additional keyword arguments passed to pandas.DataFrame.to_html() or pandas.io.formats.style.Styler.to_html().

Returns

Type Description
A decorator for a function that returns any of the following: 1. A pandas DataFrame object. 2. A pandas Styler object. 3. Any object that has a .to_pandas() method (e.g., a Polars data frame or Arrow table).

Tip

The name of the decorated function (or @output(id=...)) should match the id of a output_table container (see output_table for example usage).

See Also

  • output_table for the corresponding UI component to this render function.

Examples

#| standalone: true
#| components: [editor, viewer]
#| layout: vertical
#| viewerHeight: 400

## file: app.py
import pathlib

import pandas as pd

from shiny import App, Inputs, Outputs, Session, render, ui

dir = pathlib.Path(__file__).parent
mtcars = pd.read_csv(dir / "mtcars.csv")


app_ui = ui.page_fluid(
    ui.input_checkbox("highlight", "Highlight min/max values"),
    ui.output_table("result"),
    # Legend
    ui.panel_conditional(
        "input.highlight",
        ui.panel_absolute(
            "Yellow is maximum, grey is minimum",
            bottom="6px",
            right="6px",
            class_="p-1 bg-light border",
        ),
    ),
    class_="p-3",
)


def server(input: Inputs, output: Outputs, session: Session):
    @render.table
    def result():
        if not input.highlight():
            # If we're not highlighting values, we can simply
            # return the pandas data frame as-is; @render.table
            # will call .to_html() on it.
            return mtcars
        else:
            # We need to use the pandas Styler API. The default
            # formatting options for Styler are not the same as
            # DataFrame.to_html(), so we set a few options to
            # make them match.
            return (
                mtcars.style.set_table_attributes(
                    'class="dataframe shiny-table table w-auto"'
                )
                .hide(axis="index")
                .format(
                    {
                        "mpg": "{0:0.1f}",
                        "disp": "{0:0.1f}",
                        "drat": "{0:0.2f}",
                        "wt": "{0:0.3f}",
                        "qsec": "{0:0.2f}",
                    }
                )
                .set_table_styles(
                    [dict(selector="th", props=[("text-align", "right")])]
                )
                .highlight_min(color="silver")
                .highlight_max(color="yellow")
            )


app = App(app_ui, server)