Black Lives Matter. Please consider donating to Black Girls Code today.

Create callback using for loop

Hi,

my dash app has multiple graph (each scene has 1 graph), i would like to create a callback for each graph using the same input. can i use for loop to do so?

for scene in scenarios:
  @app.callback(
    dash.dependencies.Output(f'trend-{scene}', 'figure'),
    [dash.dependencies.Input('select-block', 'value')]
  )
  def update_figure(block)

i just want to clarify my problem statement.
i am creating an apps which contain 10 graphs for each scenario with similar callback input. instead of creating the 10 function which is troublesome i wish i can do a function factory kind of implementation. do you have any idea how to achieve it?

This is part of my code, i have function factory called “func_factory_4_timing_trend” and i want to create a callback function for each corner.

for corner in corners:
    @app.callback(
        dash.dependencies.Output('trend-{corner}', 'figure'),
        [dash.dependencies.Input('select-par', 'value')]
      )
     func_factory_4_timing_trend(corner)

it return as an error =.=
func_factory_4_timing_trend(corner)
^
SyntaxError: invalid syntax

that is because you need to define a function.


This thread should be helpful with your issue.

2 Likes

thanks. i decided to lump everything into a big callback function :slight_smile:

1 Like