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



我想创建第二个数据帧,然后在页面上显示两个表。 现在,这仅显示一个。 看起来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_DASHfile1.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)

尝试分离生成每个表的代码块。然后将它们组装在一个 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)

希望这有帮助。

最新更新