简体   繁体   中英

Plotly Dash dcc.Interval fails after a while: Callback error updating graph.figure

I'm trying to set my Dash app to automatically pull the latest data from a .csv file used in the data frame with dcc.Interval . The error code isn't providing a detailed explanation and also doesn't always appear. I've tried this with both a button and a set 6 sec interval, but the result seems to be the same. The Dash app runs fine at first and refreshes fine a few times, then error starts occurring:

Callback error updating graph.figure

import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import plotly.express as px
import plotly.graph_objects as go
import pandas as pd

app = dash.Dash(__name__)
server = app.server

df = pd.read_csv('example.csv', encoding="WINDOWS-1252")

app.layout = html.Div([
    dcc.Graph(id='graph'),
    dcc.Interval(
        id='interval-component',
        interval=1*6000,
        n_intervals=0
    )
])

@app.callback(
    Output('graph','figure'),
    [Input('interval-component', 'n_intervals')]
)

def update_df(n):
    updated_df = pd.read_csv('example.csv', encoding="WINDOWS-1252")
    
    fig = px.scatter(updated_df, x='Date', y='Deviation', height=800)
    
    fig.update_layout(
        yaxis_tickformat = '.0%', 
    )

    fig.update_xaxes(
        rangeslider_visible=True,
        rangeselector=dict(
        )
    )
    
    return fig

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

I think your issue must have something to do with your file specifically, because the following code based exactly off as you provide (with the exception of generation of random matching-df timeseries data), works perfectly updating with an interval of every 6 seconds:

import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import plotly.express as px
import plotly.graph_objects as go
import pandas as pd
import numpy as np

np.random.seed(2019)


def get_random_deviation_ts_df(N=100):
    rng = pd.date_range("2019-01-01", freq="D", periods=N)
    df = pd.DataFrame(np.random.rand(N, 1), columns=["Deviation"], index=rng)
    df["Date"] = df.index
    return df


app = dash.Dash(__name__)
server = app.server

# df = pd.read_csv('example.csv', encoding="WINDOWS-1252")

app.layout = html.Div(
    [
        dcc.Graph(id="graph"),
        dcc.Interval(
            id="interval-component", interval=1 * 6000, n_intervals=0
        ),
    ]
)


@app.callback(
    Output("graph", "figure"), [Input("interval-component", "n_intervals")]
)
def update_df(n):
    updated_df = (
        get_random_deviation_ts_df()
    )  # pd.read_csv('example.csv', encoding="WINDOWS-1252")

    fig = px.scatter(updated_df, x="Date", y="Deviation", height=800)

    fig.update_layout(yaxis_tickformat=".0%",)

    fig.update_xaxes(rangeslider_visible=True, rangeselector=dict())

    return fig


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

间隔时间序列随机数据

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM