Example below:
- Click on the link (first element) - both n_clicks and number of callback triggers go up by 1.
- Click on the button (patches the text of the link) - callback is triggered but n_clicks is the same.
As a result - I get false callback triggers and don’t have an obvious way to mask them other than a dcc.store storing past n_clicks values.
The code:
from dash import Dash, html, callback, Output, Input, State, Patch
app = Dash()
app.layout = [
html.Div(html.A(children='I am a link inside a div A', id='htmla', href='#', n_clicks=0), id='htmldiv'), # TODO MAKE HOVER
html.Button('Toggle link A <-> B', id='button', n_clicks=0),
html.Br(),
html.P(children='Link clicks:'),
html.P(children=0, id='link_clicks'),
html.P(children='Link n_clicks:'),
html.P(children=0, id='n_clicks'),
]
@callback(
Output('link_clicks', 'children'),
Output('n_clicks', 'children'),
Input('htmla', 'n_clicks'),
State('link_clicks', 'children'),
prevent_initial_call=True,
)
def update_link_clicks(n_clicks, prev_clicks):
return prev_clicks + 1, n_clicks
@callback(
Output('htmldiv', 'children'),
Input('button', 'n_clicks'),
State('htmldiv', 'children'),
prevent_initial_call=True,
)
def update_link_text(_, current_children):
patched_link = Patch()
if current_children['props']['children'] == 'I am a link inside a div A':
patched_link['props']['children'] = 'I am a link inside a div B'
else:
patched_link['props']['children'] = 'I am a link inside a div A'
return patched_link
if __name__ == '__main__':
app.run(debug=True)