首页 > 解决方案 > VSCode 笔记本静默显示失败(modify_doc)

问题描述

在 Visual Studio Code 中使用内置的 Jupyter Notebook 编辑器并使用show(modify_doc)绘图的方式时,不显示结果。显示单个图确实有效。

我尝试使用谷歌搜索并阅读文档,但在任何地方都找不到解决方案。希望有人知道这里出了什么问题。

(示例取自网络)

导入必要的模块:

from bokeh.io import push_notebook, show, output_notebook
from bokeh.layouts import row, gridplot
from bokeh.plotting import figure, show, output_file, curdoc
from bokeh.document import Document
output_notebook()
import numpy as np

这在 VSCode 中不能按预期工作,但在官方 Jupyter Notebook 中可以。我添加了一个打印语句作为测试,它也没有在 VSCode 中打印。

def modify_doc(doc):
    print("Test")
    x = np.linspace(0, 4*np.pi, 100)
    y = np.sin(x)
    TOOLS = "pan,wheel_zoom,box_zoom,reset,save,box_select"

    p1 = figure(title="Legend Example", tools=TOOLS)
    p1.circle(x,   y, legend="sin(x)")
    p1.circle(x, 2*y, legend="2*sin(x)", color="orange")
    p1.circle(x, 3*y, legend="3*sin(x)", color="green")


    # Add everything to the layout
    layout = row(p1)


    # Add the layout to curdoc
    doc.add_root(layout)

show(modify_doc)

以下代码确实运行:

x2 = np.linspace(0, 4*np.pi, 100)
y2 = np.sin(x2)
TOOLS2 = "pan,wheel_zoom,box_zoom,reset,save,box_select"

p2 = figure(title="Legend Example", tools=TOOLS2)
p2.circle(x2,   y2, legend="sin(x)")
p2.circle(x2, 2*y2, legend="2*sin(x)", color="orange")
p2.circle(x2, 3*y2, legend="3*sin(x)", color="green")

show(p2)

显示在 VSCode 中运行的两个代码片段

标签: pythonpython-3.xvisual-studio-codejupyter-notebookbokeh

解决方案


我有一个类似的问题。show()对我来说,它曾经在 VS Code中的 jupyter 笔记本单元格中输出散景图。但是即使我没有对代码进行更改(或者更准确地还原更改),它也突然停止工作。

我可以通过将 Anaconda 环境更改为另一个环境然后切换回正确的环境来解决此问题(请参见下面的屏幕截图)。

在此处输入图像描述


推荐阅读