I am trying to create a dash app, where on clicking different buttons different graphs should display.
But when the graph is initially loaded it breaks and then automatically adjusts itself.
Why the graph breaks when loaded initially, I have given a sample code.
button_style = {'background-color': 'transparent', 'color': 'inherit', 'border': 'none', 'border-radius': '0px'}
style_after_click = {'background-color': 'transparent', 'color': '#0000FF', 'border-style': 'solid',
'border-color': '#0000FF', 'border-width': '0px 0px 2px 0px', 'border-radius': '0px'}
app.layout = html.Div([
dbc.Card([
dbc.CardBody([
html.Div([
dbc.Button('Option1', id='option1_button', n_clicks=0, style=style_after_click,
className='margin_buttons_1', size="lg"),
dbc.Button('Option2', id='option2_button', n_clicks=0, style=button_style, className='margin_buttons', size="lg"),
dbc.Button('Option3', id='option3_button', n_clicks=0, style=button_style, className='margin_buttons', size="lg"),
], style={'display': 'flex'}),
html.Br(),
html.Div([
dcc.Graph(figure =fig_sample(), config={'displayModeBar': False}, style = {'height':'200px'})
], id="option1_graph"),
html.Div([
dcc.Graph(figure =fig_sample(), config={'displayModeBar': False}, style = {'height':'200px'} )
], id="option2_graph", style={"display": "none"}),
html.Div([
dcc.Graph(figure =fig_sample(), config={'displayModeBar': False}, style = {'height':'200px'})
], id="option3_graph", style={"display": "none"}),
], style={'padding': '20px'})
], style={'height': '348px'})
])
@callback(
Output(component_id="option1_graph", component_property='style'),
Output(component_id="option2_graph", component_property='style'),
Output(component_id="option3_graph", component_property='style'),
Output(component_id='option1_button', component_property='style'),
Output(component_id='option2_button', component_property='style'),
Output(component_id='option3_button', component_property='style'),
[
Input(component_id='option1_button', component_property='n_clicks'),
Input(component_id='option2_button', component_property='n_clicks'),
Input(component_id='option3_button', component_property='n_clicks'),
], prevent_initial_call=True)
def clicked_p2p(option1_button, option2_button, option3_button):
if 'option1_button' == ctx.triggered_id:
return {'display': 'block'}, {'display': 'none'}, {'display': 'none'}, style_after_click, button_style, button_style
elif 'option2_button' == ctx.triggered_id:
return {'display': 'none'}, {'display': 'block'}, {'display': 'none'}, button_style, style_after_click, button_style
elif 'option3_button' == ctx.triggered_id:
return {'display': 'none'}, {'display': 'none'}, {'display': 'block'}, button_style, button_style, style_after_click
else:
return {'display': 'block'}, {'display': 'none'}, {'display': 'none'}, style_after_click, button_style, button_style
The graph in the image below was intended to occupy the entire width of the screen. However, during rendering, the graph initially appears at only half of its intended width and then readjusts to occupy the full width
l