Iterating through columns of Dash_Table

I need to create a bunch of the same style_data_conditional ‘if’ statements. So, I was hoping I could just write one and iterate through the columns key values that match the ‘id’

e.g

   {
                        'if': {
                            'filter_query': '{PSI} > 0',
                            'column_id': 'PSI',
                        },
                            'backgroundColor': 'white',
                            'color': 'black',
                    },

I want the above code but for most of the columns… I tried different way in which to write a for loop within the style_data_conditional property but keep getting syntax errors. Down below I even tried creating a dataframe outside of the app.layout but again I think I’m running into the issues of :

A. Can I even do it this way (I believe I can)
B. Where do I actually insert the (correct) for loop ?

dash_table.DataTable(
                id='projection_table',
                columns=[
                    {
                        'name': 'Well Name',
                        'id': 'Well Name',
                        'type':'any',
                        'editable':True
                    },
                    {
                        'name': 'High PSI',
                        'id': 'PSI',
                        'type':'numeric',
                        'editable':True,
                        'presentation':'dropdown'
                    },
                    {
                        'name': 'TMD',
                        'id': 'TMD',
                        'type':'numeric',
                        'editable':True
                    },
                    {
                        'name': 'TIH Footage',
                        'id': 'TIH Footage',
                        'type':'numeric',
                        'editable':True
                    },
                    {
                        'name': 'Lateral Length',
                        'id': 'LL',
                        'type':'numeric',
                        'editable':True
                    },
                    {
                        'name': 'Plugs',
                        'id': 'Plugs',
                        'type':'numeric',
                        'editable':True
                    },
                    {
                        'name': 'Circ. Intervals',
                        'id': 'Circ. Intervals',
                        'type':'numeric',
                        'editable':True
                    },
                    {
                        'name': '# Static STS',
                        'id': '#',
                        'type':'numeric',
                        'editable':True
                    },
                    {
                        'name': 'Pump Rate',
                        'id': 'Pump Rate',
                        'type':'numeric',
                        'editable':True
                    },
                    {
                        'name': 'Rig Up',
                        'id': 'RU',
                        'type':'numeric',
                        'editable':True,
                        'presentation':'dropdown'
                    },
                    {
                        'name': 'PSI Testing',
                        'id': 'PSI Testing',
                        'type':'numeric',
                        'editable':True
                    },
                    {
                        'name': 'Trip in Hole',
                        'id': 'TIH',
                        'type':'numeric',
                        'editable':True
                    },
                    {
                        'name': 'Mill & Wash',
                        'id': 'MW',
                        'type':'numeric',
                        'editable':True
                    },
                    {
                        'name': 'Surface to Surface',
                        'id': 'STS',
                        'type':'numeric',
                        'editable':True},
                    {
                        'name': 'STS @ TMD',
                        'id': 'STS @ TMD',
                        'type':'numeric',
                        'editable':True
                    },
                    {
                        'name': 'Trip Out of Hole',
                        'id': 'TOOH',
                        'type':'numeric',
                        'editable':True
                    },
                    {
                        'name': 'Rig Over',
                        'id': 'RO',
                        'type':'numeric',
                        'editable':True,
                        'presentation':'dropdown'
                    },
                    {
                        'name': 'Rig Down',
                        'id': 'RD',
                        'type':'numeric',
                        'editable':True,
                        'presentation':'dropdown'
                    },
                    {
                        'name': 'Total Hours on Well',
                        'id': 'Total Hours on Well',
                        'type':'numeric',
                        'editable':True
                    },
                    {
                        'name': 'Total Days',
                        'id': 'Total Days',
                        'type':'numeric',
                        'editable':True
                    }
                    ],
                style_cell={'text-align':'center','minWidth': '150px', 'width': '180px', 'maxWidth': '180px'},
                style_data={  'backgroundColor': 'rgb(50, 50, 50)','color': 'white'},
                style_header={'fontWeight':'bold', 'backgroundColor':'#CC0033','color':colors['text']},
                dropdown={
                    'PSI': {
                        'options': [
                            {'label': 'Yes', 'value': 45},
                            {'label': 'No', 'value': 55}
                            ]
                        },
                    'RU': {
                        'options': [
                            {'label': '1st', 'value': 9},
                            {'label': 'N/A', 'value': 0}
                            ]
                        },
                    'RO': {
                        'options': [
                            {'label': 'N/A', 'value': 0},
                            {'label': 'RO', 'value': 5}
                            ]
                        },
                     'RD': {
                        'options': [
                            {'label': 'Last', 'value': 9},
                            {'label': 'N/A', 'value': 0}
                            ]
                        },
                    },
                style_data_conditional=[
                    {
                        'if': {
                            'filter_query': '{} > 0'.format(i),
                            'column_id': '{}'.format(i),
                        }for i in range(len(columnNames))for value in columnNames[i],
                            'backgroundColor': 'white',
                            'color': 'black',
                    },
                     {
                        'if': {
                            'filter_query': '{PSI} > 0',
                            'column_id': 'PSI',
                        },
                            'backgroundColor': 'white',
                            'color': 'black',
                    },
                    {
                        'if': {
                            'state': 'active'  # 'active' | 'selected'
                        },
                            'backgroundColor': 'rgba(204, 0, 51, 0.3)',
                            'border': '1px solid rgb(204,0,51)'
                    },
                    {
                        'if':{
                            'row_index': 0,
                            'column_id': 'Rig Up',
                        }, 'cell-selected': 9
                    }
                ],

Hi,

You can certainly do it.

You want to create one dictionary with the condition per column, therefore your for loop is misplaced. Besides, because you are using .format, you should add escaped curly brackets around the column name you are replacing. So:

style_data_conditional=[
    {
        'if': {
            'filter_query': '{{{}}} > 0'.format(col),
            'column_id': '{}'.format(col),
         },
         'backgroundColor': 'white',
         'color': 'black',
    }
    for col in columnNames
] + [ 
    # .... other conditions
]

Note that there are 3 curly brackets pairs, one to replace col in the string and the other two to add “{ }” around it, as the filter syntax require.

Please take a look on the recipes for more examples.