Search code examples
pythonplotlydashboard

Create a minor category selection based on the selected item in major category in python


Create a minor category selection based on the selected item in major category in python

Example: Major category = Month selection (Jan, Feb, Mar) Minor category = Date selection (01/01/2022, 02/01/2022, 03/01/2022, ...)

month_category = list(df['Month'].unique())

app.layout = dbc.Container([
dbc.Row([ html.H3('Month'),
        html.Br(),
         dcc.Dropdown(id='month_dd', value= 'Jan',
                      options = [{'label':x, 'value':x} 
                                for x in month_category],
                      
                      searchable = True, search_value='',
                      placeholder= 'Please select ...'
                      
                      ),
        html.Br(),
        
        html.H3('Date'),
        html.Br(),
        
        dcc.Dropdown(id='date_dd')
        
            ])
        ])

@app.callback(
Output('date_dd','options'),
Input('month_dd', 'value')
)


def update_dd (month_dd):
      month_date= df['Month','Settlement_Date'].drop_duplicates()
      relevant_date= month_date[month_date['Month']== month_dd]['Settlement_Date'].values.tolist()
      date_option= [dict(label=x,value=x)for x in relevant_date]   

      return date_option

All the data are come from a .csv file. There are columns stored the related data such as Month and Date. Each date have categorized under each month.

Currently, there is no data able to select in date category (empty in the date category).

Can anyone assist on this?


Solution

  • Should be df.drop_duplicates(['Month','Settlement_Date'], inplace= False) instead of df['Month','Settlement_Date'].drop_duplicates()

    Code:

    @app.callback(
        Output('date_dd','options'),
        Input('month_dd', 'value')
        )
    
    
     def update_dd (month_dd):
           month_date= df.drop_duplicates(['Month','Settlement_Date'], inplace= False)
           relevant_date= month_date[month_date['Month']== month_dd]['Settlement_Date'].values.tolist()
           date_option= [dict(label=x,value=x)for x in relevant_date] 
    
     return date_option