【发布时间】:2021-02-15 18:02:04
【问题描述】:
所以当我点击它时,我的(Dash Bootstrap 组件)dbc.Button 的n_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