I’m struggling to understand how precisely the Bokeh server is supposed to work. Admittedly, my use case may be a little unique. I’m currently trying to find a back to embed plots in PyQt windows. That is, to generate separate plots and display them as a web view within a PyQt application.
I’ve attempted a couple ways of doing this. Mainly, by starting a Bokeh server when the PyQt application starts via the subprocess module (which listens for Qt close events in order to kill the server on exit). I then have a Line1D class that encapsulates a session so that I can have access to the generated plots in order to facilitate interactions between the user and the plot (e.g. propagating selection events to other, non-Bokeh data analysis functions). Here is a pared down version of the class:
process = subprocess.Popen(['bokeh', 'serve'])
class Line1D(EventDispatchMixin):
def __init__(self):
# Add a listener to the close application event so we can clean up the
# Bokeh server we opened
self.add_listener("on_application_close", self.close)
x = np.linspace(0, 4*np.pi, 80)
y = np.sin(x)
p = figure(sizing_mode="stretch_both")
r1 = p.line([0, 4*np.pi], [-1, 1], color="firebrick")
r2 = p.line(x, y, color="navy", line_width=4)
# open a session to keep our local document in sync with server
self.session = push_session(curdoc())
# self.html = autoload_server(None, session_id=self.session.id)
self.url = "{}?bokeh-session-id={}".format(
server_url_for_websocket_url(self.session._connection.url),
_encode_query_param(self.session.id))
print(self.url)
def close(self):
process.kill()
I would generate the url and store it within the class, however, the url gives nothing, i.e. the Bokeh output on the terminal looks reasonable, but the browser page is blank. The source does show that injection is correct, however. If I add a “self.session.show(p)” to the code above, I can get the plot to render both in the PyQt application window and, of course, the new browser window that’s opened up. But this is not ideal – I have no use for the browser window. How can I trigger whatever it is the “show” method does to render the plot, without having it auto-open a browser window?
I’m sure I’m doing something far from ideal in my view of how this should work. For one, fragments of mailing list posts and SO questions indicates that the recommended way to operate multiple rendered plots is to start a Bokeh server for each one. Is there a programmatic way to start and handle multiple Bokeh servers?
Any help/guidance would be greatly appreciated!
Thanks,
Nick