from reactable import Reactable, Column, JS, embed_css
from reactable.data import cars_93, sleep
embed_css()
Javascript styling
Cell style
= JS(
js_style """function(rowInfo) {
const value = rowInfo.values['extra']
let color
if (value > 0) {
color = '#008000'
} else if (value < 0) {
color = '#e00000'
} else {
color = '#777'
}
return { color: color, fontWeight: 'bold' }
}"""
)
Reactable(6, :],
sleep[:=[
columns
Column(id="extra",
=js_style,
style
)
], )
Row style
Reactable(6, :],
sleep[:=JS(
row_style"""function(rowInfo) {
if (rowInfo.values['extra'] < -1) {
return { background: 'rgba(0, 0, 0, 0.05)' }
}
}"""
),=JS(
row_class"""function(rowInfo) {
if (rowInfo.values['extra'] < -1) {
return 'bold'
}
}"""
), )
Metadata
You can pass arbitrary data from Python to JavaScript style functions using the meta
argument in reactable().
meta should be a named list of values that can also be JS() expressions or functions. Custom metadata can be accessed from JavaScript using the state.meta property, and updated using updateReactable() in Shiny or Reactable.setMeta() in the JavaScript API.
Use custom metadata to:
Simplify JavaScript style functions that need access to data outside of the table Dynamically change how data is styled without rerendering the table Share JavaScript code or data between different style functions
from IPython.display import display
= cars_93[:6, ["manufacturer", "model", "type", "price", "mpg_city"]]
cars
= JS(
js_mpg_background """function(rowInfo, column, state) {
const { showColors, mpgColor } = state.meta
if (showColors) {
return {
backgroundColor: rowInfo.values[column.id] > 20 ? mpgColor : 'transparent'
}
}
}
"""
)
= Reactable(
bb
cars,=[
columns
Column(id="mpg_city",
=js_mpg_background,
style
)
],={
meta# yellow
"mpgColor": "#ff9f1a",
"showColors": True,
},="cars-colors-table",
element_id
)
import htmltools as ht
= ht.TagList(
clicker
ht.tags.label(input(
ht.tags.type="checkbox",
=None,
checked="Reactable.setMeta('cars-colors-table', function(prevMeta) { return { showColors: !prevMeta.showColors } })",
onclick
),"Show color scale",
),
)
display(bb) clicker