【问题标题】:Dash/Plotly chart doesn't show when run from Raspberry PI从 Raspberry PI 运行时不显示 Dash/Plotly 图表
【发布时间】:2019-10-09 16:21:43
【问题描述】:

我正在尝试绘制从 Raspberry Pi 到 Web 服务器的实时数据。在这种情况下,我使用破折号 (https://dash.plot.ly/live-updates) 来执行此操作。我目前正在我的mac上测试我的dash web浏览器的静态数据,到目前为止它工作得很好。除了关于 plotly 的弃用消息,并且可以为记录器“werkzeug”找到无处理程序之外,它完全可以正常工作。但是当我尝试在我的树莓派上运行完全相同的脚本时,我遇到了问题。

在我的树莓派上,网络服务器运行,我可以在本地主机上打开它,但是图表是空的!!!我没有收到错误消息或发生这种情况的原因!

编辑:添加脚本

import os, sys
from pathlib import Path
currDir = Path(os.path.abspath(__file__))
_rootDir = currDir.parent.parent
# _dataDir = str(_rootDir / 'GridBallast-RaspberryPI' / 'data')
_dataDir = str(_rootDir / 'data')


import ETL
import datetime
import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly
from dash.dependencies import Input, Output

external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.layout = html.Div(
    html.Div([
        html.H4('RaspberryPI Sensor Charts'),
        html.Div(id='live-update-text'),
        dcc.Graph(id='live-update-graph'),
        dcc.Interval(
            id='interval-component',
            interval=1*1000, # in milliseconds
            n_intervals=0
        )
    ])
)


@app.callback(Output('live-update-text', 'children'),
              [Input('interval-component', 'n_intervals')])
def update_metrics(n):
    Temperature, Time = ETL.extractLastTemp(os.path.join(_dataDir, 'tempData.csv'))
    style = {'padding': '2px', 'fontSize': '16px'}
    return [
        html.Span('Temperature: {}'.format(Temperature), style=style),
        html.Span('Time: {}'.format(Time), style=style)
    ]


@app.callback(Output('live-update-graph', 'figure'),
              [Input('interval-component', 'n_intervals')])

def update_graph_live(n):

    #Load Temp Data 
    tempDict = ETL.etlTransform(os.path.join(_dataDir, 'tempData.csv'))
    lightDict = ETL.etlTransform(os.path.join(_dataDir, 'lightData.csv'))
    currentDict = ETL.etlTransform(os.path.join(_dataDir, 'currentData.csv'))
    # Create the graph with subplots
    fig = plotly.tools.make_subplots(rows=3, cols=1, vertical_spacing=0.2, subplot_titles=("Temperature", "Light", "Current"))
    fig['layout']['margin'] = {
        'l': 30, 'r': 10, 'b': 30, 't': 25
    }
    fig['layout']['legend'] = {'x': 0, 'y': 1, 'xanchor': 'left'}
    fig['layout']['height'] = 900

    fig.append_trace({
        'x': tempDict.keys(),
        'y': tempDict.values(),
        'name': 'Temp vs. Time',
        'mode': 'lines+markers',
        'type': 'scatter'
    }, 1, 1)
    fig.append_trace({
        'x': lightDict.keys(),
        'y': lightDict.values(),
        # 'text': data['time'],
        'name': 'Light vs. Time',
        'mode': 'markers',
        'type': 'scatter'
    }, 2, 1)
    fig.append_trace({
        'x': currentDict.keys(),
        'y': currentDict.values(),
        # 'text': data['time'],
        'name': 'Current vs. Time',
        'mode': 'markers',
        'type': 'scatter'
    }, 3, 1)

    return fig


def main():
    app.run_server(debug = True)

if __name__ == '__main__':
    print(_dataDir)
    print(os.listdir(_dataDir))
    main()
    # app.run_server(debug=True)

【问题讨论】:

  • 你能发布你的情节代码吗?另外,你的 pi 和你的 mac 有相同的静态数据和文件结构,对吧?
  • 是的,文件结构是一样的。我知道这一点是因为我打印了一个 os.listdir(_dataDir) 并返回了预期的结果,即 _dataDir 目录中的数据文件列表。
  • 您的pi 可以上网吗?在你的update_graph_live 函数中创建它以确保它有数据之后,你能print(tempDict, lightDict, currentDict) 吗?
  • 请将其简化为minimal reproducible example,即每个人都可以抓住并运行以查看您的问题。此外,它在树莓派上运行或数据来自哪里有什么关系?最后,请在应用标签之前阅读标签的描述,例如使用“linux”标签表明你没有。
  • @MasonCaiby 是的,在 update_graph_live 函数中打印字典可以正常工作。但网上的图表保持不变。我还认为数据文件可能包含太多条目,所以我让它们只包含几个条目。这些解决方案都不起作用。我还要补充一点,该选项卡会不断地“更新……”,就像在 Mac 上一样,该选项卡在读取“破折号”和“更新……”之间交替。它似乎 RPI 版本似乎永远不会在浏览器中更新。

标签: python linux raspberry-pi plotly-dash werkzeug


【解决方案1】:

所以我能够通过增加更新间隔来解决这个问题。我猜在树莓派上,间隔需要更大才能使图表正确更新。很奇怪,但它有效。

【讨论】:

    猜你喜欢
    • 2019-06-25
    • 2022-07-30
    • 2021-12-24
    • 1970-01-01
    • 1970-01-01
    • 2021-02-28
    • 2021-11-04
    • 2021-08-26
    • 2021-11-14
    相关资源
    最近更新 更多