import polars as pl
import htmltools as html
from reactable.data import cars_93
from reactable import Reactable, reactable, embed_css
from reactable.models import Column, ColInfo, CellInfo, HeaderCellInfo, RowInfo, JS
embed_css()
= cars_93[20:25, ["manufacturer", "model", "type", "price"]] data
Javascript formatters
Cell
= Column(
column =JS(
cell"""
function(cellInfo, state) {
// input:
// - cellInfo, an object containing cell info
// - state, an object containing the table state (optional)
//
// output:
// - content to render (e.g. an HTML string)
return `<div><b>${cellInfo.value}</b></div>`
}
"""
),=True, # to render as HTML
html
)
={"manufacturer": column}) Reactable(data, columns
cellInfo
properties
Headers
= Column(
column =JS(
header"""
function(column, state) {
// input:
// - column, an object containing column properties
// - state, an object containing the table state (optional)
//
// output:
// - content to render (e.g. an HTML string)
return `<div><em>${column.name}</em></div>`
}
"""
),=True, # to render as HTML
html
)
Reactable(
data,={"price": column},
columns )
column
properties
Expandable row details
Reactable(
data,=JS(
details"""
function(rowInfo, state) {
// input:
// - rowInfo, an object containing row info
// - state, an object containing the table state (optional)
//
// output:
// - content to render (e.g. an HTML string)
return `<div>Details for row: ${rowInfo.index}</div>`
}
"""
), )
rowInfo
properties
Aggregate cell calculation
Reactable(
data,="type",
group_by=[
columnsid="price", aggregate="max"),
Column(
Column(id="model",
=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(', ')
}
"""
),
),
], )
Aggregated cell
# Render aggregated value as a comma-separated list of unique values
= JS(
agg_type """
function(cellInfo) {
const values = cellInfo.subRows.map(function(row) { return row['Type'] })
const unique = values.reduce(function(obj, v) { obj[v] = true; return obj }, {})
return Object.keys(unique).join(', ')
}"""
)
# Render aggregated cell as currency
= JS(
agg_price """
function(cellInfo) {
return cellInfo.value.toLocaleString('en-US', { style: 'currency', currency: 'USD' })
}"""
)
Reactable(
data,="manufacturer",
group_by={
columns"model": Column(aggregate="unique"),
"type": Column(aggregated=agg_type),
"price": Column(aggregate="sum", aggregated=agg_price),
}, )