导入 dash
从 dash.dependencies 导入输出,输入
导入 dash_core_components 作为 dcc 导入 dash_html_components 作为 html
导入 plotly,随机
导入 plotly.graph_objs 作为 go
从 collections 导入 deque
从 pandas_datareader.data 导入 DataReader
导入时间,随机
app = dash.Dash('车辆数据')
max_length = 20
次 = 双队列(maxlen=max_length)
oil_temps = 双队列(maxlen=
max_length) intake_temps = 双队列(maxlen=max_length)
coolant_temps = 双队列(maxlen=max_length)
rpms = 双队列(maxlen=max_length)
速度 = 双队列(maxlen=max_length)
throttle_pos = 双队列(maxlen=max_length)
data_dict = {“油温”:oil_temps,
“进气温度”:intake_temps,
“冷却液温度”:coolant_temps,
“转速”:rpms,
“速度”:speeds,
“油门位置”:throttle_pos}
def update_obd_values(时间、油温、进气温度、冷却液温度、转速、速度、油门位置):
times.append(time.time())
if len(times) == 1:
#starting relevant values
oil_temps.append(random.randrange(180,230))
intake_temps.append(random.randrange(95,115))
coolant_temps.append(random.randrange(170,220))
rpms.append(random.randrange(1000,9500))
speeds.append(random.randrange(30,140))
throttle_pos.append(random.randrange(10,90))
else:
for data_of_interest in [oil_temps, intake_temps, coolant_temps, rpms, speeds, throttle_pos]:
data_of_interest.append(data_of_interest[-1]+data_of_interest[-1]*random.uniform(-0.0001,0.0001))
return times, oil_temps, intake_temps, coolant_temps, rpms, speeds, throttle_pos
时间、油温、进气温度、冷却液温度、转速、速度、油门位置 = update_obd_values(时间、油温、进气温度、冷却液温度、转速、速度、油门位置)
app.layout = html.Div([
html.Div([
html.H2('车辆数据',
style={'float': 'left',
}),
]),
dcc.Dropdown(id='vehicle-data-name',
options=[{'label': s, 'value': s}
for s in data_dict.keys()],
value=['冷却液温度','油温','进气温度'],
multi=True
),
html.Div(children=html.Div(id='graphs'), className='row'),
dcc.Interval(
id='graph-update',
interval=100),
], className="container",style={'width':'98%','margin-left':10,'margin-right':10,'max-width':50000})
@app.callback(
dash.dependencies.Output('graphs','children'),
[dash.dependencies.Input('vehicle-data-name',
'value'),dash.dependencies.Input('graph-update','n_intervals')]
)
def update_graph(data_names,n_intervals):
graphs = []
update_obd_values(时间、油温、进气温度、冷却液温度、转速、
速度、油门位置)如果 len(data_names)> 2:
class_choice ='col s12 m6
l4'elif len(data_names)== 2:
class_choice ='col s12 m6 l6'
否则:
class_choice ='col s12'
for data_name in data_names:
data = go.Scatter(
x=list(times),
y=list(data_dict[data_name]),
name='Scatter',
fill="tozeroy",
fillcolor="#6897bb"
)
graphs.append(html.Div(dcc.Graph(
id=data_name,
animate=True,
figure={'data': [data],'layout' : go.Layout(xaxis=dict(range=[min(times),max(times)]),
yaxis=dict(range=[min(data_dict[data_name]),max(data_dict[data_name])]),
margin={'l':50,'r':1,'t':45,'b':1},
title='{}'.format(data_name))}
), className=class_choice))
return graphs
外部css = [”https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css“]
对于 external_css 中的 css:
app.css.append_css({“external_url”: css})
external_js =
['https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/js/materialize.min.js']
对于 external_js 中的 js:
app.scripts.append_script({'external_url': js})
如果姓名== '主要的':
app.run_server(debug=True)