我正在尝试将Raspberry Pi的实时数据绘制到Web服务器。在这种情况下,我使用破折号(https://dash.plot.ly/live-updates)来执行此操作。我目前正在对Mac上的静态数据测试dash Web浏览器,到目前为止,它工作正常。除了关于plotly的弃用消息之外,并且可以为记录器“ werkzeug”找到“没有处理程序”,它完全可以正常工作。但是,当我尝试在树莓派上运行完全相同的脚本时,出现了问题。
在我的树莓派上,Web服务器运行,我可以在localhost上打开它,但是图表为空!!!我没有收到任何错误消息或发生这种情况的原因!
编辑:添加脚本
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)
所以我能够通过增加更新间隔来解决此问题。我猜在树莓派上,间隔需要更大,以便图表正确更新。很奇怪,但是行得通。