reactable-py: interactive tables in reports and notebooks
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:
- interactive controls: sort, filter, and paginate.
- expandable rows: allow people to drill into details.
- custom javascript: allow custom actions and integration with websites.
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("manufacturer", "model", "type", "price"]],
cars_93[[=5,
default_page_size=True,
searchable=True,
filterable )
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()
= penguins.to_polars()
pl_penguins
# Create a dictionary of all the nested tables ----
= {
sub_tables =True).to_widget(), style="padding: 1rem")
g: div(Reactable(df, outlinedfor g, df in pl_penguins.group_by("species", "island")
}
# Get summary rows for displayed table ----
= pl_penguins.select(["species", "island"]).unique()
df_uniq
Reactable(
df_uniq,# Use details argument to expand rows into nested tables ----
=lambda row_info: list(sub_tables.values())[row_info.row_index],
details )
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(
js_style """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(6, :],
sleep[:=[
columns
Column(id="extra",
=js_style,
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.