使用DASH在一页上显示两个数据帧

问题描述 投票:-2回答:1

我想创建第二个数据框,然后在页面上显示两个表。现在,它仅显示一个。好像dash_bootstrap_components可以分割屏幕,但找不到包含多个表的示例

import dash
import dash_html_components as html
import pandas as pd

#would like to add a second dataframe here
df1 = pd.read_sas('C:\PY_DASH\file1.sas7bdat', format = 'sas7bdat', encoding='utf-8' )

def generate_table(dataframe, max_rows=7):
    return html.Table(
        # Header
        [html.Tr([html.Th(col) for col in dataframe.columns])] +

        # Body
        [html.Tr([
            html.Td(dataframe.iloc[i][col]) for col in dataframe.columns
        ]) for i in range(min(len(dataframe), max_rows))]
    )

external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

app = dash.Dash(__name__, external_stylesheets=external_stylesheets)

#I would like to add the second table here
app.layout = html.Div(children=[
    html.H1(children='QA Test: Test 0-1: Output Files Record Count Comparison'),
    generate_table(df1)
])

if __name__ == '__main__':
    app.run_server(debug=True, port=8000)
python plotly-dash
1个回答
0
投票

尝试分离出生成每个表的代码块。然后将它们组装在dcc容器中,然后可在您的布局中使用。这使得以后也可以轻松调试:)。下面是一些示例代码,可让您了解我的意思。

首先生成表1的部分。在此范围内,您可以使用函数generate_table

one_row = html.Tr([html.Td(<content>, id="row1" )])
t_head = [
      html.Thead(html.Tr([html.Th("Table1")]))
]
table1_section = dbc.Card(children=[
    dbc.CardHeader(html.H5("Table1 header", id="t_head")),
    dbc.CardBody([
      dbc.Table(t_head + [html.Tbody([one_row])])
    ])
])

类似地设为table2_section然后根据需要将其组装为dbc.Row()

最后,将它们全部放到dbc.Container中,例如:

page_content = dbc.Container([
      table1_section,
      table2_section,
      somegraph_section
], fluid=True)

希望这会有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.