reactable-py: interactive tables in reports and notebooks

Author

Michael Chow

Published

November 4, 2024

I’m excited to share the Python release of reactable—a library for adding interactive tables to your reports and notebooks! reactable is a port of the R library of the same name.

Interactive tables in reports provide some neat features:

For example, here is a table of the most downloaded packages on PyPI:

Notice that if you click on a row, it expands to show the details of the package. Expandable rows are great for providing a dense summary table, while allowing people to drill deeper into pieces that interest them.

(The code for this table is on github in this demo notebook or this qmd.)

Interactive Polars and Pandas DataFrames

reactable works with both Pandas and Polars DataFrames. There are a bunch of datasets included, so you can quickly try it out.

For example, here’s a table that allows searching and filtering some car data:

from reactable import Reactable, embed_css
from reactable.data import cars_93

# Note that currently this function is required
# to put css into notebooks.
embed_css()


Reactable(
    cars_93[["manufacturer", "model", "type", "price"]],
    default_page_size=5,
    searchable=True,
    filterable=True,
)

Note 2 important pieces:

  • There are boxes above each column for filtering, and a global search bar.
  • The table is paginated, with 5 rows per page.

Moreover, cars_93 is actually a lightweight reactable implementation of a “DataFrame” called SimpleFrame. Use methods like cars_93.to_polars() or .to_pandas() to convert to either library. The use of SimpleFrame allows us to provide demo data, without requiring users to install Pandas and Polars.

The Code basics page builds on this example to show the basics of reactable-py.

Expandable rows

Here’s a more advanced example, where each row of this palmer penguins data expands into a nested reactable table.

from reactable import Reactable, embed_css
from reactable.data import penguins
from htmltools import HTML, div

embed_css()

pl_penguins = penguins.to_polars()

# Create a dictionary of all the nested tables ----
sub_tables = {
    g: div(Reactable(df, outlined=True).to_widget(), style="padding: 1rem")
    for g, df in pl_penguins.group_by("species", "island")
}

# Get summary rows for displayed table ----
df_uniq = pl_penguins.select(["species", "island"]).unique()

Reactable(
    df_uniq,
    # Use details argument to expand rows into nested tables ----
    details=lambda row_info: list(sub_tables.values())[row_info.row_index],
)

Notice that if you click a summary row, it expands to show all the data belonging to that group in the data.

See more in the Expandable details page of the user guide.

Custom Javascript

reactable-py supports custom javascript for formatting, styling, and responding to events (like row selection). Below is an example of styling a column of values with javascript.

from reactable import Reactable, JS, embed_css
from reactable.data import sleep

embed_css()

# Javascript for styling values ----
js_style = JS(
    """function(rowInfo) {
      const value = rowInfo.values['extra']
      let color
      if (value > 0) {
        color = '#008000'
      } else {
        color = '#e00000'
      }

      return { color: color, fontWeight: 'bold' }
    }"""
)

# Create table ----
Reactable(
    sleep[:6, :],
    columns=[
        Column(
            id="extra",
            style=js_style,
        )
    ],
)

The JS() function wraps a string, in order to tell reactable that some javascript is being used. See the javascript style, and javascript controls pages for more examples.

Note that you can also use Python to style, which is often easier (but less exciting).

Common use cases

For more examples and use cases, check out the reactable-py Examples page or Cookbook.

For more inspiration see the tons of demo tables in the reactable R docs or this incredible Amtrak train table by Josh Fangmeier.

In conclusion

reactable-py is a port of the reactable R package to Python. It enables you to create beautiful, interactive tables for reports, dashboards, and more. Expand rows, sort columns, or extend all kinds of pieces with javascript! You can install reactable-py by using pip install reactable.

A big motivation for porting reactable to Python was to be able to render Great Tables interactively, so keep an eye out for that in the near future!

To learn more, see the reactable-py get started page or the Code basics page.