作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正准备使用 Bokeh 来实现我编写的一些 python 模型的交互式在线实现。
第 1 步是了解一些基本的交互式示例,但我无法在 Jupyter 笔记本中交互式运行介绍性示例。我希望有人能纠正我对什么是 bokeh 自己的示例代码的复制粘贴的误解。
我知道 Bokeh 文档并不完美(我修复了对 bokeh.plotting.show
而不是 io.show
的过时引用),但我认为我使用的基本结构应该接近正确。
代码基于:
https://github.com/bokeh/bokeh/blob/master/examples/app/sliders.py
https://docs.bokeh.org/en/latest/docs/user_guide/notebook.html
############ START BOILERPLATE ############
#### Interactivity -- BOKEH
import bokeh.plotting.figure as bk_figure
from bokeh.io import curdoc, show
from bokeh.layouts import row, widgetbox
from bokeh.models import ColumnDataSource
from bokeh.models.widgets import Slider, TextInput
from bokeh.io import output_notebook # enables plot interface in J notebook
# init bokeh
output_notebook()
############ END BOILERPLATE ############
# 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 = bk_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)
# 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)
### I thought I might need a show() here, but it doesn't make a difference if I add one
# show(layout)
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)
layout = row(plot,
widgetbox(text, offset, amplitude, phase, freq))
curdoc().add_root(row(inputs, layout, width=800))
curdoc().title = "Sliders"
show(layout)
push_notebook
这样的特定于 jupyter 的解决方法.
最佳答案
我同意(作为用户)文档在这方面可能会更好。我不得不搜索很多来找到程序,但是当你找到它时,它并不难!我修改了你的代码,你可以在 Jupyter notebook 中运行它。
诀窍是:
from bokeh.application import Application
from bokeh.application.handlers import FunctionHandler
.
.
<your code here>
.
.
#add server-related code inside this modify_doc function
def modify_doc(doc): #use doc as you use curdoc() in bokeh server
doc.add_root(<your_layout>)
doc.on_change(...)
doc.add_periodic_callback(...)
handler = FunctionHandler(modify_doc)
app = Application(handler)
show(app)
############ START BOILERPLATE ############
#### Interactivity -- BOKEH
import bokeh.plotting.figure as bk_figure
from bokeh.io import curdoc, show
from bokeh.layouts import row, widgetbox
from bokeh.models import ColumnDataSource
from bokeh.models.widgets import Slider, TextInput
from bokeh.io import output_notebook # enables plot interface in J notebook
import numpy as np
# init bokeh
from bokeh.application import Application
from bokeh.application.handlers import FunctionHandler
output_notebook()
############ END BOILERPLATE ############
# 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 = bk_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)
# Set up callbacks
def update_title(attrname, old, new):
plot.title.text = text.value
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)
### I thought I might need a show() here, but it doesn't make a difference if I add one
# show(layout)
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)
layout = row(plot,
widgetbox(text, offset, amplitude, phase, freq))
def modify_doc(doc):
doc.add_root(row(layout, width=800))
doc.title = "Sliders"
text.on_change('value', update_title)
handler = FunctionHandler(modify_doc)
app = Application(handler)
show(app)
关于python - 如何在 Jupyter 笔记本中获得交互式 Bokeh ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53217654/
我是一名优秀的程序员,十分优秀!