✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
🧬 Learn how to build RNA-Seq data apps with Python & Dash. Register for the May 20 Webinar!

Error in App an Key Error - Fixed it! (white space after column title was the issue)

Hi,

Having issues getting my dashboard with multiple inputs to work. It will output a graph with no data and give me two error messages:

2018-04-19 10:14:56,269] ERROR in app: Exception on /_dash-update-component [POST]
KeyError: ‘Value’

There is a column named Value so not sure why I’m getting the key error when I try to reference it. Here is the code:

app = dash.Dash(’’)

df = pd.read_csv(‘https://docs.google.com/spreadsheets/d/e/2PACX-1vRDIJ4xZAhMef7xtREBukYfvV5A1aL08mV1DrqIRiGvRCPF7eYCpq6C63XcmyKhxZKIyNd9lmvKGlz9/pub?output=csv’)

available_indicators = df[‘Indicator Name’].unique()

app.layout = html.Div([
html.Div([

    html.Div([
        dcc.Dropdown(
            id='xaxis-column',
            options=[{'label': i, 'value': i} for i in available_indicators],
            value='ODA'
        ),
        dcc.RadioItems(
            id='xaxis-type',
            options=[{'label': i, 'value': i} for i in ['Linear', 'Log']],
            value='Linear',
            labelStyle={'display': 'inline-block'}
        )
    ],
    style={'width': '48%', 'display': 'inline-block'}),

    html.Div([
        dcc.Dropdown(
            id='yaxis-column',
            options=[{'label': i, 'value': i} for i in available_indicators],
            value='INCOME INDEX'
        ),
        dcc.RadioItems(
            id='yaxis-type',
            options=[{'label': i, 'value': i} for i in ['Linear', 'Log']],
            value='Linear',
            labelStyle={'display': 'inline-block'}
        )
    ],style={'width': '48%', 'float': 'right', 'display': 'inline-block'})
]),

dcc.Graph(id='indicator-graphic'),

dcc.Slider(
    id='year--slider',
    min=df['YEAR'].min(),
    max=df['YEAR'].max(),
    value=df['YEAR'].max(),
    step=None,
    marks={str(year): str(year) for year in df['YEAR'].unique()}
)

])

@app.callback(
dash.dependencies.Output(‘indicator-graphic’, ‘figure’),
[dash.dependencies.Input(‘xaxis-column’, ‘value’),
dash.dependencies.Input(‘yaxis-column’, ‘value’),
dash.dependencies.Input(‘xaxis-type’, ‘value’),
dash.dependencies.Input(‘yaxis-type’, ‘value’),
dash.dependencies.Input(‘year–slider’, ‘value’)])
def update_graph(xaxis_column_name, yaxis_column_name,
xaxis_type, yaxis_type,
year_value):
dff = df[df[‘YEAR’] == year_value]

return {
    'data': [go.Scatter(
        x=dff[dff['Indicator Name'] == xaxis_column_name]['Value'],
        y=dff[dff['Indicator Name'] == yaxis_column_name]['Value'],
        text=dff[dff['Indicator Name'] == yaxis_column_name]['REGION'],
        mode='markers',
        marker={
            'size': 15,
            'opacity': 0.5,
            'line': {'width': 0.5, 'color': 'white'}
        }
    )],
    'layout': go.Layout(
        xaxis={
            'title': xaxis_column_name,
            'type': 'linear' if xaxis_type == 'Linear' else 'log'
        },
        yaxis={
            'title': yaxis_column_name,
            'type': 'linear' if yaxis_type == 'Linear' else 'log'
        },
        margin={'l': 40, 'b': 40, 't': 10, 'r': 0},
        hovermode='closest'
    )
}

if name == ‘main’:
app.run_server(debug=False)