【发布时间】:2018-07-24 13:48:56
【问题描述】:
我有这个功能码:
def make_dash_table(df):
table = []
for index, row in df.iterrows():
html_row = []
for i in range(len(row)):
html_row.append(html.Td([row[i]]))
table.append(html.Tr(html_row))
return table
它为 Dash Framework (Python) 创建了一个表,但是当我在 Python 上调用它时它不显示标题(第一个 .csv 行)。对于某些表格,它被定制为显示如下字幕:
modifed_perf_table.insert(
0, html.Tr([
html.Td([]),
html.Td(['Cumulative'], colSpan=4, style={'text-align': "center"}),
html.Td(['Annualised'], colSpan=4, style={'text-align': "center"})
], style={'background': 'white', 'font-weight': '600'}
)
)
但我想让表格显示 csv 标题(第一行)。 我需要更改的地方:是在此代码上还是在 CSS 上?
完整代码如下:
df_perf_summary = pd.read_csv('17530.csv', encoding='latin-1')
df_perf_summary.head()
def make_dash_table(df):
table = []
for index, row in df.iterrows():
html_row = []
for i in range(len(row)):
html_row.append(html.Td([row[i]]))
table.append(html.Tr(html_row))
return table
modifed_perf_table = make_dash_table(df_perf_summary)
modifed_perf_table.insert(
0, html.Tr([
html.Td([]),
html.Td(['Cumulative'], colSpan=4, style={'text-align': "center"}),
html.Td(['Annualised'], colSpan=4, style={'text-align': "center"})
], style={'background': 'white', 'font-weight': '600'}
)
)
html.Div([
html.Div([
html.Div([
html.H6("#####",
className="gs-header gs-table-header padded"),
html.Table(modifed_perf_table, className="reversed"),
], className="eight columns"),
], className="row "),
external_css = ["https://cdnjs.cloudflare.com/ajax/libs/normalize/7.0.0/normalize.min.css",
"https://cdnjs.cloudflare.com/ajax/libs/skeleton/2.0.4/skeleton.min.css",
"//fonts.googleapis.com/css?family=Raleway:400,300,600",
"https://cdn.rawgit.com/plotly/dash-app-stylesheets/5047eb29e4afe01b45b27b1d2f7deda2a942311a/goldman-sachs-report.css",
"https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css"]
csv如下,另存为17530.csv
Col1,Since Launch,,March,April,May,1st Y,2nd Y,3rd Y
,Cum (#),Cum (%),Q2Y18,Q3Y18,Q4Y18,1stYJun19,2ndYJun20,3rdYJun2021
SiteA,15.96,,0.1,0.27,0.27,0.87,0.51,0.43
SiteB,20.09,,0.06,0.21,0.21,2.24,'-1.48,1.46
SiteC,15.7,,'-0.03,'-0.09,'-0.09,'-0.32,'-0.09,0.04
【问题讨论】:
-
你能分享在CSV文件中读取的代码来制作
df吗? -
@PaSTE,在帖子中添加了完整代码,请检查!谢谢
-
看起来解决方案可以来自这部分 CSS (cdn.rawgit.com/plotly/dash-app-stylesheets/…) table.tiny-header tr:first-child{ font-size: 8px; } .columns{ 左边距:0 !important; } .row > .columns:not(:first-child){ padding-left: 20px; }
标签: python pandas plotly-dash