In my Dash Application, I am using a dbc.Button
- dbc.Button("Show P", id="show-p-btn", className="me-1", n_clicks=0),
In my callback, I have a condition that checks for the value of n_clicks
of the button.
import dash
from dash.dependencies import Input, Output, State
import dash_bootstrap_components as dbc
import datetime
layout = html.Div([
dbc.Input(id="output-1"),
dbc.Button("Show P", id="show-p-btn", className="me-1", n_clicks=0),
])
@application.callback(Output("output-1","value"),
[
Input("show-p-btn","n_clicks")
]
)
def a1(n_clicks_val):
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
ctx = dash.callback_context
print(ctx.triggered, current_time)
if n_clicks_val > 0:
x = 1
else:
y = 0
if n_clicks_val > 0:
return (x)
else:
return(y)
I started noticing some strange behavior, where the button resets after I click it once and the value is set to 0. I print the output of ctx.triggered
[{'prop_id': 'show-p-btn.n_clicks', 'value': 1}]
— This is expected.
[{'prop_id': 'show-p-btn.n_clicks', 'value': None}]
— This is Not expected.
This is happening every time and I only click the button once. My condition doesn’t work because of this reset / unexpected behavior.
I have several components in the app layout
, but I only see show properties
being triggered and reset once I click it.
I am on :
dash 2.6.0
dash-bootstrap-components 0.13.0