# "prevent\_initial\_callbacks" does not work

**URL:** <https://community.plotly.com/t/prevent-initial-callbacks-does-not-work/46681>\
**Category:** Dash Python\
**Created:** [October 30, 2020, 5:20pm UTC](https://community.plotly.com/t/prevent-initial-callbacks-does-not-work/46681 "2020-10-30T17:20:11Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marcas](https://avatars.discourse-cdn.com/v4/letter/m/0ea827/32.png) [@Marcas](https://community.plotly.com/u/Marcas)\
**Post date:** [October 30, 2020, 5:20pm UTC](https://community.plotly.com/t/prevent-initial-callbacks-does-not-work/46681/1 "2020-10-30T17:20:11Z")

</div>

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.

```auto
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) 

```

---

<div class="post-metadata">

**Author:** ![mwveliz](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/mwveliz/32/10290_2.png) [@mwveliz](https://community.plotly.com/u/mwveliz)\
**Post date:** [November 11, 2020, 5:43pm UTC](https://community.plotly.com/t/prevent-initial-callbacks-does-not-work/46681/2 "2020-11-11T17:43:26Z")

</div>

> [@Marcas](#):
>
> prevent\_initial\_callbacks

Hello, I have the same problem, and prevent\_initial\_call does has the same issue for me

---

<div class="post-metadata">

**Author:** ![Marcas](https://avatars.discourse-cdn.com/v4/letter/m/0ea827/32.png) [@Marcas](https://community.plotly.com/u/Marcas)\
**Post date:** [November 12, 2020, 2:56am UTC](https://community.plotly.com/t/prevent-initial-callbacks-does-not-work/46681/3 "2020-11-12T02:56:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![anish](https://avatars.discourse-cdn.com/v4/letter/a/3bc359/32.png) [@anish](https://community.plotly.com/u/anish)\
**Post date:** [April 9, 2021, 7:36pm UTC](https://community.plotly.com/t/prevent-initial-callbacks-does-not-work/46681/4 "2021-04-09T19:36:09Z")

</div>

I think this is still not resolved as I’m having the same issue

---

<div class="post-metadata">

**Author:** ![Marcas](https://avatars.discourse-cdn.com/v4/letter/m/0ea827/32.png) [@Marcas](https://community.plotly.com/u/Marcas)\
**Post date:** [April 10, 2021, 11:53pm UTC](https://community.plotly.com/t/prevent-initial-callbacks-does-not-work/46681/5 "2021-04-10T23:53:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![skapoor](https://avatars.discourse-cdn.com/v4/letter/s/f475e1/32.png) [@skapoor](https://community.plotly.com/u/skapoor)\
**Post date:** [April 20, 2021, 4:29am UTC](https://community.plotly.com/t/prevent-initial-callbacks-does-not-work/46681/6 "2021-04-20T04:29:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![AnnMarieW](https://avatars.discourse-cdn.com/v4/letter/a/ec9cab/32.png) [@AnnMarieW](https://community.plotly.com/u/AnnMarieW)\
**Post date:** [April 20, 2021, 3:53pm UTC](https://community.plotly.com/t/prevent-initial-callbacks-does-not-work/46681/7 "2021-04-20T15:53:16Z")

</div>

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 :

```auto

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

---

<div class="post-metadata">

**Author:** ![jokin](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@jokin](https://community.plotly.com/u/jokin)\
**Post date:** [July 12, 2021, 9:24am UTC](https://community.plotly.com/t/prevent-initial-callbacks-does-not-work/46681/8 "2021-07-12T09:24:21Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![mking275](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/mking275/32/22237_2.png) [@mking275](https://community.plotly.com/u/mking275)\
**Post date:** [January 5, 2023, 4:15am UTC](https://community.plotly.com/t/prevent-initial-callbacks-does-not-work/46681/9 "2023-01-05T04:15:04Z")

</div>

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

```auto
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)

```

---

<div class="post-metadata">

**Author:** ![mking275](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/mking275/32/22237_2.png) [@mking275](https://community.plotly.com/u/mking275)\
**Post date:** [January 5, 2023, 4:18am UTC](https://community.plotly.com/t/prevent-initial-callbacks-does-not-work/46681/10 "2023-01-05T04:18:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![olejorgenb](https://avatars.discourse-cdn.com/v4/letter/o/ccd318/32.png) [@olejorgenb](https://community.plotly.com/u/olejorgenb)\
**Post date:** [October 26, 2023, 10:26am UTC](https://community.plotly.com/t/prevent-initial-callbacks-does-not-work/46681/11 "2023-10-26T10:26:08Z")

</div>

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](https://stackoverflow.com/a/74184510/1517969)

> 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](https://dash.plotly.com/advanced-callbacks#when-dash-components-are-added-to-the-layout)

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.

---

<div class="post-metadata">

**Author:** ![olejorgenb](https://avatars.discourse-cdn.com/v4/letter/o/ccd318/32.png) [@olejorgenb](https://community.plotly.com/u/olejorgenb)\
**Post date:** [October 26, 2023, 2:53pm UTC](https://community.plotly.com/t/prevent-initial-callbacks-does-not-work/46681/12 "2023-10-26T14:53:40Z")

</div>

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.
