Plotly/Dash:如何防止动态插入的 link/element 在用户未点击的情况下自动触发回调?
Plotly/Dash: How to prevent dynamically inserted link/element from firing callback automatically without user clicking it?
我有一个可创建一些预览缩略图的可点击图片库。
[
html.A(id=_id, children=html.Img(src=src, height=300, id=image_id, style={'margin': '10px'}))
,...]
动态插入 Div。第一个图像 'clicked' 回调在生成列表时触发。
然后强制另一个元素加载一些内容。用户现在可以更改或删除更新缩略图的图像。但是,第一张图片再次发送点击。问题是图像从未被点击过,现在强制其他元素加载完全错误的东西。是否可以防止这种情况发生?
我试过了
@app.callback(
Output('pko-image-clicked-output', 'children'),
[Input({'type': 'image', 'index': ALL}, 'n_clicks')],
prevent_initial_call=True
)
def pko_image_clicked(ndx):
if ndx is None or len(ndx)==0: raise PreventUpdate
ctx = dash.callback_context
clicked = ctx.triggered[0]['prop_id']
clicked = clicked.replace('{"index":"', '')
clicked = clicked.split('","type":')[0].replace('\', '')
print('Clicked:', clicked)
return clicked
这没有用。我有一个多页应用程序,需要:
app.config['suppress_callback_exceptions'] = True
当此组件完成时激活(触发第一个图像的回调):
def callbacks(app, fsc, cache):
@app.callback(
Output('pko-dropdown', 'options'),
Input('tab', 'value'),
Input('pko-delete-output', 'children'),
State('wdir', 'children'),
State('pko-dropdown', 'options'),
)
def pko_controls(tab, peak_deleted, wdir, old_options):
if tab != 'pko':
raise PreventUpdate
peaklist = T.get_peaklist( wdir )
if peaklist is None:
raise PreventUpdate
options = [{'label':label, 'value': i} for i, label in enumerate(peaklist.index)]
if options == old_options:
raise PreventUpdate
return options
我不确定 ndx
是一个字符串。您正在使用 n_click
属性的 ALL
,因此我认为它应该是 int
的列表。你可以尝试两件事。
if not any(ndx):
raise PreventUpdate
如果这不起作用,您还可以这样做:
if len(dash.callback_context.triggered[0]) > 1:
raise PreventUpdate
我不得不处理同样的问题,所以我认为其中一个至少应该有效。如果没有,请告诉我。
我有一个可创建一些预览缩略图的可点击图片库。
[
html.A(id=_id, children=html.Img(src=src, height=300, id=image_id, style={'margin': '10px'}))
,...]
动态插入 Div。第一个图像 'clicked' 回调在生成列表时触发。 然后强制另一个元素加载一些内容。用户现在可以更改或删除更新缩略图的图像。但是,第一张图片再次发送点击。问题是图像从未被点击过,现在强制其他元素加载完全错误的东西。是否可以防止这种情况发生?
我试过了
@app.callback(
Output('pko-image-clicked-output', 'children'),
[Input({'type': 'image', 'index': ALL}, 'n_clicks')],
prevent_initial_call=True
)
def pko_image_clicked(ndx):
if ndx is None or len(ndx)==0: raise PreventUpdate
ctx = dash.callback_context
clicked = ctx.triggered[0]['prop_id']
clicked = clicked.replace('{"index":"', '')
clicked = clicked.split('","type":')[0].replace('\', '')
print('Clicked:', clicked)
return clicked
这没有用。我有一个多页应用程序,需要:
app.config['suppress_callback_exceptions'] = True
当此组件完成时激活(触发第一个图像的回调):
def callbacks(app, fsc, cache):
@app.callback(
Output('pko-dropdown', 'options'),
Input('tab', 'value'),
Input('pko-delete-output', 'children'),
State('wdir', 'children'),
State('pko-dropdown', 'options'),
)
def pko_controls(tab, peak_deleted, wdir, old_options):
if tab != 'pko':
raise PreventUpdate
peaklist = T.get_peaklist( wdir )
if peaklist is None:
raise PreventUpdate
options = [{'label':label, 'value': i} for i, label in enumerate(peaklist.index)]
if options == old_options:
raise PreventUpdate
return options
我不确定 ndx
是一个字符串。您正在使用 n_click
属性的 ALL
,因此我认为它应该是 int
的列表。你可以尝试两件事。
if not any(ndx):
raise PreventUpdate
如果这不起作用,您还可以这样做:
if len(dash.callback_context.triggered[0]) > 1:
raise PreventUpdate
我不得不处理同样的问题,所以我认为其中一个至少应该有效。如果没有,请告诉我。