Error : UnboundLocalError: local variable 'fig' referenced before assignment

I’m having this error on the dash server, I believe this error is linked to the callback function
I’ve looked in several places and I don’t know how to solve it, you are my last chance :frowning:

opcoes = list(df['Colunas'].unique())

app.layout = html.Div(children=[

        html.H5('Opcões'),
        dcc.Dropdown(opcoes, value = 'ACS' , id='check_opcao'),

        dcc.Graph(figure=fig, id='grafico_gc_players')
        
        
        ])
@app.callback([
        Output('grafico_gc_players', 'figure'),
        
        ], 
                [
                Input('check_opcao', 'value'),
                
                ])

def renderizar_graficos(check_opcao):


    if check_opcao == 'ACS':
        fig_acs = px.bar(df, x='Player', y=check_opcao,  color='Player', barmode = 'stack', 
            labels={
                    'Player': 'Nome e Time'}, template='plotly_dark')
    else:
        tabela_filtrada = df.loc[df['Colunas'] == check_opcao,:]
        fig_geral = px.bar(tabela_filtrada, x='Player', y=check_opcao, color='Player', barmode = 'stack', 
            labels={
                    'Player': 'Nome e Time'}, template='plotly_dark')

    return fig_acs, fig_geral

@Luisanches,

Where you are putting dcc.graph in the app.layout, remove the statement

figure=fig

This should allow your callback to fire and fill in the figure as desired.

I removed the instruction but the error remains the same

UnboundLocalError: local variable 'fig_geral' referenced before assignment

Change the function to this:

def renderizar_graficos(check_opcao):


    if check_opcao == 'ACS':
        fig = px.bar(df, x='Player', y=check_opcao,  color='Player', barmode = 'stack', 
            labels={
                    'Player': 'Nome e Time'}, template='plotly_dark')
    else:
        tabela_filtrada = df.loc[df['Colunas'] == check_opcao,:]
        fig = px.bar(tabela_filtrada, x='Player', y=check_opcao, color='Player', barmode = 'stack', 
            labels={
                    'Player': 'Nome e Time'}, template='plotly_dark')

    return fig

I did that and the graph appeared again, but the callback doesn’t work and another error appeared…
I think the error is related to the way you built this function… but I don’t know how to identify the error

 Expected type: (<class 'tuple'>, <class 'list'>)
                Received value of type <class 'plotly.graph_objs._figure.Figure'>:
                    Figure({
    'data': [{'alignmentgroup': 'True',
              'hovertemplate': 'Nome e Time=%{y}<extra></extra>',
              'legendgroup': 'Bizinha  MIBR.F',
              'marker': {'color': '#636efa', 'pattern': {'shape': ''}},
              'name': 'Bizinha  MIBR.F',
              'offsetgroup': 'Bizinha  MIBR.F',
              'orientation': 'v',
              'showlegend': True,
              'textposition': 'auto',
              'type': 'bar',
              'x': array(['Bizinha  MIBR.F'], dtype=object),
              'xaxis': 'x',
              'y': array(['Bizinha  MIBR.F'], dtype=object),
              'yaxis': 'y'}],
    'layout': {'barmode': 'stack',
               'legend': {'title': {'text': 'Nome e Time'}, 'tracegroupgap': 0},
               'margin': {'t': 60},
               'template': '...',
               'xaxis': {'anchor': 'y',
                         'categoryarray': [Bizinha  MIBR.F],
                         'categoryorder': 'array',
                         'domain': [0.0, 1.0],
                         'title': {'text': 'Nome e Time'}},
               'yaxis': {'anchor': 'x',
                         'categoryarray': [Bizinha  MIBR.F],
                         'categoryorder': 'array',
                         'domain': [0.0, 1.0],
                         'title': {'text': 'Nome e Time'}}}
})

Try this. It looks like the issue is with you listing the outputs in the callback, ie ([Output(‘grafico_gc_players’, ‘figure’),]) I just removed this bit since you are just targeting one figure.

opcoes = list(df['Colunas'].unique())

app.layout = html.Div(children=[

        html.H5('Opcões'),
        dcc.Dropdown(opcoes, value = 'ACS' , id='check_opcao'),

        dcc.Graph(figure=fig, id='grafico_gc_players')
        
        
        ])
@app.callback(
        Output('grafico_gc_players', 'figure'),
                [
                Input('check_opcao', 'value'),
                
                ])

def renderizar_graficos(check_opcao):


    if check_opcao == 'ACS':
        fig = px.bar(df, x='Player', y=check_opcao,  color='Player', barmode = 'stack', 
            labels={
                    'Player': 'Nome e Time'}, template='plotly_dark')
    else:
        tabela_filtrada = df.loc[df['Colunas'] == check_opcao,:]
        fig = px.bar(tabela_filtrada, x='Player', y=check_opcao, color='Player', barmode = 'stack', 
            labels={
                    'Player': 'Nome e Time'}, template='plotly_dark')

    return fig

It worked! only that the graph has another problem, it is not showing the correct information. but this I’ll try to solve, if not I’ll open another topic, thanks for the help

I think you should change figure=fig to figure={}. I will return fig based on your dropdown.

Depends on if he already has a pre-defined figure that was passed as fig, but yes. You are right.