"prevent_initial_callbacks" does not work

I noticed that option prevent_initial_callbacks stopped working.
I try to investigate, did updare dash, put commands to prevent callbacks from firing as general and for individual callbacks. Each time after starting an app all callbacks are executed.

My versions:

$ conda list dash
Name Version Build Channel
dash 1.16.3 py_0
dash-core-components 1.3.1 py_0
dash-html-components 1.0.1 py_0
dash-renderer 1.1.2 py_0
dash-table 4.4.1 py_0
jupyter-dash 0.3.0 py_0 plotly

Below is sample code from Dash documentation (modified for JupLab) where all callbacks are triggered at initialization.

Do you have idea how to prevent callbacks from firing at start?
Or, can you confirm/deny similar behaviour on your machines.

import dash
from dash.dependencies import Input, Output
import dash_html_components as html
from datetime import datetime
import time
from jupyter_dash import JupyterDash

app = JupyterDash(prevent_initial_callbacks=True)

# app = dash.Dash()

app.layout = html.Div(
    [
        html.Button("execute callbacks", id="button_2"),
        html.Div(children="callback not executed", id="first_output_2"),
        html.Div(children="callback not executed", id="second_output_2"),
        html.Div(children="callback not executed", id="third_output_2"),
        html.Div(children="callback not executed", id="fourth_output_2"),
    ]
)


@app.callback(
    [Output("first_output_2", "children"), Output("second_output_2", "children")],
    [Input("button_2", "n_clicks")], prevent_initial_call=True)
def first_callback(n):
    now = datetime.now()
    current_time = now.strftime("%H:%M:%S")
    return ["in the first callback it is " + current_time, "in the first callback it is " + current_time]


@app.callback(
    Output("third_output_2", "children"), [Input("second_output_2", "children")], prevent_initial_call=True)
def second_callback(n):
    time.sleep(2)
    now = datetime.now()
    current_time = now.strftime("%H:%M:%S")
    return "in the second callback it is " + current_time


@app.callback(
    Output("fourth_output_2", "children"),
    [Input("first_output_2", "children"), Input("third_output_2", "children")], prevent_initial_call=True)
def third_output(n, m):
    time.sleep(2)
    now = datetime.now()
    current_time = now.strftime("%H:%M:%S")
    return "in the third callback it is " + current_time


# if __name__ == '__main__':
#     app.run_server(debug=True)

app.run_server(mode='external', port = 8050, dev_tools_ui=True, 
          dev_tools_hot_reload =True, threaded=True) 

Hello, I have the same problem, and prevent_initial_call does has the same issue for me

I did code around this issue with ifā€™s.
I hope when prevent_initial_callbacks will start working again on my machine I wont be forced to review the code but have no way to test it at the moment.
I still do have other issues with Dash. I noticed that some of them are /were related to security restrictions I put on my web browser. I didnā€™t see a list of all dependencies for Dash and mostly it is try and error method foe me.
Beside that - Dash is great and helped me, almost web illiterate, to add nice gui over my py codes.

I think this is still not resolved as Iā€™m having the same issue

Oh, yes.
I still have dashboards with not working ā€˜prevents-initialsā€™.

I also noticed other ā€˜deviationsā€™ from norm when working with my dash scripts. Some of the problems I posted here but nobody offered a clear explanation.

Some of those ā€˜deviationsā€™ lead me to do a little investigation. I didnā€™t go into depths but I suspect that there is ā€˜something not rightā€™ with updating dash files in py environments.

To test it try simply to create new environment from the scratch. All newest versions.
I was only updating conda-env (I donā€™t use pip). When I created new one, ā€˜preventsā€™ and other issues went away.

Maybe, one day, I will do more testing, but for now, when Iā€™m not completely in the dark, Iā€™m ok with ā€˜dual standardā€™ environments. Will keel slowly moving my codes to the ā€˜standardā€™.

Hope this helps.

This stopped working for me as well when I updated dash. Currently on 1.19.0

Hi @Marcas @skapoor @mwveliz

