Formatting with functions (enhancement) for dash_table

Hi

I have noticed that the cell formatting is handled by a Format class which is nice to have but would it be possible to provide a function or lambda function to transform the data in cell to be viewed differently?

For example,

from dash_table.Format import Format

will import the Format class and then is is used in

    dash_table.DataTable(
        data=df.to_dict('records'),
        columns=[
            {
                'name': i,
                'id': i,
                'type': 'numeric',
                'format': Format(
                    nully='N/A'
                )
            } for i in df.columns
        ],
        editable=True
    )

Would it be possible to provide a function there that accepts the cell value as input and returns the formatted string to the view of the cell? I was thinking about date display.

    import datetime

    dash_table.DataTable(
        data=df.to_dict('records'),
        columns=[
            {
                'name': i,
                'id': i,
                'type': 'numeric',
                'format': (lambda x: datetime.strptime(str(x),'%Y%m%d').strftime('%d-%b-%Y'))
                )
            } for i in df.columns
        ],
        editable=True
    )

In this example with no error trapping it should take an int or string YYYYmmdd and reformat the date for presentation. The nice side benefit is that from a sorting perspective the underlying data will be ordered timewise correctly. Adding arguments from the local context would be also possible where the function is a lambda function. If this is already possible then I have not been able to find it in the documentation.

This approach could be used for many different types of custom transformation of the data where we don’t want to change the underlying data model but would like to display differently.

This could simplify the formatting of the data while giving more flexibility to the designer.

Any interest Plotly?

Thanks

M

While this would be a nice feature from a user perspective, i don’t think that it will fit in the general data model. Objects in Plotly are constructed as dicts/lists of strings, which makes it easy to pass them to the JavaScript layer. I don’t see how this could work for a general function handle, but i would love to be proved wrong :slight_smile:

You are right, Emil, if the Format/FormatTemplate class is only used to build a ‘formatting’ string that is passed to javascript then we might not be able to do it. However, if the table data is massaged before it is passed to the javascript layer then there is a chance it could be implementable. The python approach is easier for me to manipulate, but an alternative would be to provide a javascript function to render the data passed?

Just thinking aloud.