我有多个数据帧想要显示在我的Dash应用程序上. 它们都经历了类似的格式化,所以我想在一个单独的函数中进行仪表表格式化,我向该函数输入要设置样式的数据帧.我在下面try 过了,但在我的仪表盘服务器上出现"加载布局错误"
import dash
from dash import html, dash_table
import pandas as pd
def styletable(dataframe):
# Create a copy of the original DataFrame to avoid modifying it directly
styled_df = dataframe.copy()
COLNAMES = styled_df.columns
indexName = styled_df.reindex().columns[0]
# Convert the styled DataFrame to a Dash DataTable
styled_table = dash_table.DataTable(
columns=[{'name': indexName, 'id': indexName}] + [{'name': col, 'id': col, 'type': 'numeric'} for col in COLNAMES],
data=styled_df,
style_data_conditional=[
{
'if': {'column_id': col, 'filter_query': f'{{col}} < 0'},
'color': 'red'
}
for col in styled_df.columns
],
style_header={'fontWeight': 'bold'}
)
print(styled_table)
return styled_table
app = dash.Dash()
df = pd.DataFrame({
'Name': ['John Doe', 'Jane Doe', 'Mary Smith', 'Peter Jones'],
'Age': [30, 25, 40, 35],
'Salary': [50000, 40000, 60000, 55000]
})
styled_table = styletable(df)
app.layout = html.Div([styled_table])
if __name__ == '__main__':
app.run_server(debug=True)