简体   繁体   English

如何在 ipython 笔记本中重新加载图像?

[英]How to reload image in ipython notebook?

In my notebook, I can display image in markdown from the same folder like this:在我的笔记本中,我可以像这样从同一个文件夹以降价显示图像:

<img src="files/adaptive_filter.png" alt="Schema of adaptive filter" height="100"> 

If I use the code without the files/ in src it does not work.如果我在src使用没有files/的代码,它就不起作用。

Now I changed the image and the ipython notebook is still showing the original one.现在我更改了图像,ipython notebook 仍然显示原始图像。 I try to remove it from code and restart the notebook, it does not help.我尝试从代码中删除它并重新启动笔记本,它没有帮助。

What I should do?我应该怎么做? Are the images stored somewhere?图像是否存储在某处?

Thanks in advance.提前致谢。

I ran into this problem as well, where I was using a class of my own to output some python plots and embed them in an IPython notebook.我也遇到了这个问题,我使用自己的一个类来输出一些 python 绘图并将它们嵌入到 IPython 笔记本中。 A hack way to solve this would be to add a random argument to the end of your image url.解决此问题的一种黑客方法是在图像 url 的末尾添加一个随机参数。 For example例如

<img src="files/adaptive_filter.png?1" alt="Schema of adaptive filter" height="100">

will not be cached in the same place as不会缓存在同一个地方

<img src="files/adaptive_filter.png?2" alt="Schema of adaptive filter" height="100">

A programatic way to do this would be to include the picture via python, instead of markdown, for instance:执行此操作的一种编程方法是通过 python 包含图片,而不是 Markdown,例如:

# pick a random integer with 1 in 2 billion chance of getting the same
# integer twice
import random
__counter__ = random.randint(0,2e9)

# now use IPython's rich display to display the html image with the
# new argument
from IPython.display import HTML, display
display(HTML('<img src="files/adaptive_filter.png?%d" ' +
             'alt="Schema of adaptive filter" ' +
             'height="100">' % __counter__))

Should update the image everytime you run the code cell每次运行代码单元时都应该更新图像

I ran into exactly the same problem.我遇到了完全相同的问题。 The following procedure works for me:以下程序对我有用:

In the folder where your .ipynb file resides, there is a cache directory called .ipynb_checkpoints/.在 .ipynb 文件所在的文件夹中,有一个名为 .ipynb_checkpoints/ 的缓存目录。 There should be a file in that cache directory that has the same file name as the one you are working on.该缓存目录中应该有一个与您正在处理的文件名相同的文件。 Now remove/delete that cache file in the .ipynb_checkpoint/ directory then reload the browser.现在删除/删除 .ipynb_checkpoint/ 目录中的缓存文件,然后重新加载浏览器。 You should be able to see the updated image.您应该能够看到更新后的图像。

My environment: macOS 10.14.2, Chrome browser 71.0, and Jupyter 1.0.0 installed through anaconda.我的环境:macOS 10.14.2,Chrome浏览器71.0,通过anaconda安装的Jupyter 1.0.0。

Hope this helps.希望这会有所帮助。

  1. In ipynb file directory,use ls command ,it will display a hidden directory .ipynb_checkpoints在 ipynb 文件目录中,使用ls命令,它将显示一个隐藏目录.ipynb_checkpoints

  2. Delete the .ipynb_checkpoints directory ,then rerun the cell code,you will see the newest image.删除.ipynb_checkpoints目录,然后重新运行单元代码,您将看到最新的图像。

I had the same problem of images caching in jupyter and not updating when the file is modified when using matplotlib.pyplot .我在 jupyter 中遇到了同样的图像缓存问题,并且在使用matplotlib.pyplot修改文件时没有更新。 I fixed it by using IPython.display.Image我使用IPython.display.Image修复了它

from IPython.display import Image
def saveImage(path, show=True): 
    plt.savefig(path, facecolor="white", bbox_inches='tight') 
    if show: return Image(path)

Then at the end of a code cell , something like saveImage('./someImage.png') will save and display an image.然后在代码单元格的末尾,类似saveImage('./someImage.png')将保存并显示图像。

Similarly Image('./someImage.png') will display an image from disk (without saving).同样Image('./someImage.png')将显示磁盘中的图像(不保存)。

This seems to avoid caching issues, and the image displays in PDF exports correctly (see also Jupyter notebook matplotlib figures missing in exported pdf for the answer this is based on).这似乎避免了缓存问题,并且图像在 PDF 导出中正确显示(另请参阅导出的 pdf 中缺少的 Jupyter notebook matplotlib 数字以获取基于此的答案)。

In my case, it helped reduce the size of the jupyter notebook, preventing it from crashing when rendering and updating matplotlib.pyplot charts.就我而言,它有助于减小 jupyter notebook 的大小,防止它在渲染和更新matplotlib.pyplot图表时崩溃。

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

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