Forked from chriddyp/dash_simple_example_pandas_datareader.py
Created
May 5, 2025 15:45
-
-
Save nc007-cloud/7c80f74dcb05af165487a9c7cbecd9ac to your computer and use it in GitHub Desktop.
This file contains hidden or 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