我是 dash 的新手,并试图将我用函数创建的表格居中对齐。我已经从 SQL DB 中获取的表数据。在破折号布局中,我尝试为表格本身使用样式,但它不起作用。样式仅适用于页面上的文本。Df 是我没有发布的数据框,因为它与样式无关。我需要在布局中应用样式还是在函数中创建表格时应用样式?
非常感谢!
def generate_table(dataframe, max_rows=1000):
return html.Table([
html.Thead(
html.Tr([html.Th(col) for col in dataframe.columns])
),
html.Tbody([
html.Tr([
html.Td(dataframe.iloc[i][col]) for col in dataframe.columns
]) for i in range(min(len(dataframe), max_rows))
])
])
app.layout = html.Div(children=[
html.H2(
children='Products',
style={
'textAlign' : 'center'
}
),
generate_table(df)
])
一个简单的解决方法是添加style={'marginLeft': 'auto', 'marginRight': 'auto'}
到html.Table
. 所以你的功能会变成:
def generate_table(dataframe, max_rows=1000):
return html.Table([
html.Thead(
html.Tr([html.Th(col) for col in dataframe.columns])
),
html.Tbody([
html.Tr([
html.Td(dataframe.iloc[i][col]) for col in dataframe.columns
]) for i in range(min(len(dataframe), max_rows))
])
], style={'marginLeft': 'auto', 'marginRight': 'auto'})
一个更好的主意是className="center"
将以下内容添加并添加到.css
文件中:
.center {
margin-left: auto;
margin-right: auto;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句