Dash中的按钮没有“click”作为事件

问题描述 投票:2回答:1

我在布局中添加了一个按钮。当我尝试为它编写回调时,我收到以下错误:

dash.exceptions.NonExistantEventException: 
Attempting to assign a callback with
the event "click" but the component
"get_custom_vndr" doesn't have "click" as an event.

Here is a list of the available events in "get_custom_vndr":
[]

这是我如何将它添加到我的布局:

app_vndr.layout = html.Div([
    html.Button(
        '+',
        id='get_custom_vndr',
        type='submit'
    )
])

这是给出上述错误的回调函数:

@app_vndr.callback(
    dash.dependencies.Output('overlay', 'className'),
    events=[dash.dependencies.Event('get_custom_vndr', 'click'),
            dash.dependencies.Event('add_vndr_id_submit', 'click')])
def show_input(b1_n, b2_n):    
    if b1_n>0:
        return ''
    elif b1_n>0:
        return 'hidden'

当我将按钮添加到布局中时,我是否遗漏了什么?或者当我试图写回调?

我得到了它的工作

dash.dependencies.Input('get_custom_vndr', 'n_clicks')

但我想使用两个按钮进行相同的输出和n_clicks事件,我需要通过比较当前的n_clicks和每个按钮的前一个n_clicks来确定点击了哪个按钮,这看起来很漂亮hacky方式做到这一点。

python python-3.x plotly-dash
1个回答
0
投票

我不确定我是否理解你的问题,但是看看,如果这有帮助...

但是我想用两个按钮来输出相同的输出

简单!只需将显式短划线组件定义为Output,然后将两个按钮定义为Input。无论点击哪个按钮,它们都会触发相同的功能。例:

@app.callback(
    dash.dependencies.Output('overlay', 'className'),
    [dash.dependencies.Input('button1', 'n_clicks'),
     dash.dependencies.Input('button2', 'n_clicks')])

def update_output(n_clicks1, n_clicks2):
    # This is, where the magic happens

并且使用n_clicks事件,我需要尝试确定单击了哪个按钮

如果要对同一功能使用两个按钮,并且使用哪个不同,请将上述解决方案分成两个功能:

@app.callback(
    dash.dependencies.Output('overlay', 'className'),
    [dash.dependencies.Input('button1', 'n_clicks')])

def update_output(n_clicks1):
    # This is, where the magic happens for button 1

@app.callback(
    dash.dependencies.Output('overlay', 'className'),
    [dash.dependencies.Input('button2', 'n_clicks')])

def update_output(n_clicks2):
    # This is, where the magic happens for button 2
© www.soinside.com 2019 - 2024. All rights reserved.