Hi everyone!
I am new in Python and plotly/dash. Now I am working on small Dash app for a project I have taken over for my master.
I am facing a problem with the live graph that I want to plot. When I run the app, the graph contains all the data from my csv file and do not gradually plotting them.
Below you can find some data from my csv file and my code.
Regarding the data, I have a csv file from which have 3 columns:
[Path_id]: a way or track laid down for cars
[Timestapm]: the date and time that the Path_id has been traveled
[Duration]: in sec
Path_id | Timestamp | Duration |
---|---|---|
1 | 2019-09-01 00:00:00.000 | 270 |
2 | 2019-09-01 00:00:00.000 | 210 |
3 | 2019-09-01 00:00:00.000 | 190 |
4 | 2019-09-01 00:00:00.000 | 379 |
5 | 2019-09-01 00:00:00.000 | 170 |
1 | 2019-09-01 00:15:00.000 | 330 |
2 | 2019-09-01 00:15:00.000 | 210 |
3 | 2019-09-01 00:15:00.000 | 141 |
4 | 2019-09-01 00:15:00.000 | 285 |
5 | 2019-09-01 00:15:00.000 | 191 |
1 | 2019-09-01 00:30:00.000 | 292 |
2 | 2019-09-01 00:30:00.000 | 210 |
3 | 2019-09-01 00:30:00.000 | 139 |
4 | 2019-09-01 00:30:00.000 | 285 |
5 | 2019-09-01 00:30:00.000 | 200 |
here you can see my code:
import dash
import dash_core_components as dcc
import dash_html_components as html
from pandas import read_csv
import plotly.graph_objects as go
import pandas as pd
from dash.dependencies import Input, Output
app = dash.Dash()
data1 = read_csv(r'C:\Users\admin\Desktop\travel_times09_2019.csv', delimiter=';')
travel_time1 = data1[data1['Path_id'] == 1]
travel1 = pd.DataFrame(travel_time1, columns=["Timestamp", "Duration"])
travel_time2 = data1[data1['Path_id'] == 2]
travel2 = pd.DataFrame(travel_time2, columns=["Timestamp", "Duration"])
app.layout = html.Div(
html.Div([
html.Div([
dcc.Graph(
id='line_graph', style={'width': 1200}
),
dcc.Interval(
id='interval-component',
interval=1*900000,
n_intervals=0
)
])
])
)
@app.callback(Output('line_graph', 'figure'),
[Input('interval-component', 'n_intervals')])
def update_graph(n):
fig = go.Figure(
data = [
go.Scatter(
x=travel1['Timestamp'],
y=travel1['Duration'],
mode='lines',
opacity=0.7,
marker={
'size': 15,
'line': {'width': 0.5, 'color': 'white'}
},
name='path_01'),
go.Scatter(
x=travel2['Timestamp'],
y=travel2['Duration'],
mode='lines',
opacity=0.7,
marker={
'size': 15,
'line': {'width': 0.5, 'color': 'blue'}
},
name='path_02')
])
return fig
if __name__ == '__main__':
app.run_server(debug=True)
Any advice and guidance would be greatly appreciated!
Thank you in advance.