I am new to plotly-dash.
I want to refresh the existing graph with new value through button click. this is the code i have tried. when i click the update button. its creating a new graph and plotting the new values instead of plotting the new values in the existing graph.
import dash
import plotly
from plotly.graph_objs import Scatter, Layout
import plotly.graph_objs as go
import plotly.graph_objects as go
from dash.dependencies import Input, Output
import dash_html_components as html
import dash_core_components as dcc
xx = [10,20,30,40,50,60,70]
yy = [10,20,30,50,60,70,80]
app = dash.Dash(__name__)
app.layout = html.Div([
html.Button(
id='button',
children='Update',
n_clicks=0
),
#dcc.Graph(id='graph'),
dcc.Graph(
id='graph',
figure={
'data': [
go.Scatter(
x=xx,
y=yy,
#text='name',
mode='markers',
marker=dict(size=15,color='orange'),
opacity=0.7,
#line=dict(width=2, color="red"),
#name=i
), #for i in df.continent.unique()
],'layout': {
'title': 'graph-1'
}
})
])
x = [1,2,3,4,5,6,7]
y = [10,20,30,50,6,7,8]
@app.callback(Output('graph', 'figure'), [Input('button', 'n_clicks')])
def update_graph(n_clicks):
return {
'data': [{
'x': [x[n_clicks]],
'y': [y[n_clicks]],
}],
'layout': {
'title': 'graph-2'
}
}
if __name__ == '__main__':
app.run_server(debug=True)
when i click the update button, its creating the new graph instead of updating the values in the existing graph.
screenshot