|
| 1 | +import dash |
| 2 | +import dash_core_components as dcc |
| 3 | +import dash_html_components as html |
| 4 | +from dash.dependencies import Input, Output |
| 5 | + |
| 6 | +import pandas as pd |
| 7 | + |
| 8 | +external_stylesheets = ["https://codepen.io/chriddyp/pen/bWLwgP.css"] |
| 9 | + |
| 10 | +app = dash.Dash(__name__, external_stylesheets=external_stylesheets) |
| 11 | + |
| 12 | +df = pd.read_csv("https://plotly.github.io/datasets/country_indicators.csv") |
| 13 | + |
| 14 | +available_indicators = df["Indicator Name"].unique() |
| 15 | + |
| 16 | +app.layout = html.Div( |
| 17 | + [ |
| 18 | + html.Div( |
| 19 | + [ |
| 20 | + html.Div( |
| 21 | + [ |
| 22 | + dcc.Dropdown( |
| 23 | + id="xaxis-column", |
| 24 | + options=[ |
| 25 | + {"label": i, "value": i} for i in available_indicators |
| 26 | + ], |
| 27 | + value="Fertility rate, total (births per woman)", |
| 28 | + ), |
| 29 | + dcc.RadioItems( |
| 30 | + id="xaxis-type", |
| 31 | + options=[ |
| 32 | + {"label": i, "value": i} for i in ["Linear", "Log"] |
| 33 | + ], |
| 34 | + value="Linear", |
| 35 | + labelStyle={"display": "inline-block"}, |
| 36 | + ), |
| 37 | + ], |
| 38 | + style={"width": "48%", "display": "inline-block"}, |
| 39 | + ), |
| 40 | + html.Div( |
| 41 | + [ |
| 42 | + dcc.Dropdown( |
| 43 | + id="yaxis-column", |
| 44 | + options=[ |
| 45 | + {"label": i, "value": i} for i in available_indicators |
| 46 | + ], |
| 47 | + value="Life expectancy at birth, total (years)", |
| 48 | + ), |
| 49 | + dcc.RadioItems( |
| 50 | + id="yaxis-type", |
| 51 | + options=[ |
| 52 | + {"label": i, "value": i} for i in ["Linear", "Log"] |
| 53 | + ], |
| 54 | + value="Linear", |
| 55 | + labelStyle={"display": "inline-block"}, |
| 56 | + ), |
| 57 | + ], |
| 58 | + style={"width": "48%", "float": "right", "display": "inline-block"}, |
| 59 | + ), |
| 60 | + ] |
| 61 | + ), |
| 62 | + dcc.Graph(id="indicator-graphic"), |
| 63 | + dcc.Slider( |
| 64 | + id="year--slider", |
| 65 | + min=df["Year"].min(), |
| 66 | + max=df["Year"].max(), |
| 67 | + value=df["Year"].max(), |
| 68 | + marks={str(year): str(year) for year in df["Year"].unique()}, |
| 69 | + step=None, |
| 70 | + ), |
| 71 | + ] |
| 72 | +) |
| 73 | + |
| 74 | + |
| 75 | +@app.callback( |
| 76 | + Output("indicator-graphic", "figure"), |
| 77 | + [ |
| 78 | + Input("xaxis-column", "value"), |
| 79 | + Input("yaxis-column", "value"), |
| 80 | + Input("xaxis-type", "value"), |
| 81 | + Input("yaxis-type", "value"), |
| 82 | + Input("year--slider", "value"), |
| 83 | + ], |
| 84 | +) |
| 85 | +def update_graph( |
| 86 | + xaxis_column_name, yaxis_column_name, xaxis_type, yaxis_type, year_value |
| 87 | +): |
| 88 | + dff = df[df["Year"] == year_value] |
| 89 | + |
| 90 | + return { |
| 91 | + "data": [ |
| 92 | + dict( |
| 93 | + x=dff[dff["Indicator Name"] == xaxis_column_name]["Value"], |
| 94 | + y=dff[dff["Indicator Name"] == yaxis_column_name]["Value"], |
| 95 | + text=dff[dff["Indicator Name"] == yaxis_column_name]["Country Name"], |
| 96 | + mode="markers", |
| 97 | + marker={ |
| 98 | + "size": 15, |
| 99 | + "opacity": 0.5, |
| 100 | + "line": {"width": 0.5, "color": "white"}, |
| 101 | + }, |
| 102 | + ) |
| 103 | + ], |
| 104 | + "layout": dict( |
| 105 | + xaxis={ |
| 106 | + "title": xaxis_column_name, |
| 107 | + "type": "linear" if xaxis_type == "Linear" else "log", |
| 108 | + }, |
| 109 | + yaxis={ |
| 110 | + "title": yaxis_column_name, |
| 111 | + "type": "linear" if yaxis_type == "Linear" else "log", |
| 112 | + }, |
| 113 | + margin={"l": 40, "b": 40, "t": 10, "r": 0}, |
| 114 | + hovermode="closest", |
| 115 | + ), |
| 116 | + } |
| 117 | + |
| 118 | + |
| 119 | +if __name__ == "__main__": |
| 120 | + app.run_server(debug=True) |
0 commit comments