在一个Dash表中突出显示差异 – Python

huangapple go评论74阅读模式
英文:

Highlight diferences in a dashtable - python

问题

我是新手学习Dash,尝试为我的数据表格(DashTable)添加样式,以便突出显示每个单元格与同一列中第一行的单元格值不同的情况。

我尝试了类似这样的代码:

import dash
import dash_table
import pandas as pd

df = pd.DataFrame({'A': [1, 2, 3],
                   'B': [4, 5, 6],
                   'C': [7, 7, 7]})

app = dash.Dash(__name__)

app.layout = dash_table.DataTable(
    data=df.to_dict('records'),
    columns=[{'name': i, 'id': i} for i in df.columns],
    style_data_conditional=[
        {
            'if': {
                'filter_query': '{{{column}}}[{row}] != {{{column}}}[0]'.format(column=column, row=row),
            },
            'backgroundColor': 'yellow',
            'color': 'white',
        } for column in df.columns for row in range(1, len(df))
    ]
)

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

但是,我一直收到相同的错误信息,我不太理解:
"DataTable filtering syntax is invalid for query: {A}[1] != {A}[0]"

有人可以帮助我解决吗?非常感谢。

英文:

I am new to dash and trying to style my dashtable so that every cell with a different value from the cell in the same column in the first row gets highlighted.

I tried something like this

import dash
import dash_table
import pandas as pd

df = pd.DataFrame({'A': [1, 2, 3],
                   'B': [4, 5, 6],
                   'C': [7, 7, 7]})

app = dash.Dash(__name__)

app.layout = dash_table.DataTable(
    data=df.to_dict('records'),
    columns=[{'name': i, 'id': i} for i in df.columns],
    style_data_conditional=[
        {
            'if': {
                'filter_query': '{{{column}}}[{row}] != {{{column}}}[0]'.format(column=column, row=row),
            },
            'backgroundColor': 'yellow',
            'color': 'white',
        } for column in df.columns for row in range(1,len(df))
    ]
)

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

... but I keep getting the same Error that I dont really understand:
DataTable filtering syntax is invalid for query: {A}[1] != {A}[0]

Can someone help me figure it out? Thank you in advance

答案1

得分: 1

I don't know exactly why the error happens, it seems you cannot specify {column}[rowindex]..
不清楚为什么会出现错误,似乎无法指定{column}[rowindex]...

Anyway, the following conditional should work:
无论如何,以下条件应该有效:

style_data_conditional=[{
    'if': {
        'column_id': f'{column}',
        'filter_query': f'{{{column}}} != {df[column][0]}',
    },
    'backgroundColor': 'yellow',
    'color': 'white',
} for column in df.columns]
英文:

I don't know exactly why the error happens, it seems you cannot specify {column}[rowindex]..

Anyway, the following conditional should work :

    style_data_conditional=[{
        'if': {
            'column_id': f'{column}',
            'filter_query': f'{{{column}}} != {df[column][0]}',
        },
        'backgroundColor': 'yellow',
        'color': 'white',
    } for column in df.columns ]

huangapple
  • 本文由 发表于 2023年3月9日 20:46:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75684822.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定