# Prevent\_initial\_callback ineffective

**URL:** <https://community.plotly.com/t/prevent-initial-callback-ineffective/54663>\
**Category:** Dash Python\
**Created:** [July 12, 2021, 10:49am UTC](https://community.plotly.com/t/prevent-initial-callback-ineffective/54663 "2021-07-12T10:49:04Z")\
**Posts on this page:** 11\
**Page:** 1

<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, 10:49am UTC](https://community.plotly.com/t/prevent-initial-callback-ineffective/54663/1 "2021-07-12T10:49:04Z")

</div>

I am creating a new topic as this has not been resolved and other related threads grew to other topics. But as the title says, no matter if I set prevent\_initial\_call=True at callback level, the callback does get executed at startup and refresh. Here is a MWE:  
mwe.py :

```auto
import json
import dash_html_components as html
import dash_core_components as dcc
from dash import Dash
from dash_extensions.enrich import Output, Input
import mwe_test_component
app = Dash(title='MWE')

server = app.server
app.config.suppress_callback_exceptions = True

def serve_layout():
    return html.Div([dcc.Store(id='cache'),
                     dcc.Location(id='url'),
                     html.Div(id='page-content')])

app.layout = serve_layout

@app.callback(Output('page-content', 'children'),
              [Input('url', 'pathname')])
def display_page(pathname):
    return mwe_test_component.app()

@app.callback([Output("log", "children"), Output("cache", "data")],
              Input("button", "n_clicks"), prevent_initial_call=True)
def click(n_clicks):
    data_set = {"key1": [1, 2, 3], "key2": [4, 5, 6]}
    json_dump = json.dumps(data_set)
    return f"clicked ({n_clicks})", json_dump

if __name__ == ' __main__':
    app.run_server(port=7777, dev_tools_hot_reload=True, debug=True)

```

mwe\_test\_component.py:

```auto
import dash_html_components as html

def app():
    return html.Div([
        html.Button("button", id="button", n_clicks=0),
        html.Div("Initial value", id="log")])

```

Is this a bug with Dash or how the app is structured?

---

<div class="post-metadata">

**Author:** ![Emil](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/emil/32/31734_2.png) [@Emil](https://community.plotly.com/u/Emil)\
**Post date:** [July 12, 2021, 10:51am UTC](https://community.plotly.com/t/prevent-initial-callback-ineffective/54663/2 "2021-07-12T10:51:22Z")

</div>

I guess that’s because your are using “dynamic” layout. Did you try with a “static” layout, i.e. assigning a rendered output instead of the layout function (I know this is probably not what you want and thus doesn’t solve your problem)?

---

<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, 10:55am UTC](https://community.plotly.com/t/prevent-initial-callback-ineffective/54663/3 "2021-07-12T10:55:53Z")

</div>

Indeed I need this dynamic layout for my app to work when deployed, but even with a “static” layout, the callback still gets triggered at startup anyway.

---

<div class="post-metadata">

**Author:** ![Emil](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/emil/32/31734_2.png) [@Emil](https://community.plotly.com/u/Emil)\
**Post date:** [July 12, 2021, 11:02am UTC](https://community.plotly.com/t/prevent-initial-callback-ineffective/54663/4 "2021-07-12T11:02:12Z")

</div>

I just tested. On `dash==1.20.0` it works for both dynamic and static layouts. Here is my mwe (yours didn’t run, you were importing a local file),

```auto
import dash_html_components as html
from dash import Dash
from dash_extensions.enrich import Output, Input

def serve_layout():
    return html.Div([html.Div(id='trigger'), html.Div(id='page-content')])

app = Dash(prevent_initial_callbacks=True)
app.layout = serve_layout

@app.callback(Output('page-content', 'children'), [Input('trigger', 'children')])
def test(_):
    return "If you see this, prevent_initial_call didn't work"

if __name__ == ' __main__':
    app.run_server()

```

---

<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, 11:06am UTC](https://community.plotly.com/t/prevent-initial-callback-ineffective/54663/5 "2021-07-12T11:06:36Z")

</div>

Right, this one works for me too.  
In my MWE, I pasted the file that is imported so you can run it.  
I suspect that is the issue then, that importing the layout breaks things somehow?

---

<div class="post-metadata">

**Author:** ![Emil](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/emil/32/31734_2.png) [@Emil](https://community.plotly.com/u/Emil)\
**Post date:** [July 12, 2021, 11:13am UTC](https://community.plotly.com/t/prevent-initial-callback-ineffective/54663/6 "2021-07-12T11:13:07Z")

</div>

No, it doesn’t matter how you structure the code. However, the `prevent_initial_calls` flag doesn’t prevent the callback from firering when you dynamically add the input to the page _after_ it has been rendered. This seems indeed to be the case for your example now that I look a bit closer at your code.

[https://dash.plotly.com/advanced-callbacks](https://dash.plotly.com/advanced-callbacks)

---

<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, 11:22am UTC](https://community.plotly.com/t/prevent-initial-callback-ineffective/54663/7 "2021-07-12T11:22:51Z")

</div>

Aha yes that could well be the case. The intention here is to have a multipage app, so the content is loaded depending on the url. I’ll look into options from the doc, if that is the intended behavior.

---

<div class="post-metadata">

**Author:** ![Emil](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/emil/32/31734_2.png) [@Emil](https://community.plotly.com/u/Emil)\
**Post date:** [July 12, 2021, 11:27am UTC](https://community.plotly.com/t/prevent-initial-callback-ineffective/54663/8 "2021-07-12T11:27:31Z")

</div>

I believe that it is. That being said, I can see why you would prefer a different behavior for multipage apps. From _that_ perspective, this might be a suitable feature to implement in dash-extensions after all, if we can come up with a concise way to determine if a callback should be fired or not.

---

<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, 1:09pm UTC](https://community.plotly.com/t/prevent-initial-callback-ineffective/54663/9 "2021-07-12T13:09:58Z")

</div>

I would have expected most multipage apps to require this feature but maybe there is something I don’t quite grasp, or the multipage apps are an uncommon use case of Dash?  
In any case for the time being I will just assume that all callbacks are executed and insert some logic in each to control what they do at startup. But that feature would greatly clean things up.

---

<div class="post-metadata">

**Author:** ![icescreamsandwic](https://avatars.discourse-cdn.com/v4/letter/i/6a8cbe/32.png) [@icescreamsandwic](https://community.plotly.com/u/icescreamsandwic)\
**Post date:** [August 13, 2022, 2:48pm UTC](https://community.plotly.com/t/prevent-initial-callback-ineffective/54663/10 "2022-08-13T14:48:47Z")

</div>

I serve a dynamic layout, but no callback adds something to the layout. Everything is fixed from the beginning and yet the prevent\_initial\_call is not working in my case.

---

<div class="post-metadata">

**Author:** ![jhr](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@jhr](https://community.plotly.com/u/jhr)\
**Post date:** [August 29, 2022, 3:03am UTC](https://community.plotly.com/t/prevent-initial-callback-ineffective/54663/11 "2022-08-29T03:03:45Z")

</div>

I has the same issue in my multipage apps.That may be a bug?
