Why does my function fire even though its initial call is prevented and it requires a button n_click?

Why would the function below fire right away even though, like the working function preceding it, the callback includes a “prevent initial call” statement and the execution depends on the click of a submit button?

The pie chart pops up on the screen without any other output.

As a secondary question, what does the statement “px.data.tips()” - as seen in these examples - mean in the creation of a pie chart? Assuming it points to a data source, how am I to use it? Can I always simply replace it with an expression like pd.read_cvs?

Thank you for reviewing my questions.

The section of the app.layout that contains the pie chart for now:

html.Div(dcc.Graph(id ='fat_to_calories_ratio_output'))

The function and callback:

@app.callback(
    Output('fat_to_calories_ratio_output', 'figure'),
    Input('nutrients-review-datatable-submit-button', 'n_clicks'),
    State('nutrients-review-datatable', 'data'),
    prevent_initial_call=True
)

def fat_calories_pie_chart(n_clicks, data):

  with open ('./json/final_totals_list.json', 'r') as final_list:
    pie_data = json.load(final_list)
    for each_dict in pie_data:
        calories = (each_dict["Total Calories"])
        fat = (each_dict["Total Fat"])
        ratio = (fat/calories)
        fat_percentage = "{:.0%}".format(ratio)

        print("Ratio Fat to Calories: ", fat_percentage)

        labels = ['Fat','Calories']
        values = [fat, calories]
        fig = go.Figure(data=[go.Pie(labels=labels, values=values)])
        fig.show()

Hi @robertpfaff,

It is hard to say why the callback is being executed without knowing if the submit button exists in the initial callback and what is the click count… In most cases prevent_initial_call=True is enough, but there are some other reasons why it might not be enough, for instance, if the button is added by another callback that is triggered on the initial call. Please take also a look in this part of the docs, where all the cases are explained.

As for your second question, px.data.tips() just import one of the sample dataframes available in plotly. You can see how these functions are defined here. And yes, you can replace to another pandas dataframe, like you mentioned.

Hope it helps!

1 Like

Hey @robertpfaff

To stop the pie chart from showing, remove statement:

fig.show()
2 Likes

Hi there.

Yes, it helps very much - thank you. Adam dropped a video yesterday that explained a lot, which may help solve the callback problem. I have read the Advanced Callbacks page of the docs, but I definitely need to read it again…and probably again. Between the holiday chaos, while absorbing DASH info as quickly as possible, the attrition has been considerable.

Thanks again for responding to my question.

Robert

Duh! So obvious. Of course, that worked. Thank you.

Hope you had a safe and happy holiday.

Robert

Thanks for sharing this information. It was very useful.

1 Like