Robert Marciniak
Robert Marciniak

Reputation: 303

Dash Plotly triggering callbacks

I have a live-updating graph that connects to an SQL database. On page loading, the user inputs credentials and the appropriate data is loaded and displayed. I noticed that the lag time to load the data is related to the "dcc.interval" interval setting. This timer triggers the callback. However, when I added a "submit" button, to the callback input this does not trigger the callback, instead the callback awaits the "Interval" time before firing. My Question is: How can I get the callback to fire on button "submit" and not on 'interval' time on initial load?

dcc.Interval(
                    id='graph-update2',
                    interval=60000,
                    n_intervals=0),
@app.callback(
    dash.dependencies.Output('live-graph', 'figure'),
    [dash.dependencies.Input('graph-update-BP', 'n_intervals')],
    Input('submit-val', 'n_clicks'),
    Input('data_list', 'data'),
)
def update_graph_scatter_1(n,submit, data_list):
    if data_list == 'null' or data_list is None:
        raise PreventUpdate

    else: ....


Upvotes: 0

Views: 5165

Answers (1)

Nimit Gupta
Nimit Gupta

Reputation: 141

Use 'State' instead 'Input'. Inputs will trigger your callback; State do not. If you need the the current “value” - aka State - of other dash components within your callback, you pass them along via State.

           from dash.dependencies import Output,Input, State
          
           @app.callback(Output('live-graph', 'figure'),
                        [Input('submit-val', 'n_clicks')],
                        [State('graph-update-BP', 'n_intervals'),
                         State('data_list', 'data')]
                        )
           def update_graph_scatter_1(n_clicks, graph-update-BP, data_list):
                if n_clicks is not None and n_clicks > 0:
                   connection = cx.connect() #database connector
                   sql = '''''' #SQL Query


           

Upvotes: 2

Related Questions