PablocFonseca / streamlit-aggrid

Implementation of Ag-Grid component for Streamlit
https://pypi.org/project/streamlit-aggrid/
MIT License
1.05k stars 199 forks source link

Question: Is there a way for the JsCode to call python code? #255

Open mkleinbort-ic opened 6 months ago

mkleinbort-ic commented 6 months ago

Conceptual question - mainly geared towards the master-detail feature in AgGrid.

Can the getDetailRowData parameter as seen here

"detailCellRendererParams": {
        "getDetailRowData": JsCode(
            """
                function (params) {
                    params.successCallback(params.data.detailForAgGrid);
                }"""
}

Call a function in python to return the json for display?

Currently I am wrapping the detail data as a json - but it's a lot of data to have loaded into memory all at once.

The ideal would be something like:



def get_relevant_data(params)->'json':
     ... 
     return detailForAgGrid

"getDetailRowData": JsCode(
     """
     function (params) {
         params.successCallback({get_relevant_data(params)});
    }"""
zeklan commented 4 months ago

Have you managed to resolve this? I'm in need of a similar solution

mkleinbort-ic commented 4 months ago

No; you can fake this for a while by nesting the data you need within the cell's value, but I've not figured out how to call python code from within the JS.

I imagine you could set up a FastAPI server to handle this - but I've not tried.

zeklan commented 4 months ago

I see, have you got an example of this method that I could refer to for implementation? would appreciate a lot since there aren't a lot of python x ag-grid on the web...

zeklan commented 4 months ago

nvm, I was able to figure the workaround out. Leaving snippet here for anyone else who ends up in this thread looking for the same sol'n:

GET_DETAIL_ROW_DATA = '''function (params) {params.successCallback(params.data.cities)}''' master_grid_options = {..., 'detailCellRendererParams' : { 'getDetailRowData' : GET_DETAIL_ROW_DATA }, ...} master_grid = Grid(grid_data=rowData, grid_options=master_grid_options)

where 'rowData' is the input data in the example code snippet of Dash's AG-Grid page https://dash.plotly.com/dash-ag-grid/enterprise-master-detail.