根据另一页上的过滤数据表更新一页上的绘图

时间:2021-04-22 11:54:26

标签: python plotly plotly-dash

我有一个包含多个页面的侧边栏。 'page_Summ' 页面包含一个 DataTable - 然后对其进行过滤以创建一个数据框。我想将此数据框传递到第 2 页('page_SP_DP'),以根据第 1 页数据表中所做的过滤更新绘图。 第 2 页上的绘图始终为空白,并且永远不会执行 print 语句,表明永远不会为第 2 页激活回调(无论对第 1 页上的表做了什么)。有人对如何将过滤后的 dataTable 数据传递到单独的页面以更新绘图有任何建议吗?谢谢!

我在下面展示了最相关的代码:

@app.callback(Output("page-content", "children"), [Input("url", "pathname")])
def render_page_content(pathname):
    if pathname in ["/", "/page-1"]:
        return page_Summ
    elif pathname == "/page-2":
        return page_SP_DP
    # If the user tries to reach a different page, return a 404 message
    return dbc.Jumbotron(
        [
            html.H1("404: Not found", className="text-danger"),
            html.Hr(),
            html.P(f"The pathname {pathname} was not recognised..."),
        ]
    )
##### page 1 = Datatable

page_Summ = dbc.Container([
        dbc.Row([
            dbc.Col([html.Div([dash_table.DataTable(
        id='datatable-row-ids',
        columns=[
            {'name': i, 'id': i, 'deletable': True} for i in df.columns
            # omit the id column
            if i != 'id'
        ],
        data=df.to_dict('records'),
        editable=True,
        filter_action="native",
        sort_action="native",
        sort_mode='multi',
        row_selectable='multi',
        row_deletable=True,
        selected_rows=[],
        page_action='native',
        page_current= 0,
        page_size= 20,
    ),
    html.Div(id='datatable-row-ids-container')
]),
]),
]),

])

##### page 2 = Plot to update from Datatable on page 1
page_SP_DP = dbc.Container([

        dbc.Row([
            dbc.Col([
                dcc.Dropdown(
                    id='zaxis',
                    options=[{'label': i.title(), 'value': i} for i in features_ss],
                    style={"height": "100%"},#className='mr-3 text-success',
                    value='n30dBPower',
                )
        ],width={'size':1}),]),

        dbc.Row([
            dbc.Col(
                dcc.Graph(id='feature-graphic_S21_DP'))
        ]),
    ])

@app.callback(
    Output('feature-graphicEVM', 'figure'),
    Input('datatable-row-ids', 'derived_virtual_row_ids'))
def update_graphs(row_ids):
    print("Names of all rows pre or post filtering: {}".format(row_ids))
    if row_ids is None:
        dff = df
        # pandas Series works enough like a list for this to be OK
        row_ids = df['id']
    else:
        dff = df.loc[row_ids]

    fig = go.Figure()
    fig.add_traces([go.Scatter(
        x=dff['n30dBPower (dBm)'],
        y=dff['LP +17dBm\nEVM max\n 2:1\n(dB)'],
        mode='markers')]),

    return fig


if __name__ == '__main__':
    app.run_server()

0 个答案:

没有答案