Live DASH 表 he

问题描述 投票:0回答:0

我正在构建一个 DASH 应用程序,它从传感器读取数据并自动更新其信息。现在,我正在阅读一个时间序列多元完整数据集,只是为了开发应用程序。 DASH 应用程序使用数据集中的列标题填充下拉列表,然后图形根据下拉选择“实时”绘制数据,该图每秒更新数据集中的下一个数据点,使其成为“实时”阴谋”。应用程序上的表格已经填满了所有数据。我的目标是一次从集合中读取一行数据,并随着数据绘制在图表上(随时间)填充表格,使其成为“实时”更新的“实时表格”。尚未编码的 DASH 应用程序还附有一个按钮。该按钮的功能是生成一个随机行,用随机行填充表格并用随机点更新绘图。这组随机数字假设超出正常范围并生成故障条件并向用户发出警告。

import plotly.express as px
import pandas as pd
import dash
from dash.dependencies import Output, Input
from dash import dcc, html, dash_table
import plotly
import plotly.graph_objs as go
from collections import deque
import csv
import json
import time
import sys
sys.path.append('S:\\Projects\\LogLab\\LogSIL\\_code_base')
from Interop.Send import Send


#In[1]

# Load the data into a pandas dataframe
df = pd.read_csv('S:\Projects\LogLab\LogSIL\_store\\navalplantmaintenance.csv',delim_whitespace=True, header=None )
df = df.round(3)
df.columns = ['lever_position', 'ship_speed', 'gt_shaft', 'gt_rate', 'gg_rate', 'sp_torque', 'pp_torque',
    'hpt_temp', 'gt_c_i_temp', 'gt_c_o_temp', 'hpt_pressure', 'gt_c_i_pressure', 'gt_c_o_pressure',
    'gt_exhaust_pressure', 'turbine_inj_control', 'fuel_flow', 'gt_c_decay', 'gt_t_decay']
df = df.dropna()
df.insert(0, 'index', range(1, len(df) + 1))
df = df.iloc[:, :-2]



X = deque(maxlen=20)
X.append(0)
Y = deque(maxlen=20)
Y.append(0)

#In[2]

# Define the app
app = dash.Dash(__name__)

# Define the layout
app.layout = html.Div([
    html.H1('Naval Plant Data Visualization'),
    html.H4('Select a Sensor'),
    dcc.Dropdown(
        id='dropdown',
        options=[{'label': col, 'value': col} for col in df.columns],
        value=df['fuel_flow'],
        multi = False,
        clearable = False,
        style = {"width": "50%"}
    ),    
    html.H4(children='Introduce Anamoly'),
    html.Button(
        'Generate Fault', 
        id='button',
        n_clicks = 0
    ),
    html.Br(),
    html.H2('Data and Prediciton Plot'),
    dcc.Graph(id='live-graph', animate=True),
    dcc.Interval(
        id='graph-update',
        interval=1000,
        n_intervals = 0
    ),
    html.Br(),
    html.H2('Data Table'),
    dash_table.DataTable(
        id='table',
        columns=[{"name": i, "id": i} for i in df.columns],
        # data=df.to_dict('records'), 
        style_table={'height': '300px', 'overflowY': 'auto'}), 
])

#In[3]
# Update Graph

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

def update_graph_scatter(n, selected_value):
    dff1 = df

    X.append(X[-1]+1)
    Y.append(dff1[selected_value][X[-1]])

    data = plotly.graph_objs.Scatter(
        x=list(X),
        y=list(Y),
        name='Scatter',
        mode= 'lines+markers'
        )

    return {'data': [data],'layout' : go.Layout(xaxis=dict(range=[min(X),max(X)]),
        yaxis=dict(range=[min(Y),max(Y)]),)}

#In[4]
# Update Table
# Input time(graph-update), output table

@app.callback(
    Output('table', 'children'),
    [Input('graph-update', 'n_intervals')]
)

def update_table(n):
    # get the current row
    for index, row in df.iterrows():
        return [row]
    # append current row to table
    # return the updated table
    

#In[5]
# Random Anamoly
# input button, time output table, graph


#In[6]

# Run the app
if __name__ == '__main__':
    app.run_server(debug=False)
python real-time plotly-dash live-streaming
© www.soinside.com 2019 - 2024. All rights reserved.