【问题标题】:How can I plot an image with javascript, which I saved before in the docker workdir?如何使用我之前保存在 docker workdir 中的 javascript 绘制图像?
【发布时间】:2019-08-20 07:18:27
【问题描述】:

我正在运行一个 docker 容器,在这个容器内,我将一个图像保存到容器的工作目录中。我想用 javascript 在网站上绘制这张图片。在容器内部,正在运行一个烧瓶服务。

将绘图保存到 docker 容器的工作目录中:

plt.savefig('/app/TrainingAccLoss.png')

我的 Dockerfile:

# Use an official Python runtime as a parent image
FROM python:3.5

# Set the working directory to /app
WORKDIR /app
# Copy the current directory contents into the container at /app
COPY . /app

# Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt

# Make port 80 available to the world outside this container
EXPOSE 80

# Define environment variable
ENV NAME World

# Run app.py when the container launches
CMD ["python", "app.py"]

尝试绘制图像:

var img = document.createElement("img");
img.src = "/app/TrainingAccLoss.png";
document.body.appendChild(img);

当我尝试绘制图像时,出现 404 错误。

获取http://localhost:4000/app/TrainingAccLoss.png 404(未找到)。

我不知道是否有可能访问工作目录中的文件。

【问题讨论】:

  • 该页面/浏览器是否也在 docker 容器中运行?
  • 是的,网页也在docker容器中运行
  • 确保您在 Flask 中指定了正确的静态文件夹。 stackoverflow.com/questions/20646822/…

标签: javascript python docker flask


【解决方案1】:
var img = document.createElement("img");
    img.src = window.origin + "/TrainingAccLoss.png";

document.body.appendChild(img);

如果您的应用正在为应用文件夹提供服务,这应该可以工作。

【讨论】:

  • 不幸的是,这不起作用,但我也不确定“如果您的应用正在为应用文件夹提供服务”是什么意思。
  • 我的意思是您的代码正在使用 DOM 操作并在浏览器上运行,所以我假设您使用您的 python 服务器将上述代码作为应用程序文件夹中的静态文件提供。可能没有提供图像文件,因此无法通过 http 请求访问。也许你可以运行你的 docker,然后在你的浏览器上检查图像是否正在被服务,试试:localhost/TrainingAccLoss.pnglocalhost/app/TrainingAccLoss.png
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-19
  • 1970-01-01
相关资源
最近更新 更多