Hi! Could you please share your thoughts on this solution to the duplicate callback outputs problem?
I see that I could just draw all curves in a single plot and then activate/deactivate them but that’s not the point. The point is to draw different and possibly completely unrelated plots to the same dcc.Graph
depending on what the user wants to see. Actually - and maybe this is an important difference - the point is to draw the plots to the exact same location on the screen. Like switching TV channels: the location is the same, the content different. But it is fine if under the hood, not the channel is changed but, say, multiple devices get swapped out.
Also, what if the plots are heavy and it is likely that the user wants to jump back and forth between them? Would it make more sense to somehow just hide the dcc.Graph
components?
import numpy as np
import plotly.express as px
from dash import Dash, Input, Output, callback_context, dcc, html
from dash.exceptions import PreventUpdate
N = 5
app = Dash(__name__)
app.layout = html.Div([
html.Div(
[html.Div(
html.Button(
f'x^{i}',
id=f'btn-{i}'
)
) for i in range(N)]
),
dcc.Graph(id='graph')
]
)
@app.callback(
Output('graph', 'figure'),
[Input(f'btn-{i}', 'n_clicks') for i in range(N)]
)
def update_graph(*_):
ctx = callback_context
if not ctx.triggered:
raise PreventUpdate
else:
n = ctx.triggered[0]['prop_id'].split('.')[0].split('-')[1]
x = np.linspace(0, 8, 81)
y = x ** int(n)
fig = px.scatter(x=x, y=y)
return fig
if __name__ == '__main__':
app.run_server(debug=True)