Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 带回调的Plotly Dash Live Update引发LayoutInDefined异常_Python_Plotly Dash - Fatal编程技术网

Python 带回调的Plotly Dash Live Update引发LayoutInDefined异常

Python 带回调的Plotly Dash Live Update引发LayoutInDefined异常,python,plotly-dash,Python,Plotly Dash,我遵循Dash网站上的指南,在页面刷新时对其进行实时更新: import datetime import dash import dash_html_components as html def serve_layout(): return html.Div([ dcc.Interval(id="time_trigger", interval=1000), html.H1('The time is: ' + str(datetime.datetime.

我遵循Dash网站上的指南,在页面刷新时对其进行实时更新:

import datetime

import dash
import dash_html_components as html

def serve_layout():
    return html.Div([
        dcc.Interval(id="time_trigger", interval=1000),
        html.H1('The time is: ' + str(datetime.datetime.now()), id="header")])

app.layout = serve_layout

if __name__ == '__main__':
    app.run_server(debug=True)
但是,这确实适用于回调。如果执行以下操作,将引发LayoutSnotDefined:

@app.callback(
    Output("header", "children"),
    [Input("time_trigger", "n_intervals")]
)
def connect_to_date_sync_service(n_interval):
    return "Interval is triggered {} times".format(n_interval)
这是Dash引发的错误:

dash.exceptions.LayoutIsNotDefined: 
Attempting to assign a callback to the application but
the `layout` property has not been assigned.
Assign the `layout` property before assigning callbacks.
Alternatively, suppress this warning by setting
`suppress_callback_exceptions=True`

此代码适用于我:

import datetime
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output

app = dash.Dash()

def serve_layout():
    return html.Div([
        dcc.Interval(id="time_trigger", interval=1000),
        html.H1('The time is: ' + str(datetime.datetime.now()), id="header")])

app.layout = serve_layout


@app.callback(
    Output("header", "children"),
    [Input("time_trigger", "n_intervals")]
)
def connect_to_date_sync_service(n_interval):
    return "Interval is triggered {} times".format(n_interval)

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

以下是我用作参考的软件包版本:

# Name                    Version                   Build  Channel
dash                      1.11.0             pyh9f0ad1d_0    conda-forge
dash-bootstrap-components 0.9.2              pyh9f0ad1d_0    conda-forge
dash-core-components      1.9.1              pyh9f0ad1d_0    conda-forge
dash-daq                  0.4.0                      py_0    conda-forge
dash-html-components      1.0.3              pyh9f0ad1d_0    conda-forge
dash-renderer             1.4.0              pyh9f0ad1d_0    conda-forge
dash-table                4.6.2              pyh9f0ad1d_0    conda-forge

你试过运行
app.run_server(debug=True,dev_tools\u ui=True,dev_tools\u props\u check=True)
,我认为是回调验证导致了问题?dev_tools\u ui和dev_tools\u props\u check都默认为True,因此不需要指定它们