Multiple PivotTables in Dash plotly python

huangapple go评论72阅读模式
英文:

Multiple PivotTables in Dash plotly python

问题

我正在尝试在 Python Dash Plotly 中生成 n 个数据透视表n 是用户输入或者添加一个名为 '添加数据透视表' 的按钮每次单击后生成一个数据透视表我尝试过以下方式

@app.callback(Output("pivottable_div", "children"), [Input("button", "n_clicks")])
def refresh_pivottable(n_clicks):
    for i in range(n_clicks):
        print(n_clicks)
        return [
            html.Div(str(n_clicks)),
            dash_pivottable.PivotTable(data=[["a"], [n_clicks]], cols=["a"])
            if n_clicks % 2 == 1
            else "a",
        ]
英文:

I am trying to generate n pivot tables in Python Dash Plotly. n is the user input. Or add a button 'add pivot table' which generates a pivot table after each click. I tried it this way:

@app.callback(Output("pivottable_div", "children"), [Input("button", "n_clicks")])
def refresh_pivottable(n_clicks):
    for i in range(n_clicks):
        print(n_clicks)
        return [
            html.Div(str(n_clicks)),
            dash_pivottable.PivotTable(data=[["a"], [n_clicks]], cols=["a"])
            if n_clicks % 2 == 1
            else "a",
        ]

But the above code is not working. Please help with this.

答案1

得分: 1

我终于实现了在仪表板上生成多个数据透视表的逻辑。以下代码实现了这一点。

app = dash.Dash(__name__)
app.title = 'My Dash example'

app.layout = html.Div([

    html.Button('添加数据透视表', id='click', n_clicks=0),

    dash_pivottable.PivotTable(
        id='table',
        data=[list(covid_19_data_top_10.columns)] + covid_19_data_top_10.values.tolist(),
        cols=['Month'],
        rows=['Country'],
        rowOrder="key_a_to_z",
        rendererName="Grouped Column Chart",
        aggregatorName="Average",
        vals=["Confirmed"],
    ),

    html.Div(
        id='output'
    )
])

@app.callback(Output('output', 'children'),
              [Input('table', 'cols'),
               Input('table', 'rows'),
               Input('table', 'rowOrder'),
               Input('table', 'colOrder'),
               Input('table', 'aggregatorName'),
               Input('table', 'rendererName'),
               Input('click', 'n_clicks')])
def display_props(cols, rows, row_order, col_order, aggregator, renderer, n_clicks):
    return [
        dash_pivottable.PivotTable(
        id='table'+str(id),
        data=[list(covid_19_data_top_10.columns)] + covid_19_data_top_10.values.tolist(),
        cols=['Month'],
        colOrder="key_a_to_z",
        rows=['Country'],
        rowOrder="key_a_to_z",
        rendererName="Grouped Column Chart",
        aggregatorName="Average",
        vals=["Confirmed"],
    ) for id in range(n_clicks)
    ]

仪表板:
Multiple PivotTables in Dash plotly python
Multiple PivotTables in Dash plotly python

英文:

I have finally implemented the logic to generate multiple pivot table on the dashboard. Below code implements that

app = dash.Dash(__name__)
app.title = 'My Dash example'

app.layout = html.Div([
    
    html.Button('Add Pivot Table', id='click', n_clicks=0),
    
    dash_pivottable.PivotTable(
        id='table',
        data=[list(covid_19_data_top_10.columns)] + covid_19_data_top_10.values.tolist(),
        cols=['Month'],
        rows=['Country'],
        rowOrder="key_a_to_z",
        rendererName="Grouped Column Chart",
        aggregatorName="Average",
        vals=["Confirmed"],
    ),
    
    
    
    
    html.Div(
        id='output'
    )
])


@app.callback(Output('output', 'children'),
              [Input('table', 'cols'),
               Input('table', 'rows'),
               Input('table', 'rowOrder'),
               Input('table', 'colOrder'),
               Input('table', 'aggregatorName'),
               Input('table', 'rendererName'),
              Input('click', 'n_clicks')])
def display_props(cols, rows, row_order, col_order, aggregator, renderer, n_clicks):
    return [
        dash_pivottable.PivotTable(
        id='table'+str(id),
        data=[list(covid_19_data_top_10.columns)] + covid_19_data_top_10.values.tolist(),
        cols=['Month'],
        colOrder="key_a_to_z",
        rows=['Country'],
        rowOrder="key_a_to_z",
        rendererName="Grouped Column Chart",
        aggregatorName="Average",
        vals=["Confirmed"],
    ) for id in range(n_clicks)
    ]

Dashboard:
Multiple PivotTables in Dash plotly python
Multiple PivotTables in Dash plotly python

huangapple
  • 本文由 发表于 2023年2月19日 21:47:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75500574.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定