from reactable import Reactable, Column, ColFormat, JS, embed_css
from reactable.data import penguins
import polars as pl
embed_css()
= penguins.to_polars() pl_penguins
Expandable details
You can make rows expandable with additional content through details, which takes an Python or JavaScript render function. See Custom Rendering for details on how to use render functions.
Render details with Python
import htmltools
Reactable(
pl_penguins,=lambda row_info: htmltools.div(
details"Details for row: ", row_info.row_index, htmltools.pre("nice")
), )
Render details with JS
The details column can be customized by providing a Column()
instead. This can be used to add a column name, render HTML content, or change the column width:
= JS(
js_details """function(rowInfo) {
return `Details for row: ${rowInfo.index}` +
`<pre>${JSON.stringify(rowInfo.values, null, 2)}</pre>`
}"""
)
Reactable(
pl_penguins,=Column(
details="more",
name=True,
html=60,
width=js_details,
details
), )
Nested tables
With Python render functions, you can render HTML tags, Widgets (e.g. ipywidget, anywidget), and even nested tables:
from htmltools import HTML, div
= {
sub_tables =True).to_widget(), style="padding: 1rem")
g: div(Reactable(df, outlinedfor g, df in pl_penguins.group_by("species", "island")
}
= pl_penguins.select(["species", "island"]).unique()
df_uniq
Reactable(
df_uniq,=lambda row_info: list(sub_tables.values())[row_info.row_index],
details )
Conditional row details
Python render functions support conditional rendering. If a render function returns None, the row won’t be expandable:
= pl.DataFrame({"x": [1, 2, 3], "y": ["a", "b", "c"]})
sub_frame
Reactable(5],
pl_penguins[:=lambda row_info: (
details=False).to_widget() if row_info.row_index in [2, 4] else None
Reactable(sub_frame, full_width
), )
Multiple row details
You can add details
to individual columns, and even show multiple details for a row:
= pl.DataFrame({"x": [1, 2, 3], "y": ["a", "b", "c"]})
sub_frame
Reactable(5],
pl_penguins[:=lambda row_info: (
details=False).to_widget() if row_info.row_index in [2, 4] else None
Reactable(sub_frame, full_width
),=[
columns
Column(id="bill_length_mm",
=lambda row_info: f"bill_length_mm: {pl_penguins[row_info.row_index, 'bill_length_mm']}",
details
),
Column(id="bill_depth_mm",
format=ColFormat(digits=1),
=JS(
details"""
function(rowInfo) {
return 'bill_depth_mm: ' + rowInfo.values['bill_depth_mm']
}
"""
),
),
], )
Default expanded rows
You can expand all rows by default by setting default_expanded=True
:
Reactable(12],
pl_penguins[:=4,
default_page_size=lambda indx: f"Details for row: {indx}",
details=True,
default_expanded )