If you have issues where something suddenly stops working or if you canā€™t run examples pulled directly from the docs, the likely cause is that Dash is not properly installed with the correct versions of all of the libraries .

Note to conda users:

If you install Dash using conda install without any other options, it installs an out-of-date version of dash-core-components and the dash renderer (and other things as well). The conda-forge channel is up-to-date, so try installing dash with :


conda install -c conda-forge dash=1.20

The OP is a good example. The version of dash 1.16.3 was the current version as of the date of the post last October, however all the other libraries are at least a year out-of-date

In the environment thatā€™s running your app, check the version numbers of all libraries
Hereā€™s the latest:

dash 1.20.0
dash-core-components 1.16.0
dash-daq 0.5.0
dash-html-components 1.1.3
dash-renderer 1.9.1
dash-table 4.11.3

And donā€™t forget about upgrading to the latest version of dash-bootstrap-components, and
if you havenā€™t tried dash-labs yet, I recommend installing that too and checking it out. Itā€™s pretty cool!

dash-bootstrap-components 0.12.0
dash-labs 0.1.0

I have the same issue. I have been using workarounds with a lot of ifs, but as the app has been growing, it has become very difficult to manage. Has there been any update on this?

Same problem hereā€¦ anyone know what trick it takes to get prevent_initial_call to actually work?

dash 2.7.1 and the example code from the docs throws the error

import dash
from dash.dependencies import Input, Output
import dash_html_components as html
from datetime import datetime
import time
from jupyter_dash import JupyterDash

app = JupyterDash(prevent_initial_callbacks=True)

# app = dash.Dash()

app.layout = html.Div(
    [
        html.Button("execute callbacks", id="button_2"),
        html.Div(children="callback not executed", id="first_output_2"),
        html.Div(children="callback not executed", id="second_output_2"),
        html.Div(children="callback not executed", id="third_output_2"),
        html.Div(children="callback not executed", id="fourth_output_2"),
    ]
)


@app.callback(
    [Output("first_output_2", "children"), Output("second_output_2", "children")],
    [Input("button_2", "n_clicks")], prevent_initial_call=True)
def first_callback(n):
    now = datetime.now()
    current_time = now.strftime("%H:%M:%S")
    return ["in the first callback it is " + current_time, "in the first callback it is " + current_time]


@app.callback(
    Output("third_output_2", "children"), [Input("second_output_2", "children")], prevent_initial_call=True)
def second_callback(n):
    time.sleep(2)
    now = datetime.now()
    current_time = now.strftime("%H:%M:%S")
    return "in the second callback it is " + current_time


@app.callback(
    Output("fourth_output_2", "children"),
    [Input("first_output_2", "children"), Input("third_output_2", "children")], prevent_initial_call=True)
def third_output(n, m):
    time.sleep(2)
    now = datetime.now()
    current_time = now.strftime("%H:%M:%S")
    return "in the third callback it is " + current_time


# if __name__ == '__main__':
#     app.run_server(debug=True)

app.run_server(mode='external', port = 8050, dev_tools_ui=True, 
          dev_tools_hot_reload =True, threaded=True)

Exact same issueā€¦ prevent_initial_call=True throws the error:
TypeError: DashDependency.init() got an unexpected keyword argument ā€˜prevent_initial_callā€™

Even the example code from the docs does not work, and pycharm says its an unexpected arg.

dash 2.7.1

Without having read this thread in detail I suspect it might be the same thing as discussed here: python - Why Prevent_initial_call does not stop the initial call? - Stack Overflow

In other words, if the output of the callback is already present in the app layout before its input is inserted into the layout, prevent_initial_call will not prevent its execution when the input is first inserted into the layout.

See Advanced Callbacks | Dash for Python Documentation | Plotly

EDIT: Iā€™m sure thereā€™s logic behind this, but seem like an issue if thereā€™s no easy way to distinguish such calls from calls triggered by ā€œactualā€ changes.

1 Like

A possible workaround for callbacks fired due to inputs being inserted: Seems like all the inputs (or at least the inserted inputs) are marked as triggers. So itā€™s possible to include a dummy input which is never triggered normally to detect such callback invocations.

1 Like