无法在 IPython 中嵌入散景

Can't inline Bokeh in IPython

我安装了最新的 Bokeh 和 IPython,并尝试 运行 执行以下操作: (所有这些导入在后面的代码中都是相关的)

import pandas as pd
import datetime
import matplotlib.pyplot as plt
import itertools as itt
import bokeh.plotting as bk
bk.output_notebook()

xs = [0,1,2,3,4,5]
ys = [x**2 for x in xs]

p.line(xs, ys, line_width=2)
p.circle(xs,ys)
bk.show(p)

在 运行这 2 个单元格之后,我得到:

Javascript error adding output! ReferenceError: Bokeh is not defined See your browser Javascript console for more details.

所以,我 运行 控制台看到了这个:

ReferenceError: Bokeh is not defined Stack trace: @http://localhost:8888/static/components/jquery/jquery.min.js?v=20150304125302 line 4 > eval:1:1 .globalEval@http://localhost:8888/static/components/jquery/jquery.min.js?v=20150304125302:4:4231 .domManip@http://localhost:8888/static/components/jquery/jquery.min.js?v=20150304125302:5:21389 .append@http://localhost:8888/static/components/jquery/jquery.min.js?v=20150304125302:5:18980 OutputArea.prototype._safe_append@http://localhost:8888/static/notebook/js/outputarea.js?v=20150304125302:414:13 OutputArea.prototype.append_display_data@http://localhost:8888/static/notebook/js/outputarea.js?v=20150304125302:534:13 OutputArea.prototype.append_output@http://localhost:8888/static/notebook/js/outputarea.js?v=20150304125302:320:13 OutputArea.prototype.handle_output@http://localhost:8888/static/notebook/js/outputarea.js?v=20150304125302:234:9 CodeCell.prototype.get_callbacks/<.iopub.output@http://localhost:8888/static/notebook/js/codecell.js?v=20150304125302:456:21 Kernel.prototype._handle_output_message@http://localhost:8888/static/services/kernels/kernel.js?v=20150304125302:997:13 .proxy/i@http://localhost:8888/static/components/jquery/jquery.min.js?v=20150304125302:4:5486 Kernel.prototype._handle_iopub_message@http://localhost:8888/static/services/kernels/kernel.js?v=20150304125302:1024:13 Kernel.prototype._finish_ws_message@http://localhost:8888/static/services/kernels/kernel.js?v=20150304125302:866:17 .proxy/i@http://localhost:8888/static/components/jquery/jquery.min.js?v=20150304125302:4:5486 deserialize@http://localhost:8888/static/services/kernels/serialize.js?v=20150304125302:60:13 Kernel.prototype._handle_ws_message@http://localhost:8888/static/services/kernels/kernel.js?v=20150304125302:857:9 .proxy/i@http://localhost:8888/static/components/jquery/jquery.min.js?v=20150304125302:4:5486 outputarea.js:416

在看到这个之前,bk.show(p)显示了一个扭曲的情节,说"Hello Word",但是所有的按钮都变形了。 matplotlib 工作正常。

所以,我重启了几次系统,又开始了一个新的笔记本,突然一切正常。我猜问题出在其中一个进口产品上,但我不是 100% 确定。现在一切正常。

此问题已在 https://github.com/bokeh/bokeh/issues/2024 中解决,它将加载 BokehJS 的代码移动到 output_notebook 中,而不是仅在导入 bokeh 时尝试这样做。但是,我仍然建议将 bk.output_notebook 放在 自己的 ipython 单元格中,别无其他。 我将尽快更新文档以反映此建议。问题是 Bokeh 使用 IPython 的 "publish" 机制来加载自身,如果您将其他东西放在也有输出的单元格中,它可能会干扰它。这就是 IPython notebook 的工作方式,除了建议将 output_notebook 放在它自己的位置之外,没有什么可做的,所以它的输出保证是正确的。

这里的问题是 Javascript 函数在 IPython.core.display 中加载,忽略它并重新 运行 您的代码。