【问题标题】:Use returned dash datatable in another callback在另一个回调中使用返回的破折号数据表
【发布时间】:2021-04-10 13:43:32
【问题描述】:

我在 Dash 中有一个表单,它接受值并根据该值返回过滤后的数据表:

import dash
import dash_bootstrap_components as dbc
import dash_core_components as dcc
import dash_html_components as html
import pandas as pd
from dash.dependencies import Input, Output
import dash_table
import numpy as np


df = pd.DataFrame(np.random.randint(10, size=[100,4]), columns=set('ABCD'))
print(df)

app = dash.Dash(external_stylesheets=[dbc.themes.BOOTSTRAP])

controls = dbc.Card(
    [
        dbc.FormGroup(
            [
                dbc.Label("Select group"),
                dbc.Input(id="group", type="number", value=3),
            ]
        ),
        html.Div(id='my_output'),
        html.Div(id='s_rows')
    ],
    body=True,
)

app.layout = dbc.Container(
    [
        dbc.Row(
            [
                dbc.Col(controls, md=4),
            ],
            align="center",
        ),
    ],
    fluid=True,
)


@app.callback(
    Output('my_output', 'children'),
    [
        Input("group", "value"),
    ],
)
def filtered_data(group):

    data = df[df['D'] == group]

    return dash_table.DataTable(
                    style_cell={
                        'whiteSpace': 'normal',
                        'height': 'auto',
                    },
                    id='table',
                    columns=[
                        {"name": i, "id": i, "deletable": True, "selectable": True} for i in data.columns
                    ],
                    data=data.to_dict('records'),
                    editable=True,
                    filter_action="native",
                    sort_action="native",
                    sort_mode="multi",
                    row_selectable="multi",
                    selected_columns=[],
                    selected_rows=[],
                    page_action="native",
                    page_current=0
                )

@app.callback(
    Output('s_rows', 'children'),
    [
        Input('table', 'selected_rows'),
    ]
)
def get_checked(selected_rows):
    return selected_rows

if __name__ == "__main__":
    app.run_server(debug=True, port=8000)

正如您在第一个回调中看到的那样,该函数返回过滤后的数据表,并带有一个供用户选择行的选项。

我希望能够将选定的行传递给另一个回调,但 Dash 无法识别我为返回的数据表设置的 ID,给了我错误:

ID not found in layout
Attempting to connect a callback Input item to component:
  "table"
but no components with that id exist in the layout.

【问题讨论】:

    标签: python callback plotly-dash


    【解决方案1】:

    由于selected_rows 没有在应用程序运行后立即定义,它会给您一个错误,即布局中不存在具有该 id 的组件,因为该组件尚未定义。如果您还更仔细地查看错误,在它的末尾,它还会告诉suppress this exception by setting suppress_callback_exceptions=True if you are assigning callbacks to components that are generated by other callbacks (and therefore not in the initial layout)

    因此,您只需在应用的定义中添加 suppress_callback_exceptions

    app = dash.Dash(external_stylesheets=[dbc.themes.BOOTSTRAP], suppress_callback_exceptions=True)
    

    【讨论】:

      猜你喜欢
      • 2021-05-20
      • 1970-01-01
      • 2020-10-27
      • 2020-06-25
      • 1970-01-01
      • 2021-10-19
      • 1970-01-01
      • 2021-10-14
      • 1970-01-01
      相关资源
      最近更新 更多