Hello all,
I am trying to create interactive notebook widgets with Plotly. Basically it plots a default graph when clicked, then when the user clicks the button, it updates the current graph with a new set of data.
I can plot 2 separate data, however I just cannot get it to update the same graph instead of creating a new one. I have tried various parameters such as figure.show() or figure.update() or display(f), they all plot a new graph instead of update the current one. I have attempted to update the existing object but failed to do so. I can see that someone has posted similar post in the past but received no response so I am posting the same question again.
Below is my main code with some unimportant part omitted. Any help is greatly appreciated. Thank you very much.
import plotly
import sqlite3
import ipywidgets as widgets
import plotly.graph_objects as py
from plotly.offline import iplot
plotly.offline.init_notebook_mode(connected=False)
xvalues = []
yvalues = []
data = []
button = widgets.Button(
description=‘Plot graph’,
disabled=False,
button_style=’’, # ‘success’, ‘info’, ‘warning’, ‘danger’ or ‘’
tooltip=‘Click me’,
icon=‘pencil’
)
output = widgets.Output()
display(button, output)
def on_button_clicked(b):
with output:
result = c.execute(“SELECT * FROM ‘LRP from Dマシン(5290)’ LIMIT 100 OFFSET 5000”)
loadData(result)
data = [
py.Bar(x=xvalues, y=yvalues, name=“Bar graph”),
py.Scatter(x=xvalues, y=yvalues, name=“Line graph”)
]
fig = py.Figure(data)
fig.show()
def plot(data):
data = [
py.Bar(x=xvalues, y=yvalues, name=“Bar graph”),
py.Scatter(x=xvalues, y=yvalues, name=“Line graph”)
]
layout = plotly.graph_objs.Layout(
title=“Test plot”,
legend={“x”:1, “y”:1},
xaxis={“title”:“NO”},
yaxis={“title”:“Node”},
yaxis2={“title”:“Birth Rate”, “overlaying”:“y”, “side”:“right”},
)
return data, layout
#connect to database to get data
result = c.execute(“SELECT * FROM ‘LRP from Dマシン(5290)’ LIMIT 100 OFFSET 2000”)
loadData(result)
the data
data, layout = plot(data)
fig = py.Figure(data, layout)
f2 = py.FigureWidget(fig)
f2.show()
#get new data upon clicking
button.on_click(on_button_clicked)