Py.Cafe

maartenbreddels/

ipyaggrid-solara

ag-Grid using Solara

DocsPricing
  • app.py
  • requirements.txt
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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)