은 현재 상호 작용을위한 여러 가지 방법을 혼합하고 있지만 불행하게도 당신은 항상 각각 다른 방식으로 뭔가를 그리워.
사용하는 슬라이더는 bokeh이지만, 불행히도 slider.on_change
은 bokeh 서버를 실행하는 경우에만 작동합니다. documentation에서 :
사용 나뭇잎은 보케 서버를 시작하고 .on_change (또는 위젯, .on_click)와 이벤트 핸들러를 설정하는 역할을한다.
실제로 나는 jupyter 노트북과 bokeh 서버를 실행하는 데 그다지 많은 것을 찾을 수 없었지만, this issue은 그 가능성을 논의하는 것으로 보입니다. 그것은 또한 bokeh.application
을 언급하지만 결코 사용하지 못했습니다. 그래서 어떻게 작동하는지 모르겠습니다.
또한 jupyter 커널을 호출하고 update_plot(value)
을 실행하려고하는 사용자 정의 js 콜백을 추가로 사용하지만 사용자 정의 콜백은 아무 것도하지 않으므로 이러한 기능을 정의하지 않았습니다.
그런 다음 데이터를 출력으로 푸시해야합니다. 나는 bokeh 서버가 어떻게 든 그럴 수 있다고 생각한다. bokeh 서버가없는 jupyter 노트북의 경우 push_notebook
가 해결책 인 것 같다. 푸시 할 수 있으려면 show(..., notebook_handle=True)
이 필요합니다.
해결 방법 1 명을 사용하면 slider.on_change
을 사용할 수 있도록
슬라이더와 다른 사람이 다시 파이썬으로 자신의 상태를 자동으로 동기화 위젯 보케 서버. CustomJS가 필요하지 않습니다. 데이터 흐름은 다음과 같아야합니다
python script -> bokeh server -> html -> userinput -> bokeh server -> python callbacks -> bokeh server updates plots
솔루션 2 개를 사용 나뭇잎 슬라이더하지만 동기화를 CustomJS
를 통해 당신이 코드를 실행하기 위해 jupyter 커널을 사용할 수있는 별도의 프로세스를 실행하지 않으려면 귀하의 파이썬 노트북. 데이터 흐름 :
jupyter notebook -> html -> user input -> customjs -> jupyter kernel -> python callbacks -> push_notebook to update plots
output_notebook()
N = 300
source = ColumnDataSource(data={'x':random(N), 'y':random(N)})
plot = figure(plot_width=950, plot_height=400)
plot.circle(x='x', y='y', source=source)
callback = CustomJS(code="""
if (IPython.notebook.kernel !== undefined) {
var kernel = IPython.notebook.kernel;
cmd = "update_plot(" + cb_obj.value + ")";
kernel.execute(cmd, {}, {})};
""")
slider = Slider(start=100, end=1000, value=N, step=10, callback=callback)
# must have the same name as the function that the CustomJS tries to call
def update_plot(N):
source.data={'x':random(N), 'y':random(N)}
# push notebooks to update plots
push_notebook()
layout = column(slider, plot)
# notebook_handle must be true, otherwise push_notebook will not work
h1 = show(layout, notebook_handle=True)
해결 방법 3을 사용 ipywidgets 당신이 jupyter 노트북의 상호 작용을 위해 설계되어 ipywidgets를 사용할 수있는 나뭇잎 위젯 결혼하지 않는 경우
. 데이터 흐름은 다음과 같습니다 :
jupyter notebook -> html -> user input -> ipywidgets sync automatically -> python callbacks -> push_notebook
내가 여기 interact
사용하지만 예상대로 다른 위젯이 작동합니다. 당신이 CONDA을 사용하지 않는 경우 jupyter nbextension enable --py --sys-prefix widgetsnbextension
를 호출 inlcudes 제대로 ipywidgets를 설치해야
from ipywidgets import interact
output_notebook()
N = 300
source = ColumnDataSource(data={'x':random(N), 'y':random(N)})
plot = figure(plot_width=950, plot_height=400)
plot.circle(x='x', y='y', source=source)
def update_plot(v):
N = v
print(N)
source.data={'x':random(N), 'y':random(N)}
# push changed plots to the frontend
push_notebook()
# notebook_handle must be true so that push_notebook works
show(plot, notebook_handle=True)
참고. 자세한 내용은 see the documentation
[this] (https://bokeh.pydata.org/en/latest/docs/user_guide/notebook.html#jupyter-interactors) 가이드를 보았습니까? 중요한 'push_notebook'이 누락되었습니다. – syntonym
답장을 보내 주셔서 감사합니다. Jupyter에서이 코드를 실행했으며 완벽하게 작동합니다. 그러나 push_notebook을 내 코드에 추가해도 아무런 변화가 없습니다. 이걸 당신 기계에서 실행 했습니까? – AdrianC
'show' 함수에서'notebook_handle = True'도 필요하다고 생각합니다. 현재 귀하의 코드를 실행할 수는 없지만 아무도 당신을 도울 수없는 경우 오늘 저녁에 보겠습니다. 예를 들어 가져 오기 등을 추가하여 더 많은 사람들이 당신을 조속히 도와 줄 수 있습니다. 나는 왜 당신이 커스텀 js 콜백을하고 있는지 확실하지 않다. 아마도 웨지 예제에서? 제 생각에는'plot.circle'은 필요 없다고 생각하지만 오늘 저녁 테스트 만 할 수 있습니다. – syntonym