Skip to content
Advertisement

Dash Dynamic Dropdown with Custom Option

I’m trying to create a dropdown menu that says ‘today’, ‘yesterday’, ‘last 7 days’ and ‘custom’. I want the calendar to automatically update when I choose an option in the dropdown menu. For ‘custom’ I want to pull the calendar so I can choose any dates I want. Here’s the sample code:

JavaScript

The dropdown and calendar show up on that code, however, they are not linked together. The dates in the calendar don’t change when I choose other options, it just stays in the date today by default.

Advertisement

Answer

You are almost there, just two small mistakes.

In your dropdown you are adding a new option to your dropdown {'label': 'Custom', 'value': 'None'}. However you are setting its value to None. The dropdown value are the ones we receive in the callback function as input [Input('timeframe_dropdown', 'value')]. So in your if-statement you are checking elif dropdown_value == 'Custom':. This will never be the case, as you set the Custom label to have value None.

Another little mistake is that we are declaring two Output values in the callback, both start_date and end_date, so we need to always return two values. elif dropdown_value == 'Custom': return None should instead be elif dropdown_value == 'Custom': return None, None.

Full code with fixes:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement