【问题标题】:Python Dash how to create two column table?Python Dash 如何创建两列表?
【发布时间】:2020-04-29 16:20:38
【问题描述】:

我正在尝试为 Plotly Dash webapp 创建一个表。

我想从数据框中的数据创建下表(两列表,一侧为列名,另一侧为值):

列名 |价值

我正在使用下面的逻辑,但它只是给了我一个包含一列的表,并将值和列名堆叠在同一列中。

  return html.Table(
        # Header
        [html.Tr([html.Tr(col) for col in dataframe.columns])] +

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

对于那些熟悉 html 的人来说,这是我想要做的:

<table>
<tr>
<td>Column Name:</td>
<td>Values:</td>
</tr>
</table>

@brokenfoot 我尝试使用您的示例,但它告诉我逗号是语法错误:

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

【问题讨论】:

    标签: python plotly-dash


    【解决方案1】:

    这就是最终对我有用的东西:

     fig = go.Figure(data=[go.Table(
        # header=dict(values=['A Scores', 'B Scores'],
        #             line_color='darkslategray',
        #             fill_color='lightskyblue',
        #             align='left'),
        cells=dict(values=[dataframe.columns.values.tolist(), # 1st column
                           df_cohort_3.iloc[0:1].values.tolist()[0]  # 2nd column
                          ],
                   line_color='darkslategray',
                   fill_color='lightcyan',
                   align='left'))
                            ])
        #fig.update_layout(width=500, height=600)
        return fig.show()
    

    【讨论】:

      【解决方案2】:

      您可以通过以下方式传递标头数据:

      html.Th()
      

      和实际的表格数据:

      html.Td()
      

      示例用法:

       ... 
                  html.Table(className='table',
                      children = 
                      [
                          html.Tr( [html.Th('Attribute'), html.Th("Value")] )
                      ] +
                      [
                          html.Tr( [html.Td('OS'),         html.Td('{}'.format(get_platform()))] ),
                          html.Tr( [html.Td('#CPUs'),      html.Td('{}'.format(ps.cpu_count()))] ),
                          html.Tr( [html.Td('CPU Clock'),  html.Td('{} MHz'.format(int(ps.cpu_freq().current)))] ),
                          html.Tr( [html.Td('RAM'),       html.Td('{} GB'.format(ps.virtual_memory().total >> 30))] ),
                          html.Tr( [html.Td('#processes'), html.Td('{}'.format(len(ps.pids())))] ),
                      ]
                  ),
      . . .
      

      您可以查看以下文件以获取 html 表格、图表用法:
      https://github.com/tarun27sh/dash-on-heroku/blob/master/app.py

      编辑: 你可以试试(未测试!):

      html.Table(className='table',
          children = 
          [
              html.Tr( [html.Th(col) for col in dataframe.columns] )
          ] +
          [
              for i in range(min(len(dataframe), max_rows)):
                  html.Tr( [html.Td('{}' .format(dataframe.iloc[i][col])) for col in dataframe.columns])
          ]
      ),
      

      【讨论】:

      • 这很有道理,我对python有点新;我将如何修改使用循环的上述逻辑?
      • html.Tr( [html.Th(col) for col in dataframe.columns] ] ) ^ SyntaxError: invalid syntax
      • 根据您的建议尝试了上述另一种方法...仍然出现语法错误..有什么想法吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-08-03
      • 1970-01-01
      • 2020-11-06
      • 1970-01-01
      • 2011-06-10
      • 2016-11-19
      • 2014-02-11
      相关资源
      最近更新 更多