Hello guys,
I am getting call back errors while generating graph for multidropdowns for the below table. I want the Country, State, City and Restaurants to be the multi-dropdown filters and year slider for the x-axis. I want to plot a bar chart of sales and total quantity. below is my code. Please ignore the scatter plot I have mentioned in the code. I would really appreciate any help as it is pretty urgent. Thanks in advance.
my code:
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import pandas as pd
import plotly.graph_objs as go
external_stylesheets = [‘https://codepen.io/chriddyp/pen/bWLwgP.css’]
app = dash.Dash(name, external_stylesheets=external_stylesheets)
df = pd.read_csv(r’Desktop\testBook4.csv’)
COU = df[‘Country’].unique()
STA = df[‘State’].unique()
CIT = df[‘City’].unique()
RES = df[‘Restaurants’].unique()
app.layout = html.Div([
html.Div([
html.Div([
dcc.Dropdown(
id='dd1',
multi=True,
options=[{'label': i, 'value': i} for i in COU],
value='USA'
),
],
style={'width': '20%', 'display': 'inline-block'}),
html.Div([
dcc.Dropdown(
id='dd2',
multi=True,
options=[{'label': i, 'value': i} for i in STA],
value='NJ'
),
],
style={'width': '20%', 'display': 'inline-block'}),
html.Div([
dcc.Dropdown(
id='dd3',
multi=True,
options=[{'label': i, 'value': i} for i in CIT],
value='jersey city'
),
],
style={'width': '20%', 'display': 'inline-block'}),
html.Div([
dcc.Dropdown(
id='dd4',
multi=True,
options=[{'label': i, 'value': i} for i in ag],
value='bww'
),
],style={'width': '20%', '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(),
marks={str(year): str(year) for year in df['Year'].unique()},
step=None
)
])
@app.callback(
Output(‘indicator-graphic’, ‘figure’),
[Input(‘year–slider’, ‘value’)])
def update_figure(selected_year):
filtered_df = df[df.Year == selected_year]
traces = []
for i in filtered_df.BG.unique():
df_by_COU = filtered_df[filtered_df[‘Country’] == i]
traces.append(go.Scatter(
x=df_by_COU[‘Total Quantity’],
y=df_by_COU[‘Sales’],
mode=‘markers’,
opacity=0.7,
marker={
‘size’: 15,
‘line’: {‘width’: 0.5, ‘color’: ‘white’}
},
name=i
))
for j in filtered_df.State.unique():
df_by_STA = filtered_df[filtered_df['State'] == j]
traces.append(go.Scatter(
x=df_by_STA['Total Quantity'],
y=df_by_STA['Sales to Thirds'],
mode='markers',
opacity=0.7,
marker={
'size': 15,
'line': {'width': 0.5, 'color': 'white'}
},
name=j
return {
'data': traces,
'layout': go.Layout(
xaxis={'type': 'log', 'title': 'X'},
yaxis={'title': 'Y', 'range': [20, 90]},
margin={'l': 40, 'b': 40, 't': 10, 'r': 10},
legend={'x': 0, 'y': 1},
hovermode='closest'
)
}
if name == ‘main’:
app.run_server(debug=True)