import ipyaggrid
import json
import pandas as pd
import solara
from typing import cast
dfm = pd.read_json("OlympicWinners.json")
column_defs = [{'field': c} for c in dfm.columns[:]]
grid_options = {
'columnDefs' : column_defs,
}
agGridLicenseKey = "community"
buttons = [
{'name':'Expand All', 'action':'''gridOptions.api.expandAll();'''},
{'name':'Collapse All', 'action':'''gridOptions.api.collapseAll();'''},
]
grid_options = {
'columnDefs' : column_defs,
}
@solara.component
def AgGrid(df, grid_options, height, menu={}, quick_filter=False):
"""Convenient component wrapper around ipyaggrid.Grid"""
def update_df():
widget = cast(ipyaggrid.Grid, solara.get_widget(el))
widget.grid_options = grid_options
# menu will be
widget.menu_in = menu
widget.update_grid_data(df) # this also updates the grid_options, and height
# so we need to set height afterwards if we want to do more than pixels/units
# and allow to set 100% etc
widget.height = height
# when df changes, grid_data will be update, however, ...
el = ipyaggrid.Grid.element(grid_data=df, grid_options=grid_options, quick_filter=quick_filter, license=agGridLicenseKey)
# grid_data and grid_options are not traits, so letting them update by reacton/solara has no effect
# instead, we need to get a reference to the widget and call .update_grid_data in a use_effect
solara.use_effect(update_df, [df, grid_options])
return el
@solara.component
def Page():
with solara.Card("ag-grid in solara"):
AgGrid(dfm, grid_options, height="1200px", menu={'buttons':buttons}, quick_filter=True)