2017-10-04 95 views
1

我想在Python(3.x)中使用积极更新与阴谋破折号(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更新一次?

回答

0

基于对plotly forum的讨论,我找到了一个解决方案。您需要从serve_layout内部拨打gen_plot()以获取最新数字。为了调用gen_plot,您需要从中删除装饰器。

要调用gen_plotserve_layout,我在代码中移动它,并在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()参数;奇迹般有效!