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

Callback function based on multiple filter selection

Hi All,

I am facing some issues, I will be very thankful if someone helps me out.

  1. I am trying to filter a chart based on 2 dropdown’s selection in one callback function, is that possible if yes please help me with this.
    2)I have 2 columns in a data frame and with these 2 columns, I am creating a chart but want to filter that chart based on the 3rd column which is present in the parent data frame(from which my 2 column data frame is derived) is that possible if yes please help me with this.

Hi @Deepan, this is totally possible. The following code is the necessary callback structure written in R. If you provide some more details I can help you with a more specific solution.

app$callback(
  output('output_id', 'children'),
  params = list(
    input(id = 'dropdown_1', property='value'),
    input(id = 'dropdown_2', property='value')
  ),
  function(
    input.dropdown_1,
    input.dropdown_2
  )
  {
    df.sub <- df[column1 == input.dropdown_1 & column2 == input.dropdown_2]
    
    p1 <- plot_ly(df.sub, x=~xaxis, y=~yaxis)
    
    dccGraph(id = 'p1', figure = p1)
  }
)

Good luck.

Hi Peter

i am doing this on python, So could you please help me.

If you only struggle with the coding of the callback, then you will find your answer in this documentation:
https://dash.plotly.com/basic-callbacks.
This explains how to write callbacks in python.

The logic inside the callback/function is the same.

  1. filter the dataframe (exclude what you don’t want)
  2. then make the plot, so the plot only contains the values you want

Or do you struggle with more basic things like filtering the dataframe etc.?

It would be easier to help you if you would go more into details. Some sample date for example or a screenshot.


in this code i am using input from 1 dropdown but I want to filter on 2 dropdown selection,

where i am confuse is “where to write the logic to return data or condition for 2nd selector”

If you are using two dropdowns to filter only one column you could use one multi-value dropdown instead, thereby making it unnecessary to use the second dropdown. Otherwise you would just include the second dropdown like the first. How this looks like is in the documentation I linked in the previous answer.

i am using 2 dropdown for 2 diffrent column

just add another input