plotly / dash-table

OBSOLETE: now part of https://github.com/plotly/dash
https://dash.plotly.com
MIT License
420 stars 74 forks source link

[BUG] DashTable formatting fails sillently if type is not passed on columns #866

Open GabrielSimonetto opened 3 years ago

GabrielSimonetto commented 3 years ago

Describe your context

$ pip list | grep dash
dash                 1.18.1
dash-core-components 1.14.1
dash-html-components 1.1.1
dash-renderer        1.8.3
dash-table           4.11.1
$ cat /etc/lsb-release
DISTRIB_ID=ManjaroLinux
DISTRIB_RELEASE=20.2
DISTRIB_CODENAME=Nibia
DISTRIB_DESCRIPTION="Manjaro Linux"

Browser:
Chromium: Version 87.0.4280.66 (Official Build) Arch Linux (64-bit)

Describe the bug

Formatting doesn't work if type is not passed along, the problematic part for me is that it also doesn't raise an error, so in the midst of a more complex context it would be difficult to assert why it is failing.

import pandas as pd
import numpy as np
import dash
from dash_table import DataTable, FormatTemplate
from dash_table.Format import Format, Scheme, Symbol

df = pd.DataFrame(np.random.random(6).reshape(3, 2) * 50, columns=['money', 'percentage'])

app = dash.Dash(__name__)

app.layout = DataTable(
    id='table',
    columns=[
        {
            "name": "money", 
            "id": "money", 
            "format": FormatTemplate.money(2),
            # "type": "numeric",  # uncommenting this line makes the formatting work
        },
        {
            "name": "percentage", 
            "id": "percentage", 
            "format": FormatTemplate.percentage(2),
            # "type": "numeric",  # uncommenting this line makes the formatting work

            # This also fails
            # so it shouldn't be a FormatTemplate problem.
            # "format": Format(precision=2, scheme=Scheme.fixed, symbol=Symbol.yes, symbol_suffix='%'),
        },
    ],
    data=df.to_dict('records'),
)

if __name__ == '__main__':
    app.run_server(debug=True)

Expected behavior

I believe some sort of error is more than enough to help the developer, inferring is probably possible, but that could create even more silent bugs.

Screenshots

Screenshot_20210118_133636

GabrielSimonetto commented 3 years ago

I'll try my hand solving this issue later on the week, if something goes terribly wrong I'll post something here.

GabrielSimonetto commented 3 years ago

ok apparently this issue belongs on https://github.com/plotly/dash-table, sorry about that, should I just make the issue there?

alexcjohnson commented 3 years ago

@GabrielSimonetto I moved the issue over, thanks (and thanks for reporting!) Would be worth having a bit of a discussion about how best to solve this. From a quick look my impression is if you provide any sub-fields in format besides nully, this should be an unambiguous signal to infer type: 'numeric' if not overridden, but perhaps @Marc-Andre-Rivet can say more?

I don't think we do much dynamic default inference here, but in plotly.js we have rules like this all over the place...