代码之家  ›  专栏  ›  技术社区  ›  mikelowry

Python Dash如何创建两列表?

  •  1
  • mikelowry  · 技术社区  · 5 年前

    我正试图为Plotly Dash网络应用程序创建一个表。

    根据数据帧中的数据,我想创建下表(两列表,一边是列名,另一边是值):

    列名|值

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

      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))
            ]
                         )
    
    1 回复  |  直到 5 年前
        1
  •  1
  •   brokenfoot    5 年前

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

    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])
        ]
    ),
    
        2
  •  1
  •   mikelowry    5 年前

    这就是最终为我工作的东西:

     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()