# For loop with callback

**URL:** <https://community.plotly.com/t/for-loop-with-callback/64731>\
**Category:** Dash Python\
**Created:** [June 2, 2022, 10:17pm UTC](https://community.plotly.com/t/for-loop-with-callback/64731 "2022-06-02T22:17:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thomasgg](https://avatars.discourse-cdn.com/v4/letter/t/dbc845/32.png) [@Thomasgg](https://community.plotly.com/u/Thomasgg)\
**Post date:** [June 2, 2022, 10:17pm UTC](https://community.plotly.com/t/for-loop-with-callback/64731/1 "2022-06-02T22:17:06Z")

</div>

Hi,

I am using Card to have a deck of financial asset that is composed of a candlestick graph and the change on the last 24h.  
I am able to do a comprehension list for building the layout of the card for each row. However I am not able to build the callback function in a way i just write one callback to populate the card because only the symbol of the financial asset changes not the graph themselve.

the callback is as follow but it is not working with the TypeError: update\_graph() missing 1 required positional argument: ‘symbol’.:

```auto
for symbol in symbols:
    @app.callback(
        [Output('indicator-graph-{}'.format(symbol), 'figure')],
        [Input('update', 'n_intervals')]
    )
    def update_graph(timer, symbol):
        # dff_rv = dff.iloc[::-1]
        day_start = candle_hourly[symbol][candle_hourly[symbol]['date'] == candle_hourly[symbol]['date'].min()]['close'].values[0]
        day_end = candle_hourly[symbol][candle_hourly[symbol]['date'] == candle_hourly[symbol]['date'].max()]['close'].values[0]
        # print(day_start)
        # print(day_end)

        fig = go.Figure(go.Indicator(
            mode="delta",
            value=day_end,
            delta={'reference': day_start, 'relative': True, 'valueformat': '.2%'}))
        fig.update_traces(delta_font={'size': 12})
        fig.update_layout(height=30, width=70)

        if day_end >= day_start:
            fig.update_traces(delta_increasing_color='green')
        elif day_end < day_start:
            fig.update_traces(delta_decreasing_color='red')
        return fig

```

What can I change to make it work? Is there a better way to write my callback?  
Read about dynamics callback but not sure how it would work.

---

<div class="post-metadata">

**Author:** ![chriddyp](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/chriddyp/32/16855_2.png) [@chriddyp](https://community.plotly.com/u/chriddyp)\
**Post date:** [June 2, 2022, 11:25pm UTC](https://community.plotly.com/t/for-loop-with-callback/64731/2 "2022-06-02T23:25:31Z")

</div>

Welcome to Dash!

> [@Thomasgg](#):
>
> ```auto
> [Input('update', 'n_intervals')]
> )
> def update_graph(timer, symbol):
> 
> ```

Your function has two arguments (`timer`, `symbol`), but you’ve only bound one UI input to the function (`Input('update', 'n_intervals')`.

So to start, remove `symbol` from `update_graph`. You’ll want `symbol` passed into the function “implicitly” via the local scope / closure. This can be tricky, unexpected things with how these work in with Python, see e.g. [function - Python, loops and closures - Stack Overflow](https://stackoverflow.com/questions/54288926/python-loops-and-closures). It may work by just removing `symbol` but you might have better luck refactoring to create a “function factory”.

Something like this:

```auto
def create_update_graph_function(symbol):
    def update_graph(timer):
         # the function you have above
         day_start =... 
         # this function can have `symbol` in it...
         print(symbol)
         # etc etc
     return update_graph # yup, returning a function!

for symbol in symbols:
     update_graph_function = create_update_graph_function(symbol)
     app.callback(Output(...), Input(...))(update_graph_function)

```

This can also be simplified as just:

```auto
for symbol in symbols:
     app.callback(Output(...), Input(...))(create_update_graph_function(symbol))

```

`create_update_graph(symbol)` _returns_ a function which is then wrapped by the callback.

I’m using an alternative syntax for callbacks that makes it easier to pass in “dynamically” created functions. With callbacks, this:

```auto
@callback(...)
def foo():
   pass

```

is the same as:

```auto
callback(...)(foo)

```

Or in the example in the example above, `app.callback(Output(...), Input(...))(create_update_graph(symbol))`

Welcome to the world of closures and decorators!

---

<div class="post-metadata">

**Author:** ![chriddyp](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/chriddyp/32/16855_2.png) [@chriddyp](https://community.plotly.com/u/chriddyp)\
**Post date:** [June 2, 2022, 11:33pm UTC](https://community.plotly.com/t/for-loop-with-callback/64731/3 "2022-06-02T23:33:53Z")

</div>

(Also my `...` aren’t anything special or specific to Python - it’s just a short hand for “fill in the blanks” 🙂 )

---

<div class="post-metadata">

**Author:** ![Thomasgg](https://avatars.discourse-cdn.com/v4/letter/t/dbc845/32.png) [@Thomasgg](https://community.plotly.com/u/Thomasgg)\
**Post date:** [June 3, 2022, 1:20pm UTC](https://community.plotly.com/t/for-loop-with-callback/64731/4 "2022-06-03T13:20:06Z")

</div>

@chriddyp Thank you. It works. Learnt something new. It’s great.  
Need to keep practicing this part now 🙂
