I have this code which works well when it start, and when i select a different year. However when I select and then delete the year and want to go back to the default value which is ‘All Years’ (which works upon starting up),
I get an error: KeyError: (‘P04’, 1)
If someone can help me out id really appreciate it
app = dash.Dash()
app.css.append_css({
"external_url": "https://codepen.io/chriddyp/pen/bWLwgP.css"
})
app.layout = html.Div([
html.H2("Encuesta Continua de Empleo Paraguay"),
html.Div([
dcc.Dropdown(
id="Manager",
options=([{'label': year, 'value': year } for year in df['year'].unique()]),
value='All Years',
className='three columns' )
],),
# Empty
html.Div(className='twelve columns'),
html.Div(
dcc.Graph(id='funnel-graph'),
className='six columns')
])
@app.callback(
dash.dependencies.Output('funnel-graph', 'figure'),
[dash.dependencies.Input('Manager', 'value')])
def update_graph(year):
if year == "All Years":
df_plot = df.copy()
else:
df_plot = df[df['year'].isin([year])]
pv = pd.pivot_table(
df_plot,
index=['trimestre'],
columns=["A15"],
values=['P04'],
aggfunc=len,
fill_value=0)
trace1 = go.Bar(x=pv.index, y=pv[('P04',1)], name='Empleado / obrero público')
trace2 = go.Bar(x=pv.index, y=pv[('P04',2)], name='Empleado / obrero privado')
trace3 = go.Bar(x=pv.index, y=pv[('P04',3)], name='Empleador o patrón')
trace4 = go.Bar(x=pv.index, y=pv[('P04',4)], name='Trabajador por cuenta propia')
#trace5 = go.Bar(x=pv.index, y=pv[('P04',5)], name=4)
trace6 = go.Bar(x=pv.index, y=pv[('P04',6)], name='Empleado doméstico')
return {
'data': [trace1, trace2, trace3, trace4, trace6],
'layout':
go.Layout(
title='Categoría o posición que tenía en su última ocupación {}'.format(year),
barmode='group')
}
if __name__ == '__main__':
app.run_server(debug=True)