Plotly Dash 的首次更新与实时更新
First update of plotly Dash with live update
我正在尝试在 Python (3.x) 中使用 plotly Dash (0.18.3) 进行实时更新,但我不需要经常更新情节(一次或两次)我需要一天)。
到目前为止我做了以下事情:
import dash
import pandas as pd
import plotly.graph_objs as go
import dash.dependencies as ddp
import dash_core_components as dcc
import dash_html_components as html
def plot_figure(data):
layout = dict(
title="Figure w/ plotly",
)
fig = dict(data=data, layout=layout)
return fig
def serve_layout():
return html.Div(children=[
html.Div([
html.H1("Plotly test with Live update",
style={"font-family": "Helvetica",
"border-bottom": "1px #000000 solid"}),
], className='banner'),
html.Div([dcc.Graph(id='plot')],),
dcc.Interval(id='live-update', interval=interval),
],)
second = 1000
minute = 60
hour = 60
day = 24
interval = 1/2*day*hour*minute*second
app = dash.Dash()
app.layout = serve_layout
@app.callback(
ddp.Output('plot', 'figure'),
[],
[],
[ddp.Event('live-update', 'interval')])
def gen_plot():
ind = ['a', 'b', 'c', 'd']
df = pd.DataFrame({'one' : pd.Series([4., 3., 2., 1.], index=ind),
'two' : pd.Series([1., 2., 3., 4.], index=ind)})
trace = [go.Scatter(x=df.index, y=df['one'])]
fig = plot_figure(trace)
return fig
if __name__ == '__main__':
app.run_server(debug=True)
问题是一开始什么都没出现,interval
之后才更新,所以过了半天。我在 Dash documentation 后面添加了 serve_layout
函数,以便在页面加载时进行更新,但似乎没有效果。
如何在首次访问页面时进行首次更新,然后在每个 interval
时进行更新?
根据 plotly forum 上的讨论,我找到了解决方案。您需要从 serve_layout
内部调用 gen_plot()
以获得最新的数字。为了调用 gen_plot
,您需要从中删除装饰器。
要在 serve_layout
中调用 gen_plot
,我在代码中将其上移并在 serve_layout
中将 figure=gen_plot()
添加到 dcc.Graph
。
import dash
import pandas as pd
import plotly.graph_objs as go
import dash.dependencies as ddp
import dash_core_components as dcc
import dash_html_components as html
second = 1000
minute = 60
hour = 60
day = 24
interval = 1/2*day*hour*minute*second
def plot_figure(data):
layout = dict(
title="Figure w/ plotly",
)
fig = dict(data=data, layout=layout)
return fig
def gen_plot():
ind = ['a', 'b', 'c', 'd']
df = pd.DataFrame({'one' : pd.Series([4., 3., 2., 1.], index=ind),
'two' : pd.Series([1., 2., 3., 4.], index=ind)})
trace = [go.Scatter(x=df.index, y=df['one'])]
fig = plot_figure(trace)
return fig
def serve_layout():
return html.Div(children=[
html.Div([
html.H1("Plotly test with Live update",
style={"font-family": "Helvetica",
"border-bottom": "1px #000000 solid"}),
], className='banner'),
html.Div([dcc.Graph(id='plot', figure=gen_plot())],),
dcc.Interval(id='live-update', interval=interval),
],)
app = dash.Dash()
app.layout = serve_layout
app.callback(
ddp.Output('plot', 'figure'),
[],
[],
[ddp.Event('live-update', 'interval')])(gen_plot)
if __name__ == '__main__':
app.run_server(debug=True)
请注意,在我的实际应用程序中,我还需要使用我用图更新的文本摘录来执行此操作。我有一个 gen_text
函数,其装饰器与我的 gen_plot
函数相同,我应用了相同的策略,并向相关的 html.Div
添加了一个 children=gen_text()
参数;工作起来很有魅力!
我正在尝试在 Python (3.x) 中使用 plotly Dash (0.18.3) 进行实时更新,但我不需要经常更新情节(一次或两次)我需要一天)。
到目前为止我做了以下事情:
import dash
import pandas as pd
import plotly.graph_objs as go
import dash.dependencies as ddp
import dash_core_components as dcc
import dash_html_components as html
def plot_figure(data):
layout = dict(
title="Figure w/ plotly",
)
fig = dict(data=data, layout=layout)
return fig
def serve_layout():
return html.Div(children=[
html.Div([
html.H1("Plotly test with Live update",
style={"font-family": "Helvetica",
"border-bottom": "1px #000000 solid"}),
], className='banner'),
html.Div([dcc.Graph(id='plot')],),
dcc.Interval(id='live-update', interval=interval),
],)
second = 1000
minute = 60
hour = 60
day = 24
interval = 1/2*day*hour*minute*second
app = dash.Dash()
app.layout = serve_layout
@app.callback(
ddp.Output('plot', 'figure'),
[],
[],
[ddp.Event('live-update', 'interval')])
def gen_plot():
ind = ['a', 'b', 'c', 'd']
df = pd.DataFrame({'one' : pd.Series([4., 3., 2., 1.], index=ind),
'two' : pd.Series([1., 2., 3., 4.], index=ind)})
trace = [go.Scatter(x=df.index, y=df['one'])]
fig = plot_figure(trace)
return fig
if __name__ == '__main__':
app.run_server(debug=True)
问题是一开始什么都没出现,interval
之后才更新,所以过了半天。我在 Dash documentation 后面添加了 serve_layout
函数,以便在页面加载时进行更新,但似乎没有效果。
如何在首次访问页面时进行首次更新,然后在每个 interval
时进行更新?
根据 plotly forum 上的讨论,我找到了解决方案。您需要从 serve_layout
内部调用 gen_plot()
以获得最新的数字。为了调用 gen_plot
,您需要从中删除装饰器。
要在 serve_layout
中调用 gen_plot
,我在代码中将其上移并在 serve_layout
中将 figure=gen_plot()
添加到 dcc.Graph
。
import dash
import pandas as pd
import plotly.graph_objs as go
import dash.dependencies as ddp
import dash_core_components as dcc
import dash_html_components as html
second = 1000
minute = 60
hour = 60
day = 24
interval = 1/2*day*hour*minute*second
def plot_figure(data):
layout = dict(
title="Figure w/ plotly",
)
fig = dict(data=data, layout=layout)
return fig
def gen_plot():
ind = ['a', 'b', 'c', 'd']
df = pd.DataFrame({'one' : pd.Series([4., 3., 2., 1.], index=ind),
'two' : pd.Series([1., 2., 3., 4.], index=ind)})
trace = [go.Scatter(x=df.index, y=df['one'])]
fig = plot_figure(trace)
return fig
def serve_layout():
return html.Div(children=[
html.Div([
html.H1("Plotly test with Live update",
style={"font-family": "Helvetica",
"border-bottom": "1px #000000 solid"}),
], className='banner'),
html.Div([dcc.Graph(id='plot', figure=gen_plot())],),
dcc.Interval(id='live-update', interval=interval),
],)
app = dash.Dash()
app.layout = serve_layout
app.callback(
ddp.Output('plot', 'figure'),
[],
[],
[ddp.Event('live-update', 'interval')])(gen_plot)
if __name__ == '__main__':
app.run_server(debug=True)
请注意,在我的实际应用程序中,我还需要使用我用图更新的文本摘录来执行此操作。我有一个 gen_text
函数,其装饰器与我的 gen_plot
函数相同,我应用了相同的策略,并向相关的 html.Div
添加了一个 children=gen_text()
参数;工作起来很有魅力!