plotly / dash

Data Apps & Dashboards for Python. No JavaScript Required.

Home Page:https://plotly.com/dash

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[BUG] callback in infinite loop but only triggered once

yxc8 opened this issue · comments

commented

Here is a minimal example of how it breaks.

Error appears as the app keeps loading forever and debug mode suggests 'Maximum Depth Exceeded', indicating the callback is in an infinite loop. However, printing out the trigger_id suggests the callback is only triggered once and everything before return is ran once. This is almost impossible to debug since the messages don't give any actionable feedback.

The real problem seems to be the 0 Amount: changing it to non-zero OR changing if x['Type'] == 'C' to if x['Type'] == 'C' and float(x['Amount']) fixes the front-end problem.

However, I don't understand why this would be a problem if get_data() is only called once in the callback and the callback is only triggered once. Maybe it is stuck updating 0 to -0 and -0 to 0 forever? But what is causing *(-1) to be applied over and over again when it is only supposed to be called once?

It seems to relate to componentDidUpdate function here: Dash GitHub Link, but I don't have any JS knowledge to further understand this. Please help, thanks!

import dash_ag_grid as dag
import pandas as pd
from dash import dcc, html, Input, Output, no_update
import dash

def get_data():
    df = pd.DataFrame([{'Amount': 11111.0, 'Type': 'C'}, {'Amount': 0.0, 'Type': 'C'}])
    df['Amount'] = df.apply(lambda x: float(x['Amount']) * (-1) if x['Type'] == 'C' else float(x['Amount']), axis=1)
    return df

app = dash.Dash(__name__)
app.layout = dcc.Loading([html.Div(id='placeholder'), dag.AgGrid(id='datatable', columnDefs=[{'field': 'Amount'}]),
                          dcc.Store(id='data_store', storage_type='session')])


@dash.callback(
    Output('placeholder', 'children'),
    Output('datatable', 'rowData'),
    Output('data_store', 'data'),
    Input('placeholder', 'children')
)
def update_table(dummy):
    # trigger_id = dash.callback_context.triggered[0]['prop_id'].split('.')[0]
    # trigger_property = dash.callback_context.triggered[0]['prop_id'].split('.')[1] if trigger_id else ""
    # print(f"Callback triggered: {trigger_id}, {trigger_property}")
    df_all = get_data()
    cached_data = df_all.to_dict('records')
    return no_update, df_all.to_dict('records'), cached_data


if __name__ == '__main__':
    app.run_server(debug=True)
commented

Thank you for reporting this, @yxc8 .
I can confirm that I'm getting the same error message, using your code. Like you said, if I update the amount to 0.5 -- {'Amount': 0.0, 'Type': 'C'} -- it seems to work.
I'm using PyCharm, Windows 11, and the Brave browser.

Hi @Coding-with-Adam
On Ubuntu and Chrome I can't reproduce the error.

Can I work on this issue?

Hi @SarthakNikhal

Thanks for your offer to help!
One thing you could do is to see if you can reproduce the error. The code posted above works fine for me. If you do get an error, could you say what version of Dash, Pandas and Python you are using, or any other info that might help?