【问题标题】:Styled pandas dataframe in Dash-PlotlyDash-Plotly 中的样式化 pandas 数据框
【发布时间】:2021-08-10 11:04:58
【问题描述】:

我有 pandas 数据框,并且正在使用“df_styled = df.style.apply(...)”方法方便地设置数据框中的值的样式(在 Jupyter Notebook 中)。 但是,在使用 Dash-Plotly 仪表板时,我无法显示这种样式的输出。

有人可以提出任何建议吗?

我已经尝试过 df_styled.render() 方法,但这在 Dash-dashboard 上显示了我的样式数据框的字符串表示,而不是 HTML 输出(正如我在 Jupyter 笔记本中得到的那样)

【问题讨论】:

    标签: python pandas jupyter-notebook plotly-dash


    【解决方案1】:

    我会推荐下面的,而不是直接使用 Pandas 的样式化组件

    1. 将您的数据框转换为绘图数据表
    2. 根据 plotly-dash 文档格式化 dash 数据表。

    示例代码如下

    
    
    mytable = dash_table.DataTable(
        id='table1',
        columns=[{"name": i, "id": i,'type':'numeric', 'format': {'specifier': ',.0f'}}  if i not in ['phone_number'] else {"name": i, "id": i} for i in df.columns],
        data=df.to_dict('records'),
        style_as_list_view=True,
        style_cell={'padding': '2px'},
        page_action='none',
        style_table={'height': '600px', 'overflowY': 'auto'},
        style_cell_conditional=[
        {
            'if': {'column_id': 'phone_number'},
            'textAlign': 'center'
        },
    ],
        style_data_conditional=[
        {
            'if': {'row_index': 0},
            'backgroundColor': '#FFF2CC',
        },
    ],
        style_header={
            'backgroundColor': 'white',
            'fontWeight': 'bold',
            'textAlign': 'center'
        },
    )
    
    

    【讨论】:

    • 感谢您的回复。欣赏它。我和以前一样,我有一个适用于 Jupyter 的工作解决方案,并使用样式化的数据框进行流式照明)。对于 Dash-Plotly,我希望减少转换的开销。如果我想要实现的目标没有成功,那么您的解决方案可能就是我可能考虑的解决方案。如果出现适用于我现有代码的任何解决方案,我将等待一段时间。
    猜你喜欢
    • 2022-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-12
    • 2021-02-25
    • 1970-01-01
    相关资源
    最近更新 更多