Search code examples
pythondatatableformattingplotly-dash

Python Dash formatting with unknown column names


If I have a Dash table structure like below where it's creating column names from the dataframe columns names. how do I add formatting to these columns? All the examples I see have fixed column names.

dash_table.DataTable(
    id='table',
    columns=[{"name": i, "id": i} for i in df2.columns],
    data=df2.to_dict('records'),
    style_data_conditional=style_data_conditional,
    )

I'm struggling on how to add column formatting to the columns definition above.


Solution

  • You can set the formats like this:

    columns = [
        dict(id=i, name=i, type='numeric', format=FormatTemplate.percentage(2))
        for i in df2.columns
    ]
    

    Taken from this page