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

Prevent initial call #1228

Merged
merged 4 commits into from
May 4, 2020
Merged

Prevent initial call #1228

merged 4 commits into from
May 4, 2020

Conversation

alexcjohnson
Copy link
Collaborator

@alexcjohnson alexcjohnson commented May 4, 2020

Closes #1225
Supersedes #1123

Adds the ability to control whether or not callbacks fire when the page or layout chunk first loads. This can simplify a lot of code like if n_clicks is None: raise PreventUpdate, and it can improve loading performance, particularly for pattern-matching callbacks, where a page can easily generate hundreds of callback invocations, since there is no longer even a server call for these callbacks.

This option is off by default, but can either be turned on for each callback independently:

@app.callback(outputs, inputs, prevent_initial_call=True)
def cb(...):
    ...

app.clientside_callback(func, outputs, inputs, prevent_initial_call=True)

or app-wide, in which case it can be turned off for individual callbacks:

app = dash.Dash(prevent_initial_callbacks=True)

@app.callback(outputs, inputs, prevent_initial_call=False)
def cb(...)
    ...
  • I have run the tests locally and they passed. (refer to testing section in contributing)
  • I have added tests, or extended existing tests, to cover any new features or bugs fixed in this PR
  • I have added entry in the CHANGELOG.md
  • Docs follow-up issue: https://github.com/plotly/dash-docs/issues/874

@alexcjohnson alexcjohnson added this to the Dash v1.12 milestone May 4, 2020
Copy link
Contributor

@Marc-Andre-Rivet Marc-Andre-Rivet left a comment

Choose a reason for hiding this comment

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

💃

startsWith(chunkPath),
pluck('path', flatten(cb.getOutputs(paths)))
)
) {
cb.changedPropIds = {};
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm not sure what I was thinking here with clearing out changedPropIds, but in this case (we've found a callback by its inputs due to a layout change, and just determined that all its outputs are in the same layout chunk) we know the callback is already included in the queue via its outputs so the best thing to do is simply not re-include it at all. This extra addCallback was harmless before prevent_initial_call, just a slight bit of extra work; but now that we have prevent_initial_call it's possible this callback would not be added based on its outputs.

If this condition isn't met, there are outputs outside the new layout chunk and it's not in fact an initial call anyway - so it's appropriate to add the callback and that's what the new logic does.

{"clientside": False, "content": True, "global": False},
{"clientside": True, "content": True, "global": True},
{"clientside": False, "content": False, "global": True},
]
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

pytest does a nice job logging dictionary test parametrizations (flavor0, flavor1 etc):
Screen Shot 2020-05-04 at 5 15 44 PM

@alexcjohnson alexcjohnson merged commit 7961611 into dev May 4, 2020
@alexcjohnson alexcjohnson deleted the prevent-initial-call branch May 4, 2020 21:26
@maartenbreddels
Copy link

Initial impression is really good! Together with #832 we can avoid any unneeded calls, and have a very fast page load.

@alexcjohnson alexcjohnson mentioned this pull request May 13, 2020
5 tasks
nightsailer added a commit to o3cloud/dash that referenced this pull request Jun 13, 2020
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.

Ability to prevent initial callback from firing
3 participants