Hello
I have two graphs, both line graphs. X axis is date, Y axis is hours of day
Once i run code, i see both graphs as expected, here I have not selected any points on first graph (trend_grapgh).
Callback is triggered , i do see parameter values printed on console
Then i select section on trend graph and expect selection data to be sent to callback.
I dont see callback triggered , print of selection is still none
What am i missing here?
Code below:
import pandas as pd
import plotly.express as px
import dash
from dash import dcc,html
from dash.dependencies import Input, Output, State
import dash_bootstrap_components as dbc
df = pd.read_csv ('TrendData.csv')
df['USER_EXTRACT_END_TIME'] = pd.to_datetime(df['USER_EXTRACT_END_TIME'], format='%H:%M:%S')
df['BATCH_END_TIME'] = pd.to_datetime(df['BATCH_END_TIME'], format='%H:%M:%S')
df['LAST_FILE_RECEIVED'] = pd.to_datetime(df['LAST_FILE_RECEIVED_HHMI'], format='%H:%M:%S')
df['COB_DT']=pd.to_datetime(df['COB_DT'], format='%d/%m/%Y')
df['Duration']= (df['BATCH_END_TIME']-df['LAST_FILE_RECEIVED']).astype('timedelta64[s]')/60
df['Weekday']=df['COB_DT'].dt.day_name()
app = dash.Dash(external_stylesheets=[dbc.themes.BOOTSTRAP])
#app.config['suppress_callback_exceptions']=True
controls = dbc.Card(
[
html.Div(
[
dbc.Label("Product"),
dcc.Dropdown(id='product_option',
options=[{'label': x, 'value': x} for x in df.sort_values('PRODUCT')['PRODUCT'].unique()],
value='Product1',
multi=False,
disabled=False,
clearable=True,
searchable=True,
placeholder='Choose Product...',
className='form-dropdown',
style={'width': "100%"},
persistence='string',
persistence_type='memory'),
]
),
html.Div(
[
dbc.Label("Weekday"),
dcc.Dropdown(id='weekday_option',
options=[{'label': x, 'value': x} for x in
df.sort_values('Weekday')['Weekday'].unique()] + [
{'label': 'All Weekdays', 'value': 'all_values'}],
value='all_values',
multi=False,
clearable=False,
style={'width': "100%"},
persistence='string',
persistence_type='session'),
]
),
],
body=True,
style={"width": "25rem"},
)
app.layout = dbc.Container(
[
html.H1("Batch Process"),
html.Hr(),
dbc.Row(
[
dbc.Col(controls, width=3),
],
align="start",
),
dbc.Row(
[
dbc.Col(dcc.Graph(id='trend_graph'),
width="auto",
style={"height": "60%", "width": "80%"}),
],
align="center",
),
dbc.Row(
[
dbc.Col(dcc.Graph(id='duration_graph'),
width="auto",
style={"height": "60%", "width": "80%"}),
],
align="center",
),
html.Br(),
html.Div(id='my-output'),
],
#fluid=True,
)
#---------------------------------------------------------------
def build_graph(dff,product, weekday):
trendfig = px.line(dff,x='COB_DT', y=["USER_EXTRACT_END_TIME",
"BATCH_END_TIME",
"LAST_FILE_RECEIVED"]
)
trendfig.update_layout(title={
'text': "Batch Process Trend for Product "+product,
'y':0.9,
'x':0.5,
'xanchor': 'center',
'yanchor': 'top'},
legend_title="Batch Status")
durfig = px.line(dff, x='COB_DT', y=["Duration"])
durfig.update_layout(title={
'text': "Processing Time in Minutes for Product "+product,
'y': 0.9,
'x': 0.5,
'xanchor': 'center',
'yanchor': 'top'})
trendfig.update_xaxes(
title_text="COB Date",
tickformat="%b\n%d")
trendfig.update_yaxes(
title_text="Time",
tickformat="%H:%M")
durfig.update_xaxes(
title_text="COB Date",
tickformat="%b\n%d")
durfig.update_yaxes(
title_text="Minutes",
tickformat="%H:%M")
return [trendfig,durfig]
@app.callback(
[Output('trend_graph','figure'),
Output('duration_graph', 'figure')],
[Input('product_option','value'),
Input('weekday_option','value'),
Input('trend_graph', 'selectedData')]
)
def build_graph_callback(product, weekday,selection1):
print(product)
print(weekday)
print(selection1)
if weekday == 'all_values':
dff = df[(df['PRODUCT'] == product)]
else:
dff=df[(df['PRODUCT']==product)&
(df['Weekday']==weekday)]
#print(dff[["LAST_FILE_RECEIVED","Duration"]])
trendfig, durfig = build_graph(dff,product,weekday)
return [trendfig, durfig]
#---------------------------------------------------------------
if __name__ == '__main__':
app.run_server(debug=True)
Data:
"COB_DT","PRODUCT","FIRST_FILE_RECEIVED_HHMI","LAST_FILE_RECEIVED_HHMI","BATCH_END_TIME","USER_EXTRACT_END_TIME"
"01/12/2021","Product1","02:03:06","03:23:12","03:35:44","16:34:38"
"01/12/2021","Product2","06:04:08","15:41:57","16:12:11","16:34:38"
"01/12/2021","Product3","06:35:48","06:35:48","07:04:48","16:34:38"
"01/12/2021","Product4","12:00:49","15:54:02","16:16:01","16:34:38"
"01/12/2021","Product5","06:45:27","13:26:12","16:10:30","16:34:38"
"02/12/2021","Product1","01:49:04","02:07:06","02:38:38","18:00:13"
"02/12/2021","Product2","09:17:09","16:42:53","17:14:00","18:00:13"
"02/12/2021","Product3","06:39:54","06:39:54","06:55:05","18:00:13"
"02/12/2021","Product4","09:17:09","17:05:04","17:16:57","18:00:13"
"02/12/2021","Product5","06:44:09","15:06:03","17:45:05","18:00:13"
"03/12/2021","Product1","03:12:49","03:47:52","04:21:55","10:56:22"
"03/12/2021","Product2","02:27:20","06:57:48","08:03:43","10:56:22"
"03/12/2021","Product3","06:35:49","06:35:49","06:53:21","10:56:22"
"03/12/2021","Product4","02:27:20","10:25:12","10:41:41","10:56:22"
"03/12/2021","Product5","02:28:44","06:42:00","09:06:21","10:56:22"
"06/12/2021","Product1","01:40:47","02:20:50","02:50:43","18:20:16"
"06/12/2021","Product2","05:59:46","16:58:57","17:25:57","18:20:16"
"06/12/2021","Product3","10:22:37","10:22:37","10:42:12","18:20:16"
"06/12/2021","Product4","08:07:44","17:19:22","17:33:21","18:20:16"
"06/12/2021","Product5","06:43:11","14:51:31","18:05:40","18:20:16"
"07/12/2021","Product1","04:44:35","05:14:45","05:45:59","18:29:18"
"07/12/2021","Product2","01:54:40","17:42:03","18:13:40","18:29:18"
"07/12/2021","Product3","07:58:42","07:58:42","08:20:31","18:29:18"
"07/12/2021","Product4","01:54:40","17:57:20","18:08:22","18:29:18"
"07/12/2021","Product5","07:57:38","15:33:05","17:51:45","18:29:18"