首页 > 解决方案 > 散景如何从javascript更改图形上的im​​age_url

问题描述

我正在使用散景来绘制我的图表。所以我需要有一个图像作为背景,我需要每隔 X 秒发出一次 http 请求,并且根据 http 请求的响应,我将更改图形图像背景。

所以这是代码:

img_yellow = "http://localhost:8000/static/images/semaforo_yellow.png"
img_green = "http://localhost:8000/static/images/semaforo_verde.png"
img_red = "http://localhost:8000/static/images/semaforo_rosso.png"
p = figure(x_range=(0, 1), y_range=(0, 1))
p.image_url(url=[img_yellow], x=0, y=1)
source = AjaxDataSource(data_url="http://localhost:8000/rt/semaforo",
                            polling_interval=50000)
p.circle('x', 'y', source=source)
print_button = Button(label="Print")
    print_button.js_on_click(CustomJS(args=dict(source=ecg_source, plot=p), code="""
                    console.log("pippoprint");
                    plot.url = "http://localhost:8000/static/images/semaforo_rosso.png";
                    plot.image_url = "http://localhost:8000/static/images/semaforo_rosso.png";
                    console.log(plot);
                    plot.change.emit();
                """))

在测试应用程序的那一刻,我使用了一个按钮,如果我单击此按钮,我应该更改图像,但现在不工作。

标签: javascriptpythonbokeh

解决方案


this is not youre looking for but with server side

img_yellow = "http://localhost:8000/static/images/semaforo_yellow.png"
img_green = "http://localhost:8000/static/images/semaforo_verde.png"
img_red = "http://localhost:8000/static/images/semaforo_rosso.png"

def change_img(attr, old, new):
    p.image_url.url = img_yellow

p = figure(x_range=(0, 1), y_range=(0, 1))
p.image_url(url=[img_yellow], x=0, y=1)
source = AjaxDataSource(data_url="http://localhost:8000/rt/semaforo",
                            polling_interval=50000)
p.circle('x', 'y', source=source)
print_button = Button(label="Print")
    print_button.on_click(change_img)

推荐阅读