Black Lives Matter. Please consider donating to Black Girls Code today.
Dash HoloViews is now available! Check out the docs.

Dash multi-selection options

I am trying to sum data using multi-selection options as the input and outputting a bar graph.

Right now, the code I have is just summing for the default values set for the dropdowns. It is summing instances of where type = A and ind = 0. However, when I select additional options into the dropdowns, the callback is not recognizing those outputs and is not automatically updating the data.

test_table

import dash
import dash_core_components as dcc
import dash_html_components as html

import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline

from plotly import version
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
import plotly.plotly as py
import plotly.graph_objs as go
import plotly.figure_factory as FF
import numpy as np

df = pd.read_excel(‘test_file.xlsx’, ‘Sheet1’)

type_options = df[‘TYPE’].unique()
ind_options = df[‘IND’].unique()

app = dash.Dash()

app.layout = html.Div(children=[
html.H1(children=‘Dashboard’, style={
‘textAlign’: ‘center’,
}),
html.P(‘Type’),
dcc.Dropdown(
id=‘typePicker’,
options=[{‘label’: i, ‘value’: i} for i in type_options],
value=‘A’,
multi=True
),

html.P('Ind'),
dcc.Dropdown(
    id='indPicker',
    options=[{'label': i, 'value': i} for i in ind_options],
    value='0',
    multi=True
),

dcc.Graph(
    id='sales-graph'
)

])

@app.callback(
dash.dependencies.Output(‘sales-graph’, ‘figure’),
[dash.dependencies.Input(‘typePicker’, ‘value’),
dash.dependencies.Input(‘indPicker’, ‘value’)])
def update_graph(typeP, indP):

test_graph = df[df['TYPE'].isin([typeP]) & df['IND'].isin([indP])]
myArray = np.array(test_graph['April_sales'])
total = np.sum(myArray)

return {

    'data': [
        go.Bar(
            x = ['April Sales'],
            y = [total]
        ) 
    ],
    'layout': go.Layout(
        yaxis={'title': 'sales'},
        barmode='stack',
        hovermode='closest'
    )
}

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

The error I’m getting is “ERROR in app: Exception on /_dash-update-component [POST]”
and “TypeError: unhashable type: ‘list’”