Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Instantiate n_click of acknowledge button to 0 instead of 1 #176

Merged
merged 1 commit into from
Nov 12, 2024

Conversation

vanderlindenma
Copy link
Collaborator

@vanderlindenma vanderlindenma commented Nov 12, 2024

WHAT'S THE ISSUE

Before this PR we have:

dbc.Button(
"Acquitter l'alerte",
id="acknowledge-button",
n_clicks=1,

but

@app.callback(
Output("to_acknowledge", "data"),
[Input("acknowledge-button", "n_clicks")],
[
State("event_id_on_display", "data"),
State("user_headers", "data"),
State("user_credentials", "data"),
],
prevent_initial_call=True,
)
def acknowledge_event(n_clicks, event_id_on_display, user_headers, user_credentials):
"""
Acknowledges the selected event and updates the state to reflect this.
Parameters:
- n_clicks (int): Number of clicks on the acknowledge button.
- event_id_on_display (int): Currently displayed event ID.
- user_headers (dict): User authorization headers for API requests.
- user_credentials (tuple): User credentials (username, password).
Returns:
- int: The ID of the event that has been acknowledged.
"""
if event_id_on_display == 0 or n_clicks == 0:
raise PreventUpdate

The intention behind n_clicks == 0 => PreventUpdate seems to be making sure that an alert is not acknowledged when the app loads. But that cannot work given that "acknowledge-button" is instantiated to have n_clicks = 1.

WHY DOES IT MATTER IF WE HAVE prevent_initial_call=True anyways?

Preventing acknowledgment on load is indeed already covered by prevent_initial_call=True.

However, prevent_initial_call=True does not cover accidental runs on redirect.

We have:

dcc.Location(id="url", refresh=False),

which implies upon url redirects Dash (/the underlying React engine) attempts to catch redirects triggered within the app and modify pages minimally instead of fully reloading a page.

As I understand the Dash/React lifecycle, this implies redirects triggered within the app do not count as page reloads =>prevent_initial_call=True does not apply to those page reloads
=> Dash re-runs all callbacks (or at least those for which the inputs or states may have changed).
=> At least if the acknowledge button is modified by the redirect (e.g., text translated from #173), it's reinstantiated to n_clicks = 1, the safeguard in the callback does not apply, and the error is acknowledged.

@vanderlindenma vanderlindenma changed the title Acknowledge button instantiation bug Instantiate n_click of acknowledge button to 0 instead of 1 Nov 12, 2024
@vanderlindenma vanderlindenma marked this pull request as ready for review November 12, 2024 16:10
@vanderlindenma vanderlindenma requested a review from fe51 November 12, 2024 16:11
Copy link
Member

@MateoLostanlen MateoLostanlen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch

@MateoLostanlen MateoLostanlen merged commit ef3b5e9 into main Nov 12, 2024
8 checks passed
@MateoLostanlen MateoLostanlen deleted the acknowledge_n_click_instantiation_bug branch November 12, 2024 21:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants