在IPython Notebook中自动更新图像 [英] Automatically update image in IPython notebook

查看:235
本文介绍了在IPython Notebook中自动更新图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有许多png图像会定期进行一些更新,并希望将它们包含在IPython笔记本中,并在笔记本中自动进行更新.

I have a number of png images that get updated somewhat regularly and want to include them in an IPython notebook, having them automatically updated in the notebook.

我认为我可以通过修改以下matplotlib示例来做到这一点:

I thought I could do it by adapting the following matplotlib example:

import numpy as np
import matplotlib.pyplot as plt
from IPython import display
import time

%matplotlib inline

plt.ion()

fig = plt.figure()
ax = fig.gca()
fig.show()

for i in range(10):
    mu, sigma = 200, 25
    x = mu + sigma*np.random.normal(loc=2, scale=1, size=50)

    if i == 0:
        barpl = plt.bar(range(len(x)), x)
    else:
        for rect, h in zip(barpl, x):
            rect.set_height(h)
    display.clear_output(wait=True)
    display.display(plt.gcf())
    time.sleep(2)

因此,我使用了以下代码段:

So I used this snippet:

from IPython import display
import os

timestamp = get_latest_file_ts(directory="figures", file_name="fig1.png", strip_directory=True)

display.Image(os.path.join("figures", "fig1.png"))

while True:
    timestamp = check_if_modified_file(directory="figures", file_name="fig1.png", touched_on=timestamp, sleep_time=1, strip_directory=True)
    display.clear_output(wait=True)
    display.Image(os.path.join("figures", "fig1.png"))

但是没有输出.显然,在Image()之后的语句将覆盖显示(并且我不确定即使没有发生此操作也应该起作用).在上面的代码中,get_latest_file_ts()获取图像的最新版本的时间戳,而check_if_modified_file()继续检查同一文件上的新时间戳,并在找到它时返回新时间戳.

But no output is produced. Apparently statements that follow Image() override the display (and I am unsure this was supposed to work even without this happening). In the above code, get_latest_file_ts() fetches the timestamp of the latest version of the image, and check_if_modified_file() keeps checking for a newer timestamp on the same file, and returns the newer timestamp when it finds it.

[更新] 我找到了使用widgets进行此操作的部分方法,但是我的实现在旧代码的末尾创建了一个新的HTML代码块.相反,我想要的是用新的-i替换旧的HTML块.仅替换内容.

[UPDATE] I found a partial way to do this using widgets, but my implementation creates a new HTML block at the end of the old one. What I want instead, is to replace the old HTML block with the new one -i.e. replace the content only.

以下是将一个HTML堆栈在另一个HTML后面的代码:

Here is the code that stacks one HTML behind the other:

from IPython.html.widgets import interact, interactive, fixed
from IPython.html import widgets
from IPython.display import clear_output, display, HTML

def show_figs(directory="figures", file_name="fig1.png"):
    s = """<figure>\n\t<img src="%s" alt="The figure" width="304" height="228">\n</figure>""" % os.path.join(directory, file_name)
    display(HTML(s))

timestamp = get_latest_file_ts(directory="figures", file_name="fig1.png", strip_directory=True)
show_figs()
while True:
    timestamp = check_if_modified_file(directory="figures", file_name="fig1.png", touched_on=timestamp, sleep_time=1, strip_directory=True)
    show_figs()

我真的很高兴有一种方法可以使上面的第二个或第三个代码片段正常工作,或者有一些其他方法可以完成此任务.

I would really appreciate a way to get the second or third snippets above to work, or some other method to get this done.

推荐答案

第一次尝试的问题是,您仅创建了图像对象(display.Image),但显示它!要显示图像,请使用display.display函数,该函数类似于python的print语句,但使用IPython显示机制.您的通话应该看起来像

The problem with your first attempt is that you just create the image object (display.Image) but not display it! To display the image, use the display.display function which is similar to python's print statement but uses the IPython display machinery. Your call should look like

display.display(display.Image(os.path.join("figures", "fig1.png")))

某些背景

如果仅创建图像对象,则仅当返回的对象是执行块中的最后一条命令时才显示返回的对象->图像对象为"OUT"值.请注意,它位于 Out [x] 提示符旁边.在这种特殊情况下,显示设备会自动显示它.

Some background

If you just create the image object, the returned object is displayed only if it is the last command in the execution block -> the image object is the "OUT" value. Note, that it is placed next to the Out[x] prompt. In this particular case, the display machinery automatically displays it.

在您的情况下,您在循环中创建图像而没有捕获它.它不是执行块中的最后一条命令,因此图像对象不会作为输出"值返回,也不会自动显示.后必须手动完成.

In your case you create the image within a loop without capturing it. It is not the last command in the execution block, thus the image object is not returned as "out" value and not displayed automatically. Latter has to be manually done.

这与任何python对象完全相同.如果要查看它,则必须打印(显示)它或(在IPython中)确保它返回为"OUT".

This is totally the same with any python object. If you want to see it you have to print (display) it or (in IPython) make sure it is return as "OUT".

这篇关于在IPython Notebook中自动更新图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