
Hi,
I’m new to Bokeh and just trying things out. I’ve tried to create a simple web app which generates a sine wave with a random frequency from a button press. I’ve created a class which inherits from VBox and stores a button and plot in the children. When I run the app with ‘bokeh serve --show MyApp.py’ at the command prompt, the server starts and I get no errors, but the browser window is empty. Don’t know what I’m doing wrong but any help gratefully received!
Thanks,
Marcus.
Code…
import numpy
from bokeh.models import Button, Plot, ColumnDataSource
from bokeh.plotting import Figure, curdoc
from bokeh.models.widgets.layouts import VBox
from bokeh.core.properties import Instance,Float
from bokeh.models.sources import DataSource
class MyApp(VBox):
button = Instance(Button)
plot = Instance(Plot)
ds = Instance(DataSource)
f = Float
def __init__(self):
super(MyApp,self).__init__()
self.button = Button(label="Randomise Frequency")
self.button.on_click(self.callback)
self.f = 1.0
self.plot = Figure(x_range=[0,2*numpy.pi],y_range=[-1, 1])
self.ds = ColumnDataSource(data={'x':numpy.linspace(0,2*numpy.pi,100),'y':numpy.zeros(100)})
self.plot.line('x','y',source=self.ds,line_width=2,line_alpha=0.5)
self.children.append(self.button)
self.children.append(self.plot)
def callback(self):
self.f = numpy.random.rand()*10
self.ds.data['y'] = 2*numpy.pi*self.f*self.ds.data['x']
curdoc().add_root(MyApp())
