在一个单元格中用多行打印虚线 table

Print table in plotly dash with multiple lines in one cell

目前我有一个 pandas 数据框:

df = pd.DataFrame({
    "date": ["20210613", "20210614", "20210615"],
    "user": ["A\nB", "C", "D"],
    "machine" : [1, 0, 3]
})

我想知道是否有任何方法可以像这样将此 table 打印到我的 dash 应用程序:

无论用纯文本打印成dcc.Textarea还是dash_table.DataTable都可以。

目前我还没有想出一个好的方法来实现这个,非常感谢。

您可以在 DataTable 中使用 DataTable 上的 style_cell 属性,如下所示:

import dash
import dash_table
import pandas as pd

df = pd.DataFrame(
    {
        "date": ["20210613", "20210614", "20210615"],
        "user": ["A\nB", "C", "D"],
        "machine": [1, 0, 3],
    }
)

app = dash.Dash(__name__)

app.layout = dash_table.DataTable(
    id="table",
    columns=[{"name": i, "id": i} for i in df.columns],
    data=df.to_dict("records"),
    style_cell={"whiteSpace": "pre-line"},
)

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

You can make the datatable cells break when they encounter the \n character by setting the white-space CSS attribute for the cells in the table

我在 this thread

上找到了这个答案