python - 如何在 Python 的 Dash 包中创建两个可以修改卡片体的按钮,每个按钮都有自己的方式?
问题描述
我的 app.callback 函数有问题,我希望通过按下 top_masse 按钮来更改 cardBody 的内部,当我按下 top_masse 按钮时,我还以另一种方式更改 cardBody。问题是在我的代码中,在第一个按钮而不是第二个按钮上进行了更改,我不明白问题出在哪里,但它考虑了点击次数。这是简化版本的代码:
import dash
import dash_bootstrap_components as dbc
import dash_core_components as dcc
import dash_html_components as html
import dash_table as dt
from dash.dependencies import Input, Output, State
app = dash.Dash(external_stylesheets=[dbc.themes.BOOTSTRAP])
tarif_jumb = dbc.Jumbotron([
html.H2("Tarification", className="display-3"),
html.Hr(className="my-2",),
html.P(),
dbc.Button("Tarif en masse", color="primary", className="mr-3", id="top_masse"),
dbc.Button("Tarif parc", color="danger", className="mr-3", id="top_parc")
,])
card_top_tarif = dbc.Card(
dbc.CardBody([
tarif_jumb
,])
, className="mt-3"
,)
tab_top_tarif = dbc.Tab(card_top_tarif, label="Choix tarifaire", tab_id="top_tarif",id="top_tarif")
tabs = dbc.Card(dbc.CardBody([
dbc.Tabs([
tab_top_tarif
,]
, id="tabs"
, active_tab="top_tarif"
,)
,],
id="card_body"
,))
app.layout = dbc.Container([
tabs
,],)
@app.callback(Output(component_id="card_body", component_property="children"),
[Input(component_id="top_masse", component_property="n_clicks"),
Input(component_id="top_parc", component_property="n_clicks")
,])
def tarif_masse(n_clicks1,n_clicks2):
if n_clicks1>=1:
return dbc.Alert('The button has been clicked {} times'.format(n_clicks2), color="primary")
elif n_clicks2>=1:
return dbc.Alert('The button has been clicked {} times'.format(n_clicks1), color="primary")
@app.callback(Output(component_id="alert", component_property="children"),
[Input(component_id="importation", component_property="n_clicks")
,])
def tarif_masse(n_clicks):
if n_clicks>=1:
return 'The button has been clicked {} times'.format(n_clicks)
if __name__ == "__main__":
app.run_server(debug=False,port=8080,host='0.0.0.0')
解决方案
实际上你只需要定义'else'并且它有效,我无法解释它但它有效。
@app.callback(Output(component_id="card_body", component_property="children"),
[Input(component_id="top_masse", component_property="n_clicks"),
Input(component_id="top_parc", component_property="n_clicks")
,])
def tarif_masse(n_clicks1,n_clicks2):
if n_clicks1 is not None:
return dbc.Alert('The button has been clicked {} times'.format(n_clicks1),
color="primary")
elif n_clicks2 is not None:
return dbc.Alert('The button hed {} times'.format(n_clicks2), color="primary")
else:
return dbc.Tabs([tab_top_tarif,], id="tabs", active_tab="top_tarif")
if __name__ == "__main__":
app.run_server(debug=False,port=8050,host='0.0.0.0')```
推荐阅读
- javascript - 是否可以加载要转换为 HTML 表的 XLSX 文件,而无需用户浏览所述 XLSX 文件?
- javascript - 如何在 mvc 视图中单击页面上的任意位置重定向另一个页面
- sql - 将 ENCRYPTBYPASSPHRASE/DECRYPTBYPASSPHRASE 与文件路径一起使用时出现问题
- python - 3d中从点到线段的距离(Python)
- c# - 如果我有新的 UI 更新,如何停止预定的 UI 更新?
- gradle - gradle 从普通 url 加载 jar 作为依赖项
- python - 如何在 .yaml 文件中获取列表字典?
- mongodb - 在 MapReduce 和 Spark 中加载到 MongoDB 的额外记录(使用不同的写入结构)
- javascript - 为什么 `super` 关键字属性在 ES6 类中返回未定义?
- python - 如何在 Google Drive API v3、python 客户端中删除文件