from reactable import Reactable, Column, ColFormat, ColGroup, JS, embed_css
from reactable.data import us_states, cars_93
embed_css()
Row groups and aggregation
You can group rows in a table by specifying one or more columns in group_by
:
= cars_93[10:22, ["manufacturer", "model", "type", "price", "mpg_city"]]
data
Reactable(
data,="manufacturer",
group_by )
When rows are grouped, you can aggregate data in a column using an aggregate
function:
= cars_93[14:38, ["type", "price", "mpg_city", "drive_train", "man_trans_avail"]]
data
Reactable(
data,="type",
group_by=[
columnsid="price", aggregate="max"),
Column(id="mpg_city", aggregate="mean", format=ColFormat(digits=1)),
Column(id="drive_train", aggregate="unique"),
Column(id="man_trans_avail", aggregate="frequency"),
Column(
], )
You can use one of the built-in aggregate functions:
= "sum") # Sum of numbers
Column(aggregate = "mean") # Mean of numbers
Column(aggregate = "max") # Maximum of numbers
Column(aggregate = "min") # Minimum of numbers
Column(aggregate = "median") # Median of numbers
Column(aggregate = "count") # Count of values
Column(aggregate = "unique") # Comma-separated list of unique values
Column(aggregate = "frequency") # Comma-separated counts of unique values Column(aggregate
Column(name=None, aggregate='frequency', sortable=None, resizable=None, filterable=None, searchable=None, filter_method=None, show=None, default_sort_order=None, sort_na_last=None, format=None, cell=None, grouped=None, aggregated=None, header=None, footer=None, details=None, html=None, na=None, row_header=None, min_width=None, max_width=None, width=None, align=None, v_align=None, header_v_align=None, sticky=None, class_=None, style=None, header_class=None, header_style=None, footer_class=None, footer_style=None, id=None, default_sort_desc=None, type=None, _selectable=False)
Or a custom aggregate function in JavaScript:
Column(=JS(
aggregate"""
function(values, rows) {
// input:
// - values: an array of all values in the group
// - rows: an array of row data values for all rows in the group (optional)
//
// output:
// - an aggregated value, e.g. a comma-separated list
return values.join(', ')
}
"""
) )
Column(name=None, aggregate=JS(code="\n function(values, rows) {\n // input:\n // - values: an array of all values in the group\n // - rows: an array of row data values for all rows in the group (optional)\n //\n // output:\n // - an aggregated value, e.g. a comma-separated list\n return values.join(', ')\n }\n "), sortable=None, resizable=None, filterable=None, searchable=None, filter_method=None, show=None, default_sort_order=None, sort_na_last=None, format=None, cell=None, grouped=None, aggregated=None, header=None, footer=None, details=None, html=None, na=None, row_header=None, min_width=None, max_width=None, width=None, align=None, v_align=None, header_v_align=None, sticky=None, class_=None, style=None, header_class=None, header_style=None, footer_class=None, footer_style=None, id=None, default_sort_desc=None, type=None, _selectable=False)
Multiple groups
Reactable(
us_states,=["Region", "Division"],
group_by=[
columnsid="Division", aggregate="unique"),
Column(id="Area", aggregate="sum", format=ColFormat(separators=True)),
Column(
], )
Custom aggregate function
Custom aggregate functions are useful when none of the built-in aggregate functions apply, or when you want to aggregate values from multiple columns. For example, when calculating aggregate averages or percentages.
Within a custom aggregate function, you can access the values in the column using the values
argument, and the values in other columns using the rows
argument:
TODO
Include sub rows in pagination
By default, sub rows are excluded from pagination and always shown on the same page when expanded. To include sub rows in pagination, you can set paginate_sub_rows=True
. This is recommended for grouped tables with a large number of rows where expanded rows may not all fit on one page.
= cars_93[["manufacturer", "model", "type", "price", "mpg_city"]]
data
Reactable(=cars_93,
data="type",
group_by=True,
paginate_sub_rows )
Sticky column groups
Reactable(5, :],
cars_93[:=[
column_groups="Make", columns=["manufacturer", "model"], sticky="left"),
ColGroup(name="Price", columns=["min_price", "price", "max_price"], sticky="left"),
ColGroup(name
],=Column(footer="Footer"),
default_col_def=True,
resizable=False,
wrap=True,
bordered )