简体   繁体   中英

Bokeh: Update from multiple sliders with Button Click

Goal - I would like to slide the sliders around THEN when i'm ready to update press the button to update the values based upon where the current sliders are at.

Below is an example borrowed from Bokeh's website. Ideally, I would like to change the slider parameters, then when i'm ready for them all to update, click the button, have all the sliders update and display the changes. This process would be repeated over and over. I've tried the below but I'm not getting the desired result.

import numpy as np

from bokeh.io import curdoc,output_file, show
from bokeh.layouts import column, row
from bokeh.models import ColumnDataSource, Slider, TextInput, Button
from bokeh.plotting import figure

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


# Set up plot
plot = figure(plot_height=400, plot_width=400, title="my sine wave",
              tools="crosshair,pan,reset,save,wheel_zoom",
              x_range=[0, 4*np.pi], y_range=[-2.5, 2.5])

plot.line('x', 'y', source=source, line_width=3, line_alpha=0.6)


# Set up widgets
text = TextInput(title="title", value='my sine wave')
offset = Slider(title="offset", value=0.0, start=-5.0, end=5.0, step=0.1)
amplitude = Slider(title="amplitude", value=1.0, start=-5.0, end=5.0, step=0.1)
phase = Slider(title="phase", value=0.0, start=0.0, end=2*np.pi)
freq = Slider(title="frequency", value=1.0, start=0.1, end=5.1, step=0.1)
button = Button(label="Update Changes", button_type="success")


# Set up callbacks
def update_title(attrname, old, new):
    plot.title.text = text.value

text.on_change('value', update_title)

def update_data(attrname, old, new):

    # Get the current slider values
    a = amplitude.value
    b = offset.value
    w = phase.value
    k = freq.value

    # Generate the new curve
    x = np.linspace(0, 4*np.pi, N)
    y = a*np.sin(k*x + w) + b

    source.data = dict(x=x, y=y)

def update():
    for w in [offset, amplitude, phase, freq]:
        w.on_change('value', update_data)


button.on_click(update)

# Set up layouts and add to document
inputs = column(text, offset, amplitude, phase, freq, button)

curdoc().add_root(row(inputs, plot, width=800))

Delete the code that sets up the callbacks on slider change (because you don't want that) and call update_data from the button instead (after updating the callback function signature appropriately):

def update_data():

    # Get the current slider values
    a = amplitude.value
    b = offset.value
    w = phase.value
    k = freq.value

    # Generate the new curve
    x = np.linspace(0, 4*np.pi, N)
    y = a*np.sin(k*x + w) + b

    source.data = dict(x=x, y=y)

button.on_click(update_data)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM