✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
🐇 Announcing Dash VTK for 3d simulation graphics. Check out the March webinar.

html.Button as an out target in a callback

I’m trying to determine if a button can be the output target of a callback, that is, use the
callback output to fire the button to ‘click’?

For example, I’d like the button with id=‘target-button-id’ to be ‘clicked’ id the date-picker-range value(s) change:

        dash.dependencies.Output('target-button-id', '<some-button-attribute>'),
        [dash.dependencies.Input('date-picker-range', 'start_date'),
         dash.dependencies.Input('date-picker-range', 'end_date')]
    def click_button_if_date_changed(start_date, end_date):
        return <something-to-click-button>

You could try increasing the n_clicks attribute of the button?

Mike - thanks much for the suggestion. I have a follow-up question: how do you get the attributes, in this case, n_clicks, when the button is not in the input? That is, what is the return value if you don’t already know what n_clicks is equal to?

You can use State as an input:

dash.dependencies.Output(‘target-button-id’, ‘n_clicks’),
[dash.dependencies.Input(‘date-picker-range’, ‘start_date’),
dash.dependencies.Input(‘date-picker-range’, ‘end_date’)],
[dash.dependencies.State((‘target-button-id’, ‘n_clicks’)]
def click_button_if_date_changed(start_date, end_date, old_n_clicks):
return old_n_clicks + 1

This will of course only work if you have a second callback that takes (‘target-button-id’, ‘n_clicks’) as an input to do something else.

Spot on answer Mike! Thank you.