问题描述
Hiyah!我一直在尝试实时绘制ping命令的响应,发现dash模块是最有用的。不幸的是,我对模块本身没有太多经验。我浏览了文档以及大量示例,并能够构建足以打开具有正确轴的图的代码,但是该图没有移动。我想知道是因为数据没有正确地添加到列表中,还是因为我没有标识正确的参数。如果某些在实时绘图或破折号方面具有更多经验的pyGenius可以看一下我的代码并提出一些建议,我将不胜感激。
代码流如下:
- 通过 ping -t 拨打给定地址;
- 接收数据,并通过 re 进行过滤,仅获得响应时间数字;
- 将过滤后的数据追加到列表中,同时将本地时间记录在另一个列表中;
- 将数据应用于散点图,然后进行绘制。
def initiate(self):
import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.graph_objs as go
import plotly
from dash.dependencies import Input,Output
ip_v4 = '10.0.0.0'
try:
app = dash.Dash(__name__)
app.layout = html.Div(
[
dcc.Graph(id='live-graph',animate=True),dcc.Interval(
id = 'graph-update',interval=1000,n_intervals=0
)
]
)
response = subprocess.Popen(['ping','-t',ip_v4],stdout=subprocess.PIPE,stderr=subprocess.PIPE)
timeout = time.time() + 15
listen_loop = True
while listen_loop:
list = []
list_t = []
if timeout < time.time():
listen_loop = False
else:
value = response.stdout.readline()
value = value.decode("utf-8")
value = re.findall(r"(?s)(?=time=(\d+))",value)
cur_time = time.strftime("%H:%M:%s",time.localtime())
@app.callback(
Output('live-graph','figure'),[Input('graph-update','n_intervals')]
)
def update_graph_scatter(n):
if value == []:
list.append(0)
list_t.append(cur_time)
else:
list.append(int(value[0]))
list_t.append(cur_time)
data = plotly.graph_objs.Scatter(
x=list_t,y=list,name='Scatter',mode='lines+markers'
)
return {'data':[data],'layout':go.Layout(xaxis=dict(
range=[
min(list_t),max(list_t)]),yaxis=dict(range=[min(list),max(list)
]),)}
if __name__ == '__main__':
app.run_server()
except:
print('Error!')
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)