Skip to content

Commit 9846817

Browse files
committed
app.config.prevent_initiall_callbacks
1 parent 19095e9 commit 9846817

File tree

2 files changed

+167
-73
lines changed

2 files changed

+167
-73
lines changed

dash/dash.py

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,15 @@ class Dash(object):
211211
env: ``DASH_SUPPRESS_CALLBACK_EXCEPTIONS``
212212
:type suppress_callback_exceptions: boolean
213213
214+
:param prevent_initial_callbacks: Default ``False``: Sets the default value
215+
of ``prevent_initial_call`` for all callbacks added to the app.
216+
Normally all callbacks are fired when the associated outputs are first
217+
added to the page. You can disable this for individual callbacks by
218+
setting ``prevent_initial_call`` in their definitions, or set it
219+
``True`` here in which case you must explicitly set it ``False`` for
220+
those callbacks you wish to have an initial call. This setting has no
221+
effect on triggering callbacks when their inputs change later on.
222+
214223
:param show_undo_redo: Default ``False``, set to ``True`` to enable undo
215224
and redo buttons for stepping through the history of the app state.
216225
:type show_undo_redo: boolean
@@ -241,6 +250,7 @@ def __init__(
241250
external_scripts=None,
242251
external_stylesheets=None,
243252
suppress_callback_exceptions=None,
253+
prevent_initial_callbacks=False,
244254
show_undo_redo=False,
245255
plugins=None,
246256
**obsolete
@@ -288,6 +298,7 @@ def __init__(
288298
suppress_callback_exceptions=get_combined_config(
289299
"suppress_callback_exceptions", suppress_callback_exceptions, False
290300
),
301+
prevent_initial_callbacks=prevent_initial_callbacks,
291302
show_undo_redo=show_undo_redo,
292303
)
293304
self.config.set_read_only(
@@ -814,6 +825,9 @@ def dependencies(self):
814825
return flask.jsonify(self._callback_list)
815826

816827
def _insert_callback(self, output, inputs, state, prevent_initial_call):
828+
if prevent_initial_call is None:
829+
prevent_initial_call = self.config.prevent_initial_callbacks
830+
817831
_validate.validate_callback(output, inputs, state)
818832
callback_id = create_callback_id(output)
819833
callback_spec = {
@@ -832,7 +846,7 @@ def _insert_callback(self, output, inputs, state, prevent_initial_call):
832846
return callback_id
833847

834848
def clientside_callback(
835-
self, clientside_function, output, inputs, state=(), prevent_initial_call=False
849+
self, clientside_function, output, inputs, state=(), prevent_initial_call=None
836850
):
837851
"""Create a callback that updates the output by calling a clientside
838852
(JavaScript) function instead of a Python function.
@@ -893,6 +907,10 @@ def clientside_callback(
893907
Input('another-input', 'value')]
894908
)
895909
```
910+
911+
The last, optional argument `prevent_initial_call` causes the callback
912+
not to fire when its outputs are first added to the page. Defaults to
913+
`False` unless `prevent_initial_callbacks=True` at the app level.
896914
"""
897915
self._insert_callback(output, inputs, state, prevent_initial_call)
898916

@@ -925,7 +943,18 @@ def clientside_callback(
925943
"function_name": function_name,
926944
}
927945

928-
def callback(self, output, inputs, state=(), prevent_initial_call=False):
946+
def callback(self, output, inputs, state=(), prevent_initial_call=None):
947+
"""
948+
Normally used as a decorator, `@app.callback` provides a server-side
949+
callback relating the values of one or more `output` items to one or
950+
more `input` items which will trigger the callback when they change,
951+
and optionally `state` items which provide additional information but
952+
do not trigger the callback directly.
953+
954+
The last, optional argument `prevent_initial_call` causes the callback
955+
not to fire when its outputs are first added to the page. Defaults to
956+
`False` unless `prevent_initial_callbacks=True` at the app level.
957+
"""
929958
callback_id = self._insert_callback(output, inputs, state, prevent_initial_call)
930959
multi = isinstance(output, (list, tuple))
931960

0 commit comments

Comments
 (0)