Linking Bar Charts and Drop-Downs
Learn how to incorporate bar charts and drop-downs into Dash applications.
We'll cover the following...
We'll cover the following...
We now want to put everything together that we’ve done so far. The plan is to have two drop-downs side by side with a chart underneath each. The first will provide years as options that will generate a horizontal bar chart. The second will generate a vertical bar chart based on the selected country. The end goal is to produce a new section in our app that looks like the illustration below:
Press + to interact
Building the application in JupyterLab
Let’s start by building this as a complete and independent app in JupyterLab and make sure it works as expected.
Press + to interact
# necessary importsfrom jupyter_dash import JupyterDashimport dash_html_components as htmlimport dash_core_components as dccimport dash_bootstrap_components as dbcfrom dash.dependencies import Output, Inputfrom dash.exceptions import PreventUpdateapp = JupyterDash(__name__)# create gini dataFramegini_df = poverty[poverty[gini].notna()]# create app layout and add componentsapp.layout = html.Div([html.Br(),html.H2('Gini Index - World Bank Data', style={'textAlign': 'center'}),html.Br(),dbc.Row([dbc.Col([dcc.Dropdown(id='gini_year_dropdown',options=[{'label': y, 'value': y}for y in gini_df['year'].drop_duplicates().sort_values()]),html.Br(),dcc.Graph(id='gini_year_barchart')]),dbc.Col([dcc.Dropdown(id='gini_country_dropdown',multi=True,options=[{'label': country, 'value': country}for country in gini_df['Country Name'].unique()]),html.Br(),dcc.Graph(id='gini_country_barchart')]),])])# create a callback. Input: year and returns a chart@app.callback(Output('gini_year_barchart', 'figure'),Input('gini_year_dropdown', 'value'))def plot_gini_year_barchart(year):if not year:raise PreventUpdatedf = gini_df[gini_df['year'].eq(year)].sort_values(gini).dropna(subset=[gini])n_countries = len(df['Country Name'])fig = px.bar(df,x=gini,y='Country Name',orientation='h',height=200 + (n_countries*20),title=gini + ' ' + str(year))return fig# create a second callback@app.callback(Output('gini_country_barchart', 'figure'),Input('gini_country_dropdown', 'value'))def plot_gini_country_barchart(countries):if not countries:raise PreventUpdatedf = gini_df[gini_df['Country Name'].isin(countries)].dropna(subset=[gini])fig = px.bar(df,x='year',y=gini,height=100 + (250*len(countries)),facet_row='Country Name',labels={gini: 'Gini Index'},color='Country Name',title=''.join([gini, '<br>', '<b>',', '.join(countries), '</b>']))return fig# run the appif __name__ == '__main__':app.run_server(mode='inline')
-
Lines 2–9: We first run the necessary imports and instantiate the app. We already ...