Hi,
Getting error message.
Error message:
Duplicate callback outputs
In the callback for output(s):
mer_test.data
Output 0 (mer_test.data) is already in use. To resolve this, set `allow_duplicate=True` on duplicate outputs, or combine the outputs into one callback function, distinguishing the trigger by using `dash.callback_context` if necessary.
I had multiple input for one output and this pattern for multiple pages.
All the table id is unique with the same children.
However, keep on getting the same error message even tried to change the id name.
All the dropdown value is stored in the dcc.store.
CODE
dropdown.py
from .query import get_date
df = get_date()
year_cat = list(df['year'].unique())
corp_cat=[
{'label': 'Corp A', 'value': 'corp1'},
{'label': 'Corp B', 'value': 'corp2'},
]
top_cat = [
{'label': '15', 'value': 15},
{'label': '50', 'value': 50},
{'label': '100', 'value': 100},
]
dropdown = dbc.Container ([
dbc.Row([
dbc.Col([
html.H6('Year:'),
dcc.Dropdown(id='year_dd',
value= df['year'].max(),
options = [{'label':x, 'value':x} for x in year_cat],
searchable = True,
search_value='',
placeholder= 'Please select ...',
),
], width=3, md=3 ),
dbc.Col([
html.H6('Month:'),
dcc.Dropdown(id='month_dd',
value= '',
searchable = True,
search_value='',
placeholder= 'Please select ...',
className = 'mchanly-dropdown-list'
),
html.Br(),
], width=3, md=3),
dbc.Col([
html.H6('Date:'),
dcc.Dropdown(id='date_dd',
value='',
searchable = True,
search_value='',
placeholder= 'Please select ...',
),
html.Br(),
], width=3, md=3),
dbc.Col([
html.H6('Corporation:'),
dcc.Dropdown(id='meranly_corp_dd',
options=corp_cat,
value='tpa',
searchable = True,
search_value='',
placeholder= 'Please select ...',
),
], width=3, md=3),
]),
])
top = dbc.Container ([
dbc.Row([
dbc.Col([
html.P('Top:'),
dcc.Dropdown(id='top_dd',
options=top_cat,
value= 15,
searchable = True,
search_value='',
placeholder= 'Please select ...',
),
]),
]),
])
@callback(
Output('month_dd','options'),
Input('year_dd', 'value')
)
def update_dd (year_dd):
year_month= df.drop_duplicates(['mthtyr'], inplace= False)
relevant_month= year_month[ df['year'] == year_dd]['monthname'].values.tolist()
month_option= [dict(label=x,value=x)for x in relevant_month]
return month_option
@callback(
Output('month_dd','value'),
Input('month_dd', 'options')
)
def default_value(latest_month):
month_value = latest_month[-1]['value']
return month_value
# #date dropdown based on filtered month
@callback(
Output('date_dd','options'),
Input('month_dd', 'value'),
State('year_dd', 'value')
)
def update_dd (month_dd, year_dd):
month_date= df.drop_duplicates(['Date'], inplace= False)
relevant_date= month_date[ (df['monthname'] == month_dd) & (df['year'] == year_dd)]['Date'].values.tolist()
date_option= [dict(label=x,value=x)for x in relevant_date]
return date_option
@callback(
Output('date_dd','value'),
Input('date_dd', 'options')
)
def default_value(latest_date):
date_value = latest_date[-1]['value']
return date_value
@callback(
Output('storedate','data'),
Input('date_dd', 'value'),
State('year_dd', 'value'),
State('month_dd', 'value'),
)
def stored_selected_value (date, year, month):
return {'date': date, 'year': year, 'month': month}
@callback(
Output('storecorp','data'),
Input('corp_dd', 'value'),
)
def stored_selected_value (corp):
return {'corp': corp}
@callback(
Output('storetop','data'),
Input('top_dd', 'value'),
)
def stored_selected_value (top):
return {'top': top}
`store.py`
import dash_core_components as dcc
# Define the dcc.Store component
store = dcc.Store(id='storedate', storage_type='session')
corp_store = dcc.Store(id='storecorp', storage_type='session')
top_store = dcc.Store(id='storetop', storage_type='session')
analysis.py
from .pages.dropdown import dropdown, top
from .pages.nav import nav
from .pages.store import store,corp_store , top_store
from .pages.bestperform import best_perform
dash.register_page(__name__,
path='/testting', # '/' is home page and it represents the url
name=' Analysis', # name of page, commonly used as name of link
title='analysis', # title that appears on browser's tab
icon="bi bi-clipboard-data",
)
require_login(__name__,
access_level= package2
)
def layout():
if not current_user.is_authenticated:
return html.Div(["Please ", dcc.Link("login", href="/login"), " to continue"])
return dbc.Container ([
mcht_store,
mcht_corp_store,
mcht_top_store,
dbc.Row([
dbc.Col ([
html.H2 ('Analysis')
],width=12)
], className = ''),
dbc.Row(dropdown, className = ''),
dbc.Row(nav, className = ''),
html.Hr(className = 'r'),
dbc.Row(test1, className = ''),
])
best_perform.py
from .dropdown import top
from .query import get_best_perform
from .view import (
comm_table,
comm_header,
comm_cell,
cell_con1,
comm_data_con,
data_con1_green,
data_con2_red,
data_con3_orange,
data_con4_grey,
)
best_perform = dbc.Container([
dbc.Row([
dbc.Col([
html.H5("BEST PERFORMANCE"),
dbc.Row (top),
dash_table.DataTable(id='mer_test',
columns=[
{'name': 'ID', "id": 'id'},
{'name': 'Registration Name', "id": 'registration_name'},
{'name': 'Rank3', "id": 'rank3'},
{'name': 'Last 3-mth', "id": 'lmtd3'},
{'name': 'Rank2', "id": 'rank2'},
{'name': 'Last 2-mth', "id": 'lmtd2'},
{'name': 'Rank1', "id": 'rank1'},
{'name': 'Last 1-mth', "id": 'lmtd'},
{'name': 'This mth', "id": 'tmtd'},
{'name': 'Diff(%)', "id": 'variance'},
],
export_format='csv',
export_headers='display',
page_size= 50,
style_table = comm_table(),
style_header= comm_header(),
style_cell=comm_cell(),
style_cell_conditional = cell_con1(),
style_data_conditional= comm_data_con() + data_con1_green() +
data_con2_red() + data_con3_orange()+ data_con4_grey(),
),
], className = '')
], className = ''),
html.Br(),html.Br(),html.Br()
])
@callback(
[Output('mer_test', 'data')],
[
Input('storedate','data'),
Input('storecorp','data'),
Input('storetop', 'data')
],
)
def update_table1(selection, corp_selection, top_filter):
if not selection or not corp_selection or not top_filter:
return dash.no_update
else:
selection = pd.to_datetime(selection['date'], format = '%Y-%m-%d')
corp_selection = corp_selection['corp']
top_filter = top_filter['top']
dff = get_best_perform(selection, corp_selection)
dff= dff.head(top_filter)
dt = dff
dt['tmtd']=dt['tmtd'].fillna(0)
dt['lmtd']=dt['lmtd'].fillna(0)
dt['lmtd2']=dt['lmtd2'].fillna(0)
dt['lmtd3']=dt['lmtd3'].fillna(0)
dt['variance'] = ((dt['tmtd'] - dt['lmtd']) / dt['lmtd']) * 100
dt['rank2'] = dt['rank2'].fillna('-')
dt['rank3'] = dt['rank3'].fillna('-')
dt['variance'] = dt['variance'].apply(lambda x: float(x))
dt['variance'] = dt['variance'].round()
dt['tmtd']=dt['tmtd'].map('{:,.2f}'.format)
dt['lmtd']=dt['lmtd'].map('{:,.2f}'.format)
dt['lmtd2']=dt['lmtd2'].map('{:,.2f}'.format)
dt['lmtd3']=dt['lmtd3'].map('{:,.2f}'.format)
dt['variance']=dt['variance'].map('{:.0f}%'.format)
dt['variance']=dt['variance'].replace('inf%', '-')
columns = dt[['id', 'registration_name',
'rank3',
'lmtd3',
'rank2',
'lmtd2',
'rank1',
'lmtd',
'tmtd' ,
'variance'
]]
data= columns.to_dict('records')
return data