python - 通过单击 Bokeh 中的按钮运行 python 代码

标签 python button bokeh jupyter

有没有人有关于如何通过单击 Bokeh 中的按钮在 jupyter 中运行 python 代码的示例?

最佳答案

更新 原来的答案已经过时了。答案已更新以反射(reflect)自 Bokeh 0.11 以来的变化。于 2016 年 1 月发布。

从 slider 演示中删除的一个完整示例,它使用了 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))

bokeh serve --show <filename> 运行并在您的浏览器中获取以下响应式 Web 应用程序:

Simplified Bokeh Sliders demo

关于python - 通过单击 Bokeh 中的按钮运行 python 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32418045/

相关文章:

Python从下载链接获取s3文件

python - Word2Vec min_count 是如何应用的

python - Matplotlib matshow : show all tick labels

java - 单击按钮时在文本区域中显示类输出

python - Bokeh 图 : HoverTool tips

python - 在 Bokeh 中链接两个散点图

python - 如何在 Windows 机器上删除文本文件中的空行

android - Android 5 上的 AppCompatButton : app:backgroundTint works but supportBackgroundTintList does NOT :(

java - 处理填充问题的 list

python - 从多处理过程更新 Bokeh 图