简体   繁体   English

python bokeh散景包不会在jupyter笔记本中缓存图

[英]python bokeh plotting package does not cache plots in jupyter notebook

Been running into a frustrating problem with the bokeh plotting package in python. python中的bokeh绘图包遇到了一个令人沮丧的问题。 So I have a jupyter notebook (notebook version 5.0.0) in which I have some bokeh plots. 所以我有一个jupyter笔记本(笔记本版本5.0.0),其中有一些散景图。 The notebook is pretty large now, so it does take a bit of time to load. 笔记本现在非常大,因此加载确实需要一些时间。 Anyhow, when I was using Matplotlib the images in the notebook would be cached. 无论如何,当我使用Matplotlib时,笔记本中的图像将被缓存。 That way I would not need to rerun them each time I ran the notebook. 这样,我每次运行笔记本时都不需要重新运行它们。

Bokeh has the same ability to cache the images, but I cannot seem to get the image cache to work. Bokeh具有缓存图像的相同功能,但是我似乎无法使图像缓存正常工作。 So for a very simple example, if I had the following code in a notebook: 因此,对于一个非常简单的示例,如果我在笔记本中有以下代码:

from bokeh.resources import INLINE
import builtins
import os, sys
import time
import pyugend
import datetime
from IPython.lib import deepreload
builtins.reload = deepreload.reload
from ipywidgets import widgets
from IPython.display import display
from bokeh.io import show, output_notebook
from bokeh.layouts import gridplot
from bokeh.palettes import Viridis3
from bokeh.plotting import figure
from bokeh.charts import defaults
from bokeh import mpl
defaults.width = 700
defaults.height = 700

output_notebook(resources=INLINE)
#output_notebook()
#notebook_handle=True
%reload_ext autoreload
time.sleep(1)

from bokeh.sampledata.iris import flowers

colormap = {'setosa': 'red', 'versicolor': 'green', 'virginica': 'blue'}
colors = [colormap[x] for x in flowers['species']]

p = figure(title = "Iris Morphology")
p.xaxis.axis_label = 'Petal Length'
p.yaxis.axis_label = 'Petal Width'

p.circle(flowers["petal_length"], flowers["petal_width"],
         color=colors, fill_alpha=0.2, size=10)


show(p)

Running this plot works just fine. 运行该图可以正常工作。 But when I saved the notebook, closed it, and reopened it, the plot would not show up again. 但是,当我保存笔记本,关闭并重新打开笔记本时,该图将不再显示。

Anyone else have this issue. 其他人都有这个问题。

I figured out the answer to this question. 我想出了这个问题的答案。 So the problem was with the Jupyter initialization cells extension. 所以问题出在Jupyter初始化单元扩展上。 I had the code to do the imports of bokeh in the initialization cells at the bottom of my notebook. 我有代码可以在笔记本底部的初始化单元中导入bokeh But when I did this for some reason the plotting cells that were earlier in the notebook were not displaying the cached images. 但是当我出于某种原因这样做时,笔记本中较早的绘图单元没有显示缓存的图像。

So I had to basically put the imports of the initialization cells at the very beginning of the notebook. 因此,我必须基本上将初始化单元的导入放在笔记本的最开始。 Only then did the caching of images work. 只有这样,图像缓存才起作用。 Interesting problem. 有趣的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM