gpt4 book ai didi

python - 在 Bokeh 服务应用程序中绘制本地镜像

转载 作者:行者123 更新时间:2023-11-28 18:14:48 26 4
gpt4 key购买 nike

我正在尝试使用本地存储在应用程序 ./static 目录中的 ImageURL 类绘制 png 图像。在下面的代码中,当对同一张图片使用 web url 时,它按预期工作,但所有创建本地 url 的尝试都失败了。此外,当运行基本相同的代码并输出到文件时,所有 url 情况都可以正常工作。

Bokeh 服务 --show

import os
import numpy as np
from bokeh.plotting import curdoc
from bokeh.models import ColumnDataSource, Range1d, Plot
from bokeh.models.glyphs import ImageURL

url= "http://pngimg.com/uploads/palm_tree/palm_tree_PNG2504.png", # works
# url= "static/palm.png", # 404 GET /static/palm.png
# url=os.path.join(os.path.dirname(__file__), 'palm.png'), # 404 GET /Volumes/Home/Code/scratch/palm.png
# url='file://'+os.path.join(os.path.dirname(__file__), 'static', 'palm.png'),
N = 1
source = ColumnDataSource(dict(
url = [url]*N,
x1 = np.linspace( 1, 1, N),
y1 = np.linspace( 1, 1, N),
w1 = np.linspace( 253, 253, N),
h1 = np.linspace( 409, 409, N),
))
p = Plot(
title=None,
x_range=Range1d(start=0, end=500),
y_range=Range1d(start=0, end=500),
plot_width=500,
plot_height=500,
h_symmetry=False,
v_symmetry=False,
min_border=0,
toolbar_location=None
)
p.add_glyph(source, ImageURL(url="url", x="x1", y="y1", w="w1", h="h1", anchor="bottom_left"))
curdoc().add_root(p)

python main.py

from bokeh.plotting import show, output_file
output_file("tbe.html")

import os
import numpy as np
from bokeh.plotting import curdoc
from bokeh.models import ColumnDataSource, Range1d, Plot
from bokeh.models.glyphs import ImageURL
# all work
url = "http://pngimg.com/uploads/palm_tree/palm_tree_PNG2504.png",
url = "static/palm.png",
url = os.path.join(os.path.dirname(__file__), 'static', 'palm.png'),
url ='file://'+os.path.join(os.path.dirname(__file__), 'static', 'palm.png'),
N = 1
source = ColumnDataSource(dict(
url = [url]*N,
x1 = np.linspace( 1, 1, N),
y1 = np.linspace( 1, 1, N),
w1 = np.linspace( 253, 253, N),
h1 = np.linspace( 409, 409, N),
))
p = Plot(
title=None,
x_range=Range1d(start=0, end=500),
y_range=Range1d(start=0, end=500),
plot_width=500,
plot_height=500,
h_symmetry=False,
v_symmetry=False,
min_border=0,
toolbar_location=None
)
p.add_glyph(source, ImageURL(url="url", x="x1", y="y1", w="w1", h="h1", anchor="bottom_left"))
show(p)

最佳答案

this answer 中的建议,您必须从脚本上方的目录启动 bokeh serve(这意味着您必须将脚本重命名为 main.py)。然后你可以使用:

url = os.path.join(os.path.basename(os.path.dirname(__file__)), "static", "palm.png")

例如:

your_folder/
+main.py
+static/
+palm.png

启动 Bokeh 作为bokeh serve your_folder --show。图像的最终可达地址将是 http://localhost:5006/your_folder/static/palm.png

关于python - 在 Bokeh 服务应用程序中绘制本地镜像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49055972/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com