Accessing current year from slider in choropleth in callback

Hi everyone,

I have dashboard with various graphs in a chained callback. One of them is a choropleth map with a slider for the years of my time-series data (1975-2014). Now I want to access the current year of the slider to update the graphs in the chained callback as the slider moves. I suppose there is not way to access the animation_frame of a simple plotly express choropleth (although that would be the easiest solution), so I created a plotly graph_object :
fig = go.Figure(data=[trace_base], layout=layout, frames=frames)
The whole thing is wrapped in a function that I call in the callback function. Is there a way to return the current year as part of the function or any other way to access the current year?
Actually the choropleth even displays the current year as part of the slider arguments but all I can access is the first year not the changing year as it loops through years.


            'active': 0,
            'yanchor': 'top',
            'xanchor': 'left',
            'currentvalue': {
                'font': {'size': 20},
                'prefix': 'Year:',
                'visible': True,
                'xanchor': 'right'
            'transition': {'duration': 300, 'easing': 'cubic-in-out'},
            'pad': {'b': 10, 't': 50},
            'len': 0.9,
            'x': 0.1,
            'y': 0,
            'steps': [{'method': 'animate', 'label': str(year), 'args': [[str(year)], {'frame': {'duration': 300, 'redraw': True}, 'mode': 'immediate'}]} for year in years]

Or is it better practice to have the slider as a separate dash component rather than using the slider of the choropleth?

I’m new to this so any help with this would be much appreciated!


Just to confirm, when you say current year, you’re trying to find the current mark value?

If so, in the simple slider example in the documentation, there’s a code snippet with a callback that takes the slider’s currently selected value and outputs it to a html.Div .

Slider | Dash for Python Documentation | Plotly

You could also look into the updatemode property.

updatemode (a value equal to: ‘mouseup’ or ‘drag’ ; default 'mouseup' ): Determines when the component should update its value property. If mouseup (the default) then the slider will only trigger its value when the user has finished dragging the slider. If drag , then the slider will update its value continuously as it is being dragged. If you want different actions during and after drag, leave updatemode as mouseup and use drag_value for the continuously updating value.
Slider | Dash for Python Documentation | Plotly

I hope that helps.

Happy graphing,