How to pin static dash ag grid columns in dynamic dataframe

I have a dataframe that has some static columns and some dynamic columns (that can be change based on source data).

For example:

import pandas as pd
import numpy as np
import dash_ag_grid as dag
from dash import Dash, html, dcc

#create DataFrame
df = pd.DataFrame({'name': ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'g'],
                   'school': ['A', 'A', 'A', 'B', 'B', 'B', 'C', 'C', 'C'],
                   'points': [2800, 1700, 1900, 1400, 2300, 2600, 5000, 10000, 15000],
                   'rebounds': [5000, 6000, 4000, 7000, 14000, 12000, 9000, 3000, 9000],
                   'assists': [10000, 13000, 7000, 8000, 4000, 5000, 8000, 10000, 7000]})

In this case name and school are static columns and points, rebounds, assists are dynamic columns (in some case dataframe just have points). I want to pin name and school in the left of dash ag grid and show all remaining columns of dataframe but I did not find the way.

I tried as below:

import pandas as pd
import numpy as np
import dash_ag_grid as dag
from dash import Dash, html, dcc

#create DataFrame
df = pd.DataFrame({'name': ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'g'],
                   'school': ['A', 'A', 'A', 'B', 'B', 'B', 'C', 'C', 'C'],
                   'points': [2800, 1700, 1900, 1400, 2300, 2600, 5000, 10000, 15000],
                   'rebounds': [5000, 6000, 4000, 7000, 14000, 12000, 9000, 3000, 9000],
                   'assists': [10000, 13000, 7000, 8000, 4000, 5000, 8000, 10000, 7000]})

app = Dash(__name__)
app.layout = html.Div([
    dag.AgGrid(
        id="format-specifiers-example",
        style={"height": "500px", "width": "100%"},
        columnDefs=[{'field':'name', 'pinned':'left'}, {'field':i} for i in df.columns[1:]],
        rowData=df.to_dict("records"),
        columnSize="sizeToFit")],
    style={"margin": 20},
)

if __name__ == "__main__":
    app.run(debug=False)

But it raised: SyntaxError: invalid syntax.
I tried columnDefs=[{'field':'name', 'pinned':'left'}, {'field':i} for i in df.columns if i not in ['name'] but it not worked too.

What should I do in this case. Thank you.

try changing the columnDefs to:


 columnDefs=[{'field':'name', 'pinned':'left'}] +[ {'field':i} for i in df.columns[1:]],
2 Likes

Thank you Ann, it worked.