✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
🧬 Learn how to build RNA-Seq data apps with Python & Dash. Register for the May 20 Webinar!

2 digit after decimal float datatable

Hey,
I would like to show only the two digits for float in datable but I’m unable to do it. I used :

from dash_table.Format import Format

and tried to play with different format (precision, decimal_delimiter, …) but I’m unable to make it work.

I’m loading a dataframe and the technique I use is to replace columns containing float to the same column with only two digits. But this technique has two drawbacks

  • I want to keep the precision of the numbers so I need to copy the dataframe to get one version with two digits, one with all the digits, I would like to avoid this.
  • The int are also converted to 2 digits numbers

Is there a method with datatable that I could use ?

Thank you

Hi @Feitan

See if this works for you:

from dash_table.Format import Format, Group, Scheme
app.layout = dash_table.DataTable(
    id="table",
    columns=[
        {
            "name": "A",
            "id": "A",
            "type": "numeric",
            "format": Format(group=Group.yes, precision=2, scheme=Scheme.fixed),
        },
    ],
    data=df.to_dict("records"),
)

Updated my earlier post to be more consistent with documentation: https://dash.plotly.com/datatable/typing

Hello @AnnMarieW,

Thank you, it’s working !
I tried to put this Format in style conditional (cell or data) but it was not working. And I didn’t think about putting it in columns.
So I will add some condition to get the result I expected but it’s working.

Here is a minimal example for people that needs it

import dash_table
import dash
from dash_table.Format import Format
import pandas as pd

df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/solar.csv')
df['Average MW Per Plant'] = df['Average MW Per Plant']/100


app = dash.Dash(__name__)

server = app.server

app.layout = dash_table.DataTable(id='table',
                                data=df.to_dict('records'),
                                columns=[
                                    {"name": i, "id": i,
                                     "type": "numeric", "format": Format(group=",", precision=2, scheme="f")} for i in df.columns
                                ],
                                export_format='csv',
                                export_headers='display',
                                merge_duplicate_headers=True,
                                virtualization=True,
                                page_action='none')

if __name__ == "__main__":
    app.config.suppress_callback_exceptions = True
    app.run_server(debug=True, threaded=True, port=8051)
1 Like