【问题标题】:Dash Plotly - dbc.Button n_clicks callback not workingDash Plotly - dbc.Button n_clicks 回调不起作用
【发布时间】:2021-02-15 18:02:04
【问题描述】:

所以当我点击它时,我的(Dash Bootstrap 组件)dbc.Buttonn_clicks 属性不会增加。

按钮代码

save_button = dbc.Button("Save", id="save_new_item_button", className="btn btn-danger")

此按钮用于将我在模型中键入的数据保存到 SQLITE3 数据库。

回调函数

@app.callback(
    Output("modal_add_new", "is_open"),
    Output("add-new-status", "children"),
    [
        Input("add_new_item_btn", "n_clicks"),
        Input("close_modal_add_new", "n_clicks"),
        Input("save_new_item_button", "n_clicks"),
        Input("name-row", "value"),
        Input("description-row", "value"),
        Input("price-row", "value"),
        Input("unit-row", "value"),
        Input("limited-row", "value"),
        Input("stock-row", "value"),
        Input("active-row", "value"),
        Input("image-row", "filename"),
        Input("image-row", "contents"),
    ],
    [State("modal_add_new", "is_open")],
)
def toggle_modal(add, close, save, name, desc, price, unit, limited, stock, active, filename, content, is_open):

    # CONNECT TO SQLITE3 DATABASE
    connection = sql.connect(DATABASE)
    cursor = connection.cursor()

    print(save)

    if add or close:
        return not is_open, ""
    if save:
        print(is_open)
        if name is None or len(name) > 20:
            return is_open, "Enter a correct name."
        if desc is None:
            return is_open, "Enter a correct description."
        if price is None or price <= 0:
            return is_open, "Enter a number bigger than 0 in price field."
        if unit is None:
            return is_open, "Enter a correct Basic Unit."
        if limited != "yes" or limited != "no" or limited is None:
            return is_open, "Enter 'yes' or 'no' in limited field."
        if stock is None or stock < 0:
            return is_open, "Enter a number equal or bigger than 0 in stock field."
        if active != "yes" or active != "no" or active is None:
            return is_open, "Enter 'yes' or 'no' in active field."
        if filename is None or content is None:
            return is_open, "Invalid image file"
        else:
            for n, d in zip(filename, content):
                save_file(n, d)
            add_product(cursor, connection, name, price, unit, limited, stock, active, filename, desc)
            print("added")
            return is_open, "Added successfully"

    return is_open, ""

我无法理解我在做什么,因为它旁边的按钮(关闭 toggle_modal())工作正常。

【问题讨论】:

  • 如果您能创建一个MRE,那将非常有帮助。

标签: python button plotly-dash


【解决方案1】:

问题可能源于 id="save_new_item_button" 在多个 html/dbc/dcc 组件中重复。

即使它没有在您的布局中呈现,只要 Dash 在加载时注册具有重复 id 的组件,“n_clicks”(或“值”)属性将始终在 @app.callback 中采用“无”值.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-07
    • 2020-03-17
    • 1970-01-01
    • 2021-06-07
    • 1970-01-01
    • 2021-05-22
    • 1970-01-01
    • 2018-12-06
    相关资源
    最近更新 更多