Callback function on page load

Hi,

I’ve got a callback function to update a chart. It takes in a dataframe from a hidden div.

The problem is that on page load the dataframe is empty and the code breaks because of this. The dataframe will only contain data once the user makes a selection from a dropdown menu.

I only want the chart update callback function to run after the user makes a selection from a dropdown menu.

I have put in an if statement to test whether the dataframe is empty. That stops the syntax errors but now it looks like the callback function doesn’t fire when a user makes a selection. Can someone please advise what I am doing wrong?

@app.callback(
dash.dependencies.Output(‘payoff_graph’, ‘figure’),
[dash.dependencies.Input(‘filtered_df’, ‘children’)]
)
def update_chart(json_data):
df_filter = pd.read_json(json_data, orient=‘split’)

if not df_filter.empty:
    issuer = df_filter[df_filter['Security Type'] == 'OS']['Security'].values[0]
    min_price = 0
    max_price = max(df_filter['Market Price'].max(), df_filter['Excercise Price'].max()) * 1.2
    prices = np.arange(min_price, max_price, 0.01)
    df_payoff = pd.DataFrame(index=prices)

replace your if by “if json_data is None: raise PreventUpdate”

This is (I thinck) the right way to prevent callback being fire at page load.

If this doesn’t fixe your situation, tell me :slight_smile:

1 Like

thanks it seemed like the issue was that I had to return something ie an empty json string or an empty figure to make sure the callback function would trigger again.

1 Like