Key Errors when selecting Dropdown Values

Hello everyone -

I am learning python / dash and trying to make a stacked bar plot that is filterable by a column value “Manager” - I am able to generate the default stacked bar plot (for value = "All Managers" ) upon initialization, but I get a key error when trying to revert back to the default after selecting a given manager (by clicking the x on the dropdown).

I also get key error when selecting those managers that do not have data for all the “stacks”.

For example, for Manager #5, I get a key error for ‘Personal Training’ if there are no “Personal Training” data points in the “Activity Type” column for Manager #5.

On the other hand, for Manager #2, the stacked bar chart shows data for that manager with no error, but then when I click the x to clear the dropdown, the value does not revert to its initial value.

Regarding the key error I get when resetting the dropdown, I was able to workaround by converting mgr_options to a list, inserting the initial value “All Managers” into the list, and set ‘clearable’ to False.

Regarding the key error I get when selecting a manager without column values for each type of “Activity Type”,I’ve tried wrapping the Go.Bar definitions with if statements, but no luck. Any advice?

Below is my code - and here is a link to a dummy xlsx file that can be used to run the app.

https://github.com/tiguere/Dash/blob/master/dummy_data.xlsx

import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.graph_objs as go
import pandas as pd
from dash.dependencies import Input, Output

external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
movement_activity_file = 'PATH_TO_FILE'

df = pd.read_excel(movement_activity_file,
sheet_name="Activity Report")

df = df.loc[:,['Activity Type','Group','Manager']]
mgr_options = df["Manager"].unique()


app.layout = html.Div(children=[
    html.H1(children='Sample Text'),
    html.Div(children='''Sample Text'''),
    html.Div(
    [
    dcc.Dropdown(
        id="Manager",
        options=[{
            'label': i,
            'value': i
        } for i in mgr_options],
        value = 'All Managers'),
    ],
    style={'width': '25%',
           'display': 'inline-block'}),
    dcc.Graph(id='activity-graph'),
])


@app.callback(
    dash.dependencies.Output('activity-graph', 'figure'),
    [dash.dependencies.Input('Manager', 'value')])

def update_graph(Manager):
    if Manager == "All Managers":
        df_plot = df.copy()
    else:
        df_plot = df[df['Manager'] == Manager]

    sum_typs = pd.pivot_table(df_plot,
        index=['Group'],
        columns = ['Activity Type'], 
        aggfunc=len,
        fill_value=0)


    grp_trn = go.Bar(x=sum_typs.index, y=sum_typs['Manager']['Group Training'], name='Group Training')
    phn_cll = go.Bar(x=sum_typs.index, y=sum_typs['Manager']['Phone Call'], name='Phone Call')
    gym_ent = go.Bar(x=sum_typs.index, y=sum_typs['Manager']['Entry to Gym'], name='Entry to Gym')
    prs_ntr = go.Bar(x=sum_typs.index, y=sum_typs['Manager']['Personal Nutrition'], name='Personal Nutrition')
    grp_emp = go.Bar(x=sum_typs.index, y=sum_typs['Manager']['Group Empowerment'], name='Group Empowerment')
    prs_trn = go.Bar(x=sum_typs.index, y=sum_typs['Manager']['Personal Training'], name='Personal Training')
    grp_ntr = go.Bar(x=sum_typs.index, y=sum_typs['Manager']['Group Nutrition'], name='Group Nutrition')

    return{
        'data': [grp_trn, phn_cll, gym_ent ,prs_ntr, grp_emp, prs_trn, grp_ntr],
        'layout':
        go.Layout(
            title='Activities by {}'.format(Manager), 
            barmode='stack')
    }

if __name__ == '__main__':
    app.run_server(debug=True)

Hi @tiguere, can you post a link to your dataset (PATH_TO_FILE) or if it’s not possible create an example using dummy data so that we can run the app? Thanks!

1 Like

I’ve edited my post with a link to a dummy xlsx file