How to deal with multiple outputs from one callback? How to reset buttons clicked?

Hi awesome people!

I am creating a dashboard for my research project which will allow me to visually check the quality of the processing of hundreds of MRIs. The following is a screenshot of the components my dashboard has.

  1. The dashboard is divided by rows and each row has two Carousels (40 total) which automatically display a set of 3d brain surfaces for one scanning session for the chosen subject.
  2. At the top, the orange buttons are for choosing the subject. Consequently, when choosing a subject, all rows (carousels) have to update the images they display to match the subject chosen.
    3 The blue buttons in between each row with carousels, only update a textbox on the bottom right corner. This textbox allows me to take notes about which surfaces need to be reprocessed etc.

QUESTIONS:

  1. When I choose a subject, I need to update the 20 rows with the 40 carousels to display the 3D surfaces for that subject. I also need to update each set of blue buttons in between the rows containing the carrousels to match the scans of the subject chosen. What is the best way to update all these carousels and buttons when I am only using one callback? This is a screenshot of my callback. I only listed few outputs (ses-xx refers to 4 of the blue buttons and the CAROUSEL… refers to 4 of the carousels to be updated with the callback).

The following is how ugly the return of my function already looks like with just a few things I need to update with the callback.

I am sure there must be a better way to do what I am trying to do. I am somewhat new to DASH and would love to hear any suggestions. Thank you so much before hand for helping me out. :slight_smile:

Hi @numam, I recommend you read about Pattern-Matching Callbacks | Dash for Python Documentation | Plotly
it will show you a great way to handle input and outputs of the same type.

and also, you can return a list of outputs instead of writing them one by one.
meaning u can define a variable called OUTPUT_LIST
and append each element you want to return in the order you need to return them,
then just

return OUTPUT_LIST
2 Likes

Thank you for the info Matan. I’ll check out pattern matching. I have not heard about this!

Hey Matan! I checked the Pattern-Matching callbacks and I cannot see how it solves my problem. I am probably not getting how I can apply those dynamic callbacks to my particular problem. What found from the documentation, videos I watched and playing around with different examples is that pattern-matching callbacks still only gives me one output when having multiple inputs. In my case, I have one input (the buttons that switch between subjects) and multiple outputs to update the data being displayed (40 carousels and 15-20 set of buttons).

I basically need a way to update all the content of the dashboard, while retaining the components, when I choose a subject.

Further insights?

Let’s say you use pattern matching in your situation:

instead of

Output("ses-01", "options"),
Output("ses-02", "options"),
Output("ses-03", "options"),
Output("ses-04", "options"),
Output("ses-05", "options"),

You would give you elements a dictionary as an id =>

{"type":"ses", "index":1},
{"type":"ses", "index":2},
{"type":"ses", "index":3},
{"type":"ses", "index":4},
{"type":"ses", "index":5},

and when you need the output for the components you use this in the callback

Output({"type":"ses", "index":ALL},"options")

Then you can create a list of all the ses components and append each ses by creating it with its data and appending it to the list

ses_list = []
for ses_data in ses_data_list:
    ses_list.append(create_ses_options_with_data(ses_data))

return ses_list # => instead of returning each element by itself.
2 Likes

Hey Matan! You were right. It works! I did not get this from the documentation and videos I watched. Thank you so much for clarifying everything I needed to do. THANK YOU so much!

I am now facing a different issue now. Though the callbacks work perfectly, when I switch a couple of times the subject and update the carousels, it gets to a point where some carousels get frozen or do not display any image at all. However, I checked the inputs they receive and the inputs contain the information. Any idea about what could cause this behavior? I will create a new topic since it is a new issue. But if you have any insights, please let me know. I will highly appreciate it.

1 Like

Never mind! I found why. It happens that if the slide being played has a key x and the subject to which I switch does not happen to have a slide with key x also, it will not display the image.

1 Like