首页 > 解决方案 > 在 JupyterHub 中与 Bokeh 服务器交互

问题描述

我有一个简单的散景交互笔记本,可以捕捉用户绘制的坐标,它在 Jupyter 中运行良好:

2018-07-03_6-52-18

import numpy as np

N = 500
x = np.linspace(0, 3, N)
y = np.linspace(0, 3, N)

xx, yy = np.meshgrid(x, y)
z = np.sin(xx) * np.cos(yy)

from bokeh import events
from bokeh.io import show, output_notebook
from bokeh.plotting import figure

output_notebook()

geom = {}
global geom

def print_event(attributes=[]):
    def python_callback(event):
        geom.update(event.__dict__['geometry'])
    return python_callback

def modify_doc(doc):
    p = figure(x_range=(0, 3), y_range=(0, 3), 
      tools='reset,box_select,lasso_select,poly_select', plot_height=300)
    p.image(image=[z], x=0, y=0, dw=3, dh=3, palette='Spectral11')
    p.on_event(events.SelectionGeometry, print_event(attributes=['geometry']))
    doc.add_root(p)

show(modify_doc)

print(geom)

但是在 JupyterHubshow(modify_doc)中不会生成绘图,并且开发人员控制台显示 JS 无法从该请求中加载响应数据:

http://localhost:43474/autoload.js?bokeh-autoload-element=111c97fa-dbc8-437c-9770-471dc23fb13f&bokeh-absolute-url=http://localhost:43474&resources=none

因为我在我的 jupyterhub 上访问 Dask 仪表板

http://pangeo.esipfed.org/user/rsignell-usgs/proxy/8787

我虽然也许这样的事情可以工作:

show(modify_doc, notebook_url='pangeo.esipfed.org/user/rsignell-usgs/proxy')

但生成了这个 URL:

http://pangeo.esipfed.org/user/rsignell-usgs/proxy:34560/autoload.js?bokeh-autoload-element=870004ec-7366-4b38-b20f-2119e2b52327&bokeh-app-path=/user/rsignell-usgs/proxy:34560&bokeh-absolute-url=http://pangeo.esipfed.org/user/rsignell-usgs/proxy:34560&resources=none

这给出了一个404错误。

我看到几个月前有一个Bokeh 增强应该允许这种交互工作,我认为我应该能够指定notebook_url一个函数来完成这项工作,但我无法弄清楚。

是否清楚我做错了什么,或者有人有一个例子说明这是如何工作的?

标签: pythonbokehjupyterhub

解决方案


Bokeh 的文档提到了一些设置,当通过 jupyterhub 代理时,您需要做一些设置来与 bokeh 服务器交互:

https://docs.bokeh.org/en/latest/docs/user_guide/notebook.html#jupyterhub

希望这会有所帮助。


推荐阅读