import solara
import solara.lab
from ipyaggrid import Grid
@solara.component
def AgGrid(grid_data, grid_options, dark_mode, **kwargs):
theme = 'ag-theme-balham' if not dark_mode else 'ag-theme-balham-dark'
def update_data():
widget = solara.get_widget(el)
if widget is not None:
widget.grid_options = grid_options
widget.update_grid_data(grid_data)
print(f'Set theme to {theme}')
widget.theme = theme
solara.Text(f'dark_mode = {dark_mode} {theme}')
el = Grid.element(
grid_data=grid_data,
grid_options=grid_options,
theme=theme,
**kwargs,
).key(f"theme-{theme}")
solara.use_effect(update_data, dependencies=[grid_data, grid_options, dark_mode])
@solara.component
def Page():
data = solara.use_reactive([
{"make": "Toyota", "model": "Celica", "price": 35000, "rowColor": True},
{"make": "Ford", "model": "Mondeo", "price": 32000},
{"make": "Porsche", "model": "Boxster", "price": 72000}
])
dark_mode = solara.use_reactive(True)
solara.Switch(label="Dark Mode", value=dark_mode)
with solara.lab.Tabs(lazy=True):
with solara.lab.Tab('One'):
AgGrid(
grid_data=data.value,
grid_options={
"columnDefs": [
{"headerName": "Make", "field": "make", "sortable": True},
{"headerName": "Model", "field": "model"},
{"headerName": "Price", "field": "price"}
],
},
dark_mode=dark_mode.value
)
with solara.lab.Tab('Two'):
solara.Text(f'dark_mode = {dark_mode}')