Hi there,
On my dash app I have 3 buttons and I want to set one of them to be the default active pressed button upon page load. So when I load up my app, this one button should look like it’s already been pressed.
Looking at other somewhat similar examples, I still haven’t been able to find the solution for my case. As I understand, if n_clicks=0 or n_clicks=None then it should fire on page load? I have tried both, setting n_clicks to 0 and None (the latter of which is shown below). I also tried using dbc.Button instead and using the active=True property as well, but so far with no results.
From my code, I want to set my “Overview” button to be the active pressed button when I first load up.
Would appreciate any help on this. Thank you.
Button code:
# Buttons
html.Div([
html.Button('Overview', id='button_overview', n_clicks=None),
html.Button('Details', id='button_details', n_clicks=1),
html.Button('Employee Data', id='button_employee_data', n_clicks=1),
html.Div(id='page')
]),
Callback code:
# Callback - buttons
@app.callback(Output('page', 'children'),
Input('button_overview', 'n_clicks'),
Input('button_details', 'n_clicks'),
Input('button_employee_data', 'n_clicks'))
def render_page(button_1, button_2, button_3):
changed_id = [p['prop_id'] for p in dash.callback_context.triggered][0]
if 'button_overview' in changed_id:
return html.Div([
dcc.Markdown('''Overview coming soon''')
])
elif 'button_details' in changed_id:
return html.Div([
html.Br(),
dcc.Markdown('''**Select filters**'''),
dcc.Markdown('''Category'''),
# Radio buttons for graph category
dcc.RadioItems(
id='graph_category',
options=[
{'label':'Department', 'value':'Department'},
{'label':'Position Level', 'value':'Position Level'},
],
value='Department',
labelStyle={'display':'inline-block'},
inputStyle={'margin-left':'30px'}
),
html.Br(),
# Markdown text
dcc.Markdown('''Departments'''),
# Checklist row
html.Div([
# Checklist - all departments
dcc.Checklist(
id='checklist_all_dept',
options=[{'label': 'All', 'value': 'All'}],
value=['All'],
labelStyle={'display': 'inline-block'},
inputStyle={'margin-left':'30px'},
),
], style={'display':'inline-block'}),
html.Div([
# Checklist - departments
dcc.Checklist(
id='checklist_dept',
options=options,
value=[],
labelStyle={'display': 'inline-block'},
inputStyle={'margin-left':'30px'},
),
], style={'display':'inline-block'}),
dbc.Card([
html.Div([
dcc.Graph(id='graph1'),
dcc.Graph(id='graph2')
], style={'columnCount':2}),
], color='primary'),
])
elif 'button_employee_data' in changed_id:
return html.Div([
dash_table.DataTable(
id='table',
columns=[{'id':c, 'name':c} for c in df.columns],
data=df.to_dict('records'),
page_size=20,
style_table={'height':'500px', 'overflowY':'auto'}
)
])
EDIT: Full callback code included