Search code examples
plotlyplotly-dashplotly-python

Plotly graph does not update if there isn't a value ''


I am trying to understand why do I get this error. error

I know it is because I have set the value = ' ' in the dropdown:

                       dcc.Dropdown(
                                    id = 'dd_tag_yaxis', 
                                    clearable = False,
                                    placeholder = 'Select tag x-axis',
                                    value = '',
                                    options=[ ], 
                                    style={
                                        'width': '100%'
                                })

However, what happens is that if I remove this value the figure that gets displayed when selecting an option in the dropdown does not appear anymore, and thus, using this value is the only way I have found to get the figure display working. An example of the callback I am using can be seen bellow.

@app.callback(
    Output("display", "figure" ),
    Input("dd_tag_yaxis", "value")
)

def update_figure(dd_tag_selected):
    zone = 'Zone 1'
    if (dd_tag_selected == ('TIC 3201_SP' or 'TIC 3201_PV' or 'TI_3207')):
        zone = 'Zone 1'
    elif (dd_tag_selected == ('TIC 3203_SP' or 'TIC 3203_PV' or 'TI_3208')):
        zone = 'Zone 2'
    elif (dd_tag_selected == ('TIC 3205_SP' or 'TIC 3205_PV' or 'TI_3208')):
        zone = 'Zone 3'

    print("Graph should be being returned")
    print(dd_tag_selected)
    fig = px.line(df, x=x_axis, y= df1['Oven Temperature Controllers [°C]'][zone][dd_tag_selected], title='Temperature Profile')

    fig.update_layout(transition_duration=500,  xaxis_title = 'Time [h]', yaxis_title = 'Temperature [°C]', )
    return fig

I have tried putting 'if dd_tag_selected == '': return [] ' inside the callback to avoid receiving this error, but again if I do that, (although the error disappears) the graph does not get displayed anymore.

@app.callback(
    Output("display", "figure" ),
    Input("dd_tag_yaxis", "value")
)

def update_figure(dd_tag_selected):
    if dd_tag_selected == '':
        return []
    
    zone = 'Zone 1'
    if (dd_tag_selected == ('TIC 3201_SP' or 'TIC 3201_PV' or 'TI_3207')):
        zone = 'Zone 1'
    elif (dd_tag_selected == ('TIC 3203_SP' or 'TIC 3203_PV' or 'TI_3208')):
        zone = 'Zone 2'
    elif (dd_tag_selected == ('TIC 3205_SP' or 'TIC 3205_PV' or 'TI_3208')):
        zone = 'Zone 3'

    print("Graph should be being returned")
    print(dd_tag_selected)
    fig = px.line(df, x=x_axis, y= df1['Oven Temperature Controllers [°C]'][zone][dd_tag_selected], title='Temperature Profile')

    fig.update_layout(transition_duration=500,  xaxis_title = 'Time [h]', yaxis_title = 'Temperature [°C]', )
    return fig

You can see what I mean with the graph display here:

  • Here the display works fine but having value = '' in the dropdown and the error: with_error

  • Here the display doesn't work but there isn't an error anymore because I have added the if statement. without_butNoDisplay


Solution

  • I am not sure if this solves the problem exactly the way you would like, but in your if statement, you can instead return an empty figure. I have done this in dash apps I have built as well.

    if dd_tag_selected == '':
       return go.Figure()