I am new to Bokeh and “web apps”. I am trying to create the following workflow:
WHAT I AM TRYING TO DO:
-
In python create 2 Bokeh plots and a button. Show it in chrome browser.
-
Look at plots in browser and click the “Done” button to get back to python
-
Same as 1.
-
Same as 2.
When I do this (1) and (2) work i.e. plots are shown and clicking DONE shuts down server and gets back to python script (test2() below). When I show the page again (please see code below) I get the following 2 plots + 1 button (inactive) + 2 plots + 1 button (active).
I would like to see just the 2 plots + 1 button (active) when I do 3) and 4).
QUESTIONS:
-
What am I doing wrong to see replicated content?
-
If I have a lot more plots, with around 20 curves per plot, to display on one page is my code/design below a reasonable choice?
Thanks for any help/insight.
Thanks
Prabu
import os
import os.path
import numpy as np
import subprocess
import time
import signal
from bokeh.client import push_session
from bokeh.models import ColumnDataSource
from bokeh.models.widgets import RadioButtonGroup
from bokeh.models.widgets import Button
from bokeh.plotting import curdoc
from bokeh.plotting import figure
from bokeh.layouts import layout
from bokeh.palettes import all_palettes
from bokeh.models import Legend
from bokeh.io import output_file
from bokeh.models import CheckboxGroup
class TestPage(object):
def init(self):
self._type = type
self._x = np.linspace(0, 10, 1000)
self._sinx = np.sin(self._x)
self._cosx = np.cos(self._x)
self._plots =
def show(self):
self._start_server()
self._create_doc()
def _create_doc(self):
Create oa bunch of copies of the same graphs
for i in range(2):
self._plots.append(figure(plot_width=1000,
plot_height=400,
toolbar_location=‘above’,
x_axis_label=‘Wavelengths (nm)’,
y_axis_label=‘Measurand’,
x_range=(0, 11),
y_range=(-3, 3)))
self._plots[i].line(self._x,
self._sinx,
color=‘red’,
line_width=2,
line_alpha=0.7)
self._plots[i].line(self._x,
self._cosx + 0.5,
color=‘green’,
line_width=2,
line_alpha=0.7)
A button to shut down and return to python
self._done_btn = Button(label=‘Done’)
self._done_btn.on_click(self._done_cb)
The layout
self._layout = layout([[p] for p in self._plots] + [[self._done_btn]])
self._session = push_session(curdoc())
self._session.show(self._layout, browser=‘google-chrome’)
self._session.loop_until_closed()
def _start_server(self):
Start the Bokeh server
args = [‘bokeh’, ‘serve’]
self._process = subprocess.Popen(args, shell=False)
time.sleep(1) # time to wait might be longer on other PC
print(‘Server pid: {}’.format(self._process.pid))
def _done_cb(self):
os.kill(self._process.pid, signal.SIGKILL)
self._process.terminate()
def test1():
TestPage().show()
def test2():
page1.show()
TestPage().show()
s = 0
for i in range(10):
s += i
TestPage().show()
page2.show()
if name == ‘main’:
test1()
test2()