# @app.callback improvements? (Trigger component, same Output multiple times, callback without Output, serverside store / callback cache)

**URL:** <https://community.plotly.com/t/app-callback-improvements-trigger-component-same-output-multiple-times-callback-without-output-serverside-store-callback-cache/42937>\
**Category:** Dash Python\
**Created:** [July 25, 2020, 12:57pm UTC](https://community.plotly.com/t/app-callback-improvements-trigger-component-same-output-multiple-times-callback-without-output-serverside-store-callback-cache/42937 "2020-07-25T12:57:14Z")\
**Posts on this page:** 5\
**Page:** 2

<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 30, 2020, 6:38pm UTC](https://community.plotly.com/t/app-callback-improvements-trigger-component-same-output-multiple-times-callback-without-output-serverside-store-callback-cache/42937/21 "2020-07-30T18:38:57Z")

</div>

Before commenting, i would like to clarify a few details. In my current implementation, i am creating one (key,value) pair per output, not one per callback. There are a few reasons for this choice

- If makes it possible to write different outputs from the same callback to different stores (e.g. large outputs to files, smaller ones to a redis cache)
- If only a single (key,value) per is written for a multi output callback, any callback that has any of the outputs as input will need to read the data associated with _all_ outputs. By writing a (key,value) pair per output, this overhead can be avoided

As i noted previously, my intention of the `memoize` keyword in this context is to reuse the server side caching which is already carried out. However, this will only work for the `ServersideOutput` (which is being cached on the server) and not for the `Output` (which is not cached anywhere).

Another option would be simply to use the `@flask_caching.memoize` decorator, but the downside is that you would then be caching the data twice. Depending on the size of the data, this could be a big problem (big data) or no problem at all (small data).

EDIT: I guess one way to solve the problem would be to cache _all_ outputs of a callback whenever the `memoize` keyword is used. The `ServersideOutput`s should of course be written to whatever backend specified, while the `Outputs` would need some kind of default backend.

---

<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 31, 2020, 7:36am UTC](https://community.plotly.com/t/app-callback-improvements-trigger-component-same-output-multiple-times-callback-without-output-serverside-store-callback-cache/42937/22 "2020-07-31T07:36:36Z")

</div>

The defaults for output storage (i.e. `backend` and `session_check`) could be configured via the app, i.e something like

`app = Dash(prevent_initial_callbacks=True, output_defaults=dict(backend=FileSystemStore(), session_check=True)`

It would then be possible to mix `Output` and `ServersideOutput` in a memoized callback. You could even do an `Output` only callback with `memoize=True`,

```auto
@app.callback([Output("store", "data"), Output("log", "children")], Trigger("btn", "n_clicks"), memoize=True)
def query_data():
    time.sleep(1)
    return px.data.gapminder().to_json(), "Data loaded"

```

which would yield the exact same behaviour as the `@flask_caching.memoize` decorator.

---

<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 31, 2020, 11:57am UTC](https://community.plotly.com/t/app-callback-improvements-trigger-component-same-output-multiple-times-callback-without-output-serverside-store-callback-cache/42937/23 "2020-07-31T11:57:46Z")

</div>

I have just pushed a new version of `dash-extensions` (0.0.26) that implements the ideas above. Suggestions for syntax changes are still very welcome, but i think it is already pretty neat 🙂

---

<div class="post-metadata">

**Author:** ![sdementen](https://avatars.discourse-cdn.com/v4/letter/s/67e7ee/32.png) [@sdementen](https://community.plotly.com/u/sdementen)\
**Post date:** [November 9, 2020, 9:01pm UTC](https://community.plotly.com/t/app-callback-improvements-trigger-component-same-output-multiple-times-callback-without-output-serverside-store-callback-cache/42937/24 "2020-11-09T21:01:41Z")

</div>

Another improvement for the Input/State dependencies would be the ability to specify the resulting argument name in the dependency itself, the argument would then be passed to the function as named arguments (instead of positional arguments), the order of the Input/State dependency would then be irrelevant.  
The same could be done with the Output dependencies (giving them a name) and allowing callbacks to return a dict instead of a value (if mono output) or a tuple (if multiple output).

E.g. `Input("my-button", "nclicks", name=btn_clicks)` will pass an argument named btn\_clicks to the callback function.  
The callback function could then also accept \*\*kwargs if useful.  
With an `Output("my-graph","figure", "my_fig")`, the callback would return a `dict(my_fig=...)`.  
Using named inputs/outputs would make adapting the callback (adding/removing/moving dependencies) less troublesome.

An alternative (which would also be backward compatible) to adding a name argument to the dependency would be to pass a dict to the callback decorator like

```auto
@app.callback({"btn_clicks":Input("my-button", "nclicks"), ...})
def mycallaback(btn_clicks): ...

```

Or even named arguments like

`@app.callback(btn_clicks=Input("my-button", "nclicks"), ...)`

…

Yet, when passing a dict (instead of adding a name argument to Dependency), we should specify a dict for Input/State (goes into callaback signature) and a dict for Output (used for mapping the return value) as the same key name could be used as Input/State as well as Output.

---

<div class="post-metadata">

**Author:** ![sdementen](https://avatars.discourse-cdn.com/v4/letter/s/67e7ee/32.png) [@sdementen](https://community.plotly.com/u/sdementen)\
**Post date:** [November 9, 2020, 9:14pm UTC](https://community.plotly.com/t/app-callback-improvements-trigger-component-same-output-multiple-times-callback-without-output-serverside-store-callback-cache/42937/25 "2020-11-09T21:14:53Z")

</div>

Hi @chriddyp, ?  
Reading the link you provide re DAG mentions that a DAG has no cycles.  
Could you elaborate on why multiple callbacks outputting to the same component breaks the DAG/no cycle property?

[Previous page](https://community.plotly.com/t/app-callback-improvements-trigger-component-same-output-multiple-times-callback-without-output-serverside-store-callback-cache/42937.md?page=1)
