如何为生成的组件创建动态回调
How to create dynamic callback for generated component
我能够理解 callbacks
如何在 dash-table-experiment
中工作,其中 DataTable
是 app.layout = Div/Html layout
的一部分。
但是当 DataTable
像这样生成并且它不是静态布局的一部分时,如何创建 callback
?
def generate_table(tdf, max_rows=200):
return dt.DataTable(rows=tdf.to_dict('records'),
columns=tdf.columns,
row_selectable=True,
filterable=False,
sortable=False,
selected_row_indices=[],
id="datatable-gapminder"
)
如果我说
@app.callback(
Output('datatable-gapminder', 'selected_row_indices'),
[Input('graph-gapminder', 'clickData')],
[State('datatable-gapminder', 'selected_row_indices')])
def update_selected_row_indices(clickData, selected_row_indices):
if clickData:
for point in clickData['points']:
if point['pointNumber'] in selected_row_indices:
selected_row_indices.remove(point['pointNumber'])
else:
selected_row_indices.append(point['pointNumber'])
return selected_row_indices
我收到一个错误
Attempting to assign a callback to the
component with the id "datatable-gapminder" but no
components with id "datatable-gapminder" exist in the
app's layout.
我不知道,目前 dash 是否完全可行。但是,您可能会考虑创建一个空数据 table 组件,并在 call-back 中简单地更新它的所有属性(当然除了 id :))。如果用户不应该在启动时看到空数据table,您可以在开始时将其设置为隐藏div,并确保它在回调中可见。
如果您决定尝试这种方法,请确保每次调用创建数据 table 的回调时 return 有意义的值。即使这些值为空。
您收到该错误是因为 ID 为 datatable-gapminder
的组件尚未在布局中。
如果您想为尚未在布局中的组件创建回调,则必须抑制回调异常。
app.config.supress_callback_exceptions = True
我认为您还需要一个为布局服务的函数。默认情况下,Dash 应用程序将 app.layout
存储在内存中。如果将 app.layout 设置为函数,则可以在每次页面加载时提供动态布局。参见 here。
def serve_layout():
layout = html.Div(
children=[
# dt.DataTable()
],
)
return layout
app.layout = serve_layout
我能够理解 callbacks
如何在 dash-table-experiment
中工作,其中 DataTable
是 app.layout = Div/Html layout
的一部分。
但是当 DataTable
像这样生成并且它不是静态布局的一部分时,如何创建 callback
?
def generate_table(tdf, max_rows=200):
return dt.DataTable(rows=tdf.to_dict('records'),
columns=tdf.columns,
row_selectable=True,
filterable=False,
sortable=False,
selected_row_indices=[],
id="datatable-gapminder"
)
如果我说
@app.callback(
Output('datatable-gapminder', 'selected_row_indices'),
[Input('graph-gapminder', 'clickData')],
[State('datatable-gapminder', 'selected_row_indices')])
def update_selected_row_indices(clickData, selected_row_indices):
if clickData:
for point in clickData['points']:
if point['pointNumber'] in selected_row_indices:
selected_row_indices.remove(point['pointNumber'])
else:
selected_row_indices.append(point['pointNumber'])
return selected_row_indices
我收到一个错误
Attempting to assign a callback to the
component with the id "datatable-gapminder" but no
components with id "datatable-gapminder" exist in the
app's layout.
我不知道,目前 dash 是否完全可行。但是,您可能会考虑创建一个空数据 table 组件,并在 call-back 中简单地更新它的所有属性(当然除了 id :))。如果用户不应该在启动时看到空数据table,您可以在开始时将其设置为隐藏div,并确保它在回调中可见。
如果您决定尝试这种方法,请确保每次调用创建数据 table 的回调时 return 有意义的值。即使这些值为空。
您收到该错误是因为 ID 为 datatable-gapminder
的组件尚未在布局中。
如果您想为尚未在布局中的组件创建回调,则必须抑制回调异常。
app.config.supress_callback_exceptions = True
我认为您还需要一个为布局服务的函数。默认情况下,Dash 应用程序将 app.layout
存储在内存中。如果将 app.layout 设置为函数,则可以在每次页面加载时提供动态布局。参见 here。
def serve_layout():
layout = html.Div(
children=[
# dt.DataTable()
],
)
return layout
app.layout = serve_layout