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()