更新回调以作图

时间:2019-10-01 07:35:02

标签: python apache-kafka plotly-dash

我一直在一起研究Dash和kafka。 我能够以图表的形式打印从Kafka用户那里收到的值。 在这里我创建了Kafka Consumer对象,并从obj中循环调用数据以从Kafka Consumer中获取数据。

当没有产生数据时,它会抛出错误说明:

Callback error updating example-graph.figure

我要停止此错误或处理此错误,显示一些消息,指出“未收到数据”

代码:

import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output,State
from dash.exceptions import PreventUpdate
import matplotlib.pyplot as plt
from kafka import KafkaConsumer
import pandas as pd
from datetime import datetime
consumer = KafkaConsumer('mytest',bootstrap_servers=['myip:9092'])

def consume(msg):
   time2 = []
   msg1 = str(msg)
   data_frame = msg1
   data_frame = data_frame.reindex(data_frame.index.drop(0))
   return data_frame

 app = dash.Dash(__name__)
 app.config.suppress_callback_exceptions = True
 c=pd.DataFrame()
 global d 
 d = pd.DataFrame()
#print(d['time'])
app.layout =html.Div([
    html.Div([ 
    html.Center(
    html.H1('Data Visualization Tool')),
    html.Hr()]),
html.Div(children=[
html.Center(html.H3(children='Vehicle Speed', id='first')),
dcc.Interval(id='timer', interval=1000),
html.Div(id='dummy'),
dcc.Graph(
        id='example-graph',
    )],className="five columns"),])

@app.callback([Output('example-graph', 'figure')],
          inputs=[Input('timer', 'n_intervals')])

def update_graph(n_clicks):
print(n_clicks)
global d
    for index,msg in enumerate(consumer):
        c = consume(msg.value)
        d = d.append(c,ignore_index = True)
        d = d[-20:]
        return {
                        'data': [{'x': d['1'],'y':  d['2'],'type': 'line', 'name': 'Data1'},]}
  }             
if __name__ == '__main__':
app.run_server(debug=True)

它曾经回调1000ms。并在屏幕上显示错误

Callback error updating example-graph.figure

当生产者停止时,图形应显示任何不包含数据或无法接收数据的消息。

谢谢。

0 个答案:

没有答案