import dash
from dash import dcc, html, Input, Output, State
import plotly.express as px
df = px.data.gapminder()
years = sorted(df["year"].unique())
app = dash.Dash(__name__)
app.layout = html.Div([
dcc.Slider(id="year", min=years[0], max=years[-1], value=years[0],
marks={y: str(y) for y in years}, step=None),
html.Button("▶ Play", id="play"),
dcc.Interval(id="tick", interval=800, disabled=True),
dcc.Graph(id="map"),
dcc.Graph(id="scatter")
])
@app.callback(
Output("tick", "disabled"),
Output("play", "children"),
Input("play", "n_clicks"),
State("tick", "disabled"),
prevent_initial_call=True
)
def toggle_play(_, disabled):
return (not disabled, "⏸ Pause" if disabled else "▶ Play")
@app.callback(
Output("year", "value"),
Input("tick", "n_intervals"),
State("year", "value"),
prevent_initial_call=True
)
def advance(_, current):
i = years.index(current)
return years[(i + 1) % len(years)]
@app.callback(
Output("map", "figure"),
Output("scatter", "figure"),
Input("year", "value")
)
def update_figures(year):
dfx = df[df["year"] == year]
map_fig = px.choropleth(dfx, locations="iso_alpha", color="lifeExp",
hover_name="country", title=f"Life Expectancy {year}")
scatter_fig = px.scatter(dfx, x="gdpPercap", y="lifeExp", size="pop",
color="continent", hover_name="country",
log_x=True, title=f"GDP vs Life Expectancy {year}")
return map_fig, scatter_fig
if __name__ == "__main__":
app.run_server(debug=True)