Option update works great, I see there is a bit of delay but it’s ok for my use case. Here is what I am doing, drawing chart using the data from CSV file
import pandas as pd
import plotly.graph_objects as go
import dash
import dash_html_components as html
import dash_core_components as dcc
from dash.dependencies import Input, Output
import os
from os import listdir
from collections import deque
yMinValue = 50
yMaxValue = 60
xMin = deque(maxlen=2)
xMax = deque(maxlen=2)
yMin = deque(maxlen=2)
yMax = deque(maxlen=2)
app = dash.Dash(__name__)
pathway = 'C:/temp'
files=[f for f in listdir(pathway) if f.endswith('csv')]
app.layout = html.Div(id='my-dropdown-parent',children=[
dcc.Dropdown(id='filename',placeholder="Select a file...",
style={'width': '30em', 'padding': '0em'}
),
dcc.Graph(id='output-graph')
])
@app.callback(Output('filename', 'options'), [Input('my-dropdown-parent', 'n_clicks')])
def updateOptionList(n_clicks):
if n_clicks is None:
raise dash.exceptions.PreventUpdate
files=[f for f in listdir(pathway) if f.endswith('csv')]
options=[
{'label': i, 'value': i} for i in files
]
return options
@app.callback(Output('output-graph', 'figure'), [Input('filename', 'value')])
def showGraph(n):
fig = go.Figure()
if n is not None:
with open(n) as f:
df = pd.read_csv(n, header=None, usecols=[0,1], names=['time', 'value'])
beginDate = df['time'].iloc[0]
endDate = df['time'].iloc[-1]
xMin.append(beginDate)
yMin.append(yMinValue)
xMin.append(endDate)
yMin.append(yMinValue)
xMax.append(beginDate)
yMax.append(yMaxValue)
xMax.append(endDate)
yMax.append(yMaxValue)
fig.add_trace(go.Scatter(x = df['time'], y = df['value'],
mode='lines',
line= dict(color="#66ff00")))
fig.add_trace(go.Scatter(x=list(xMin), y=list(yMin),
name= 'Minimum Threshold',
mode = 'lines',
connectgaps = True,
line = dict(color="yellow")))
fig.add_trace(go.Scatter(x=list(xMax), y=list(yMax),
name= 'Maximum Threshold',
mode = 'lines',
connectgaps = True,
line = dict(color="red")))
fig.update_layout(
title_text='Graph',
titlefont=dict(family='Courier New, monospace', size=20, color='#FFFFFF'),
legend=go.layout.Legend(
traceorder="normal",
font=dict(
family="Courier New, monospace",
size=12,
color="#FFFFFF"
),
xanchor= 'right',
borderwidth = 1
),
xaxis=go.layout.XAxis(
title_text="Time",
showgrid=True, gridcolor='#343434', dtick=3600
),
yaxis=go.layout.YAxis(
title_text="Value",
showgrid=True, gridcolor='#343434', dtick=0.01
)
)
return fig
if __name__ == '__main__':
app.run_server(debug=False,port=8090,host='0.0.0.0')