I am trying to create a graph based on data from a MySQL database. I have print statements in the code that show the dataframe is there and has some data.
import dash
from dash import html, dcc, Input, Output, callback, ctx,
import pandas as pd
import sqlalchemy as sq
import plotly.express as px
import plotly.graph_objects as go
from .utils import getActionCounts,ThroughPut, Graph_Throughput
import dash_bootstrap_components as dbc
import json
from datetime import datetime
import dash_mantine_components as dmc
connection = sq.create_engine(f'mysql://{username}:{password}@{hostname}/{table_name}')
minmax_query = '''
SELECT
"from",
"to",
MIN(START_DATETIME) AS MinDate,
MAX(START_DATETIME) AS MaxDate
FROM
schema.TableName
GROUP BY
"from",
"to";
'''
# Data loading
dates = pd.read_sql(minmax_query,con=connection)
unit_type = ['Unit1', 'Unit2', 'Unit3','*']
dates['MinDate']=pd.to_datetime(dates['MinDate'])
dates['MaxDate']=pd.to_datetime(dates['MaxDate'])
layout = dbc.Container([
dbc.Row([
dbc.Col([
dcc.RadioItems(
options=['Date','TotalTestsRun','Tests Passed','Yield'],
value = 'Yield',
id='tr_radio_btn',
)
],width=2),
dbc.Col([
dmc.DateRangePicker(
id='FPY-DatePicker',
minDate=min(dates["MinDate"]),
maxDate=max(dates["MaxDate"]),
value=[min(dates["MinDate"]),max(dates["MaxDate"])],
clearable=False,
#display_format='YYYY-MM-DD'
) ]),
dbc.Col([
dcc.RadioItems(
options=unit_type,
value = '*',
id='Unit-Type-radio',
),
html.H5('Select Graph Type: '),
dcc.Dropdown(
id='FPY-Graph-Type',
options=[{'label': 'Trend', 'value': 'Trend'},
{'label': 'Bar', 'value': 'Bar'}],
value='Trend',
clearable=False,
),
html.Button('Update Graph',id='update-button', n_clicks=0),
])
]),
dbc.Row(
dcc.Graph(id='FPY-df',)
) ])
@callback(
Output(component_id='FPY-df', component_property='figure'),
[Input('FPY-DatePicker', 'value'),
Input('Unit-Type-radio','value'),
Input('tr_radio_btn','value'),
Input('FPY-Graph-Type','value'),
Input('update-button','n_clicks')]
)
def update_text(date,Unit_type,test_data,graph_type, n_clicks):
if 'update-button'==ctx.triggered_id:
start_date,end_date = date
fig = go.Figure()
df_sys=pd.read_sql('SELECT * FROM schema.TABLE WHERE START_DATETIME >= %s and START_DATETIME<=%s',con=connection,params=(start_date,end_date))
dict_df_filter = ThroughPut(df_sys,start_date=start_date,end_date=end_date,UnitType=Unit_type)
print(type(dict_df_filter['bench']))
fig = Graph_Throughput(df=dict_df_filter['bench'],test_data=test_data,graph_type=graph_type,Unit=Unit_type,fig=fig)
print(type(fig))
return fig
else:
return None
and the Graph_Throughput function is:
def Graph_Throughput(df:pd.DataFrame,test_data:str,Unit_type:str, graph_type:str ,fig:go.Figure):
print(df.head())
print(test_data)
print(Unit_type)
print(graph_type)
if graph_type=='Trend':
if Unit_type=='*':
fig.add_traces(go.Scatter(x=df['Date'], y=df[test_data], mode='lines', name='All Units'))
else:
fig.add_traces(go.Scatter(x=df['Date'], y=df[test_data], mode='lines', name=str(Unit_type+' '+test_data)))
elif graph_type=='Bar':
if Unit_type=='*':
fig.add_traces(go.Bar(x=df['Date'], y=df[test_data], mode='stacked', name=str('All Units'+test_data)))
else:
fig.add_traces(go.Bar(x=df['Date'], y=df[test_data], mode='stacked', name=str(Unit_type+' '+test_data)))
return fig
When I try it out in a jupyter notebook ignoring the dash components everything seems to work fine. And if I check the callback graph, then it confirms there is no data shown for the graph.