2017-10-04 3 views
1

플롯 대시 (0.18.3)를 파이썬 (3.x)으로 사용하여 라이브 업데이트를 사용하려고하지만 플롯을 너무 자주 업데이트하지 않아도됩니다 (한두 번 하루가 필요합니다.)라이브 업데이트로 플롯 대쉬의 첫 번째 업데이트

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에 전화하려면 데코레이터를 제거해야합니다.

내가 코드에서 그것을 이동, serve_layout 내부 gen_plot를 호출하고 serve_layoutdcc.Graphfigure=gen_plot()를 추가합니다.

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_plot 함수와 동일한 장식자를 사용하여 gen_text 함수를 사용했지만 동일한 전략을 적용하고 children=gen_text() 인수를 관련 html.Div에 추가했습니다. 매력처럼 작동합니다!

관련 문제