I am using version 3.7.3 of Bokeh.
I know that the “Dropping a patch” warnings are just warnings, but they show up in the terminal and I don’t want the customers of the script I wrote have to see those over and over again. Here is an MRE that shows what is happening. When I run this, and use the Pan tool, I get many of those warnings. For example, this warning.
WARNING:root:Dropping a patch because it contains a previously known reference (id=‘p1005’). Most of the time this is harmless and usually a result of updating a model on one side of a communications channel while it was being removed on the other end.
The line that causes the warnings is
self.plot_figure.x_range = Range1d(0, self._iteration)
What am I doing wrong?
Thanks in advance!
from bokeh.models import ColumnDataSource, Range1d
from bokeh.models.tools import PanTool
from bokeh.plotting import figure
from bokeh.server.server import Server
from bokeh.application.application import Application
from bokeh.application.handlers import FunctionHandler
_time_between_callbacks_in_ms = 200
class _RealTimeOptimizerPlot(object):
def __init__(self, callback_period, doc):
self.plot_figure = figure(
tools=[PanTool(),],
title="MRE for dropped patches when panning",
)
self._source_stream_dict = {
'iteration': [],
'y': [],
}
self._source = ColumnDataSource(self._source_stream_dict)
self._iteration = 0
self._y = 0
self.plot_figure.line(
x="iteration",
y="y",
source=self._source,
)
doc.add_periodic_callback(self._update, callback_period)
doc.title = "MRE for dropped patches when panning"
doc.add_root(self.plot_figure)
def _update(self):
self._source_stream_dict = {
"iteration": [self._iteration],
"y": [self._y],
}
self._source.stream(self._source_stream_dict)
# the following line causes the "Dropping a patch" warnings when panning
self.plot_figure.x_range = Range1d(0, self._iteration)
self._iteration += 1.
self._y += 0.25
def _make_realtime_plot_doc(doc):
_RealTimeOptimizerPlot(
_time_between_callbacks_in_ms,
doc=doc,
)
try:
server = Server(
{"/": Application(FunctionHandler(_make_realtime_plot_doc))},
port=5123,
)
server.start()
server.io_loop.add_callback(server.show, "/")
server.io_loop.start()
except KeyboardInterrupt as e:
print(
f"Real-time optimization plot server stopped due to keyboard interrupt: {e}"
)
except Exception as e:
print(f"Error starting real-time optimization plot server: {e}")
finally:
print("Stopping real-time optimization plot server")
if "server" in globals():
server.stop()