Show the latest month and date in option based on dataframe

Hi,

How the option can show / display the latest date according to the dataframe?

Example:
Today 03/02/2022 which is the latest date and month from the dataframe.

There have a month selection and a date selection which is based on the month selection.

Data:

Settlement_Date Month
01/01/2022 Jan
02/01/2022 Jan
03/01/2022 Jan
04/01/2022 Jan
01/02/2022 Feb
02/02/2022 Feb
03/02/2022 Feb

Code:

 dbc.Col([
        html.H3('Month'),
        html.Br(),
        
        dcc.Dropdown(id='month_dd', value= '',
                      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', value='',
                     searchable = True, search_value='',
                     placeholder= 'Please select ...')
        
            ])

Expected the dropdwon can auto detect and display the latest month and date in dropdown.

Hey @beginof ,

If I understand your question correctly, you can achieve this with " Chained Callbacks".

https://dash.plotly.com/basic-callbacks

Write a callback for first dropdown. When user selects a month from first dropdown you can pass its value as an input to second callback. Then you can filter your data and send its result as an output to second dropdown.

Have a nice day.

Hi @akroma,

Thanks for the solution.

Yes, the chained callback came out the result is what I expected.

If want to set the default value as the latest date within the month, how can I make it?

The default latest month, I am using .max to perform. Plan to use the same way, but cant make it.
Any advise?

dcc.Dropdown(id='month_dd', value= df['Month'].max(),
                      options = [{'label':x, 'value':x} 
                                for x in month_category]
# create dropdown and display latest month based on df   
    dcc.Dropdown(id='month_dd', value= df['Month'].max(),
                  options = [{'label':x, 'value':x} 
                            for x in month_category],                     
                  ),

         dcc.Dropdown(id='date_dd', value=''
   
        ])

# create date dropdown according to month dropdown list
@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


#set last item as default value according to month dropdown list
@app.callback(
    Output('date_dd','value'),
    Input('date_dd', 'options')
    )


def default_value(latest_date):
    value = latest_date[-1]['value']

    return value