2

Does anyone have an example of how to run python code in jupyter by clicking a button in Bokeh?

3

1 Answer 1

8

UPDATE The original answer was very out of date. The answer has been updated to reflect changes since Bokeh 0.11 which was released in January of 2016.

A complete example pared from the sliders demo, that uses features from Bokeh 0.12.4:

from numpy import linspace, pi, sin

from bokeh.io import curdoc
from bokeh.layouts import row, widgetbox
from bokeh.models import ColumnDataSource, Slider
from bokeh.plotting import figure

# Set up data
x = linspace(0, 4*pi, 200) 
y = sin(x)
source = ColumnDataSource(data=dict(x=x, y=y))

# Set up plot
plot = figure(x_range=(0, 4*pi), y_range=(-2.5, 2.5))
plot.line('x', 'y', source=source, line_width=3, line_alpha=0.6)

# Set up widgets
amplitude = Slider(title="amplitude", value=1.0, start=-5.0, end=5.0)
freq = Slider(title="frequency", value=1.0, start=0.1, end=5.1)

# Set up callbacks
def update(attrname, old, new):
    # Get the current slider values
    a = amplitude.value
    k = freq.value

    # Update the data for the new curve
    source.data = dict(x=x, y=a*sin(k*x))

amplitude.on_change('value', update)
freq.on_change('value', update)

# Set up layout and add to document
inputs = widgetbox(amplitude, freq)
curdoc().add_root(row(inputs, plot, width=1200))

Run with bokeh serve --show <filename> and get the following responsive web app in your browser:

Simplified Bokeh Sliders demo

Sign up to request clarification or add additional context in comments.

7 Comments

This was really helpful. I appreciate it.
Note that the new version of bokeh-server should make this easier/nicer, but I haven't played with it yet.
any change we can get an update? bokeh-server was removed after version 0.10.
Looks like thorough documentation here bokeh.pydata.org/en/latest/docs/user_guide/server.html
Updated to reflect current bokeh serve in use since January 2016 (Bokeh 0.11 and newer)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.