Last active
October 9, 2024 00:00
-
-
Save chriddyp/3d2454905d8f01886d651f207e2419f0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# See official docs at https://dash.plotly.com | |
# pip install dash pandas | |
from dash import Dash, dcc, html, Input, Output | |
import plotly.express as px | |
import pandas as pd | |
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/gapminderDataFiveYear.csv') | |
app = Dash(__name__) | |
app.layout = html.Div([ | |
dcc.Graph(id='graph-with-slider'), | |
dcc.Slider( | |
df['year'].min(), | |
df['year'].max(), | |
step=None, | |
value=df['year'].min(), | |
marks={str(year): str(year) for year in df['year'].unique()}, | |
id='year-slider' | |
) | |
]) | |
@app.callback( | |
Output('graph-with-slider', 'figure'), | |
Input('year-slider', 'value')) | |
def update_figure(selected_year): | |
filtered_df = df[df.year == selected_year] | |
fig = px.scatter(filtered_df, x="gdpPercap", y="lifeExp", | |
size="pop", color="continent", hover_name="country", | |
log_x=True, size_max=55) | |
return fig | |
if __name__ == '__main__': | |
app.run_server(debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Working example: