In [ ]:
#''' Present an interactive function explorer with slider widgets.
#Scrub the sliders to change the properties of the ``sin`` curve, or
#type into the title text box to update the title of the plot.
#Use the ``bokeh serve`` command to run the example by executing:
# bokeh serve sliders.py at your command prompt. Then navigate to the URL
# http://localhost:5006/sliders
#in your browser.
#'''
import numpy as np
from bokeh.io import curdoc
from bokeh.layouts import row, widgetbox
from bokeh.models import ColumnDataSource
from bokeh.models.widgets import Slider, TextInput
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)
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)
# 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)
for w in [offset, amplitude, phase, freq]:
w.on_change('value', update_data)
# Set up layouts and add to document
inputs = widgetbox(text, offset, amplitude, phase, freq)
curdoc().add_root(row(inputs, plot, width=800))
curdoc().title = "Sliders"
In [ ]:
def my_text_input_handler(attr, old, new):
print("Previous label: " + old)
print("Updated label: " + new)
text_input = TextInput(value="default", title="Label:")
text_input.on_change("value", my_text_input_handler)
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [1]:
from __future__ import absolute_import
from ...core.properties import abstract
from ...core.properties import Bool, Int, Float, String, Date, RelativeDelta, Enum, List, Tuple, Either, Instance
from ..callbacks import Callback
from .widget import Widget
from ...core.enums import SliderCallbackPolicy
@abstract
class InputWidget(Widget):
""" Abstract base class for input widgets. `InputWidget`` is not
generally useful to instantiate on its own.
"""
title = String(default="", help="""
Widget's label.
""")
@classmethod
def coerce_value(cls, val):
prop_obj = cls.lookup('value')
if isinstance(prop_obj, Float):
return float(val)
elif isinstance(prop_obj, Int):
return int(val)
elif isinstance(prop_obj, String):
return str(val)
else:
return val
class TextInput(InputWidget):
""" Single-line input widget. """
value = String(default="", help="""
Initial or entered text value.
""")
callback = Instance(Callback, help="""
A callback to run in the browser whenever the user unfocuses the TextInput
widget by hitting Enter or clicking outside of the text box area.
""")
placeholder = String(default="", help="""
Placeholder for empty input field
""")
class AutocompleteInput(TextInput):
""" Single-line input widget with auto-completion. """
completions = List(String, help="""
A list of completion strings. This will be used to guide the
user upon typing the beginning of a desired value.
""")
class Select(InputWidget):
""" Single-select widget.
"""
options = List(Either(String, Tuple(String, String)), help="""
Available selection options. Options may be provided either as a list of
possible string values, or as a list of tuples, each of the form
``(value, label)``. In the latter case, the visible widget text for each
value will be corresponding given label.
""")
value = String(default="", help="""
Initial or selected value.
""")
callback = Instance(Callback, help="""
A callback to run in the browser whenever the current Select dropdown
value changes.
""")
class MultiSelect(InputWidget):
""" Multi-select widget.
"""
options = List(Either(String, Tuple(String, String)), help="""
Available selection options. Options may be provided either as a list of
possible string values, or as a list of tuples, each of the form
``(value, label)``. In the latter case, the visible widget text for each
value will be corresponding given label.
""")
value = List(String, help="""
Initial or selected values.
""")
callback = Instance(Callback, help="""
A callback to run in the browser whenever the current selection value
changes.
""")
size = Int(default=4, help="""
The number of visible options in the dropdown list. (This uses the
``select`` HTML element's ``size`` attribute. Some browsers might not
show less than 3 options.)
""")
class Slider(InputWidget):
""" Slider-based number selection widget.
"""
value = Float(default=0.5, help="""
Initial or selected value.
""")
start = Float(default=0, help="""
The minimum allowable value.
""")
end = Float(default=1, help="""
The maximum allowable value.
""")
step = Float(default=0.1, help="""
The step between consecutive values.
""")
orientation = Enum("horizontal", "vertical", help="""
Orient the slider either horizontally (default) or vertically.
""")
callback = Instance(Callback, help="""
A callback to run in the browser whenever the current Slider value changes.
""")
callback_throttle = Float(default=200, help="""
Number of microseconds to pause between callback calls as the slider is moved.
""")
callback_policy = Enum(SliderCallbackPolicy, default="throttle", help="""
When the callback is initiated. This parameter can take on only one of three options:
"continuous": the callback will be executed immediately for each movement of the slider
"throttle": the callback will be executed at most every ``callback_throttle`` milliseconds.
"mouseup": the callback will be executed only once when the slider is released.
The `mouseup` policy is intended for scenarios in which the callback is expensive in time.
""")
class RangeSlider(InputWidget):
""" Range-slider based range selection widget
"""
range = Tuple(Float, Float, default=(0.1, 0.9), help="""
Initial or selected range.
""")
start = Float(default=0, help="""
The minimum allowable value.
""")
end = Float(default=1, help="""
The maximum allowable value.
""")
step = Float(default=0.1, help="""
The step between consecutive values.
""")
orientation = Enum("horizontal", "vertical", help="""
Orient the slider either horizontally (default) or vertically.
""")
callback = Instance(Callback, help="""
A callback to run in the browser whenever the current Slider value changes.
""")
callback_throttle = Float(default=200, help="""
Number of microseconds to pause between callback calls as the slider is moved.
""")
callback_policy = Enum(SliderCallbackPolicy, default="throttle", help="""
When the callback is initiated. This parameter can take on only one of three options:
"continuous": the callback will be executed immediately for each movement of the slider
"throttle": the callback will be executed at most every ``callback_throttle`` milliseconds.
"mouseup": the callback will be executed only once when the slider is released.
The `mouseup` policy is intended for scenarios in which the callback is expensive in time.
""")
class DateRangeSlider(InputWidget):
""" Slider-based date range selection widget.
"""
value = Tuple(Date, Date, help="""
The initial or selected date range.
""")
bounds = Tuple(Date, Date, help="""
The earliest and latest allowable dates.
""")
range = Tuple(RelativeDelta, RelativeDelta, help="""
[TDB]
""")
step = RelativeDelta(help="""
The step between consecutive dates.
""")
# formatter = Either(String, Function(Date))
# scales = DateRangeSliderScales ... # first, next, stop, label, format
enabled = Bool(True, help="""
Enable or disable this widget.
""")
arrows = Bool(True, help="""
Whether to show clickable arrows on both ends of the slider.
""")
value_labels = Enum("show", "hide", "change", help="""
Show or hide value labels on both sides of the slider.
""")
wheel_mode = Enum("scroll", "zoom", default=None, help="""
Whether mouse zoom should scroll or zoom selected range (or
do nothing).
""")
callback = Instance(Callback, help="""
A callback to run in the browser whenever either slider's value changes.
""")
class DatePicker(InputWidget):
""" Calendar-based date picker widget.
"""
value = Date(help="""
The initial or picked date.
""")
min_date = Date(default=None, help="""
Optional earliest allowable date.
""")
max_date = Date(default=None, help="""
Optional latest allowable date.
""")
callback = Instance(Callback, help="""
A callback to run in the browser whenever the current date value changes.
""")
In [ ]:
In [ ]:
In [ ]: