【问题标题】:Using static folder in Flask for multiple images [duplicate]在 Flask 中为多个图像使用静态文件夹 [重复]
【发布时间】:2018-08-15 09:50:56
【问题描述】:

我目前正在将两个 matplotlib 图保存到 Flask 应用程序的 /static 文件夹中。

但是,当它们以 HTML 呈现时。他们都只渲染第一张图像。不是我所期望的两个单独的图像。

name_img_1 = 'static/1.png?%d' % time.time()
name_img_2 = 'static/2.png?%d' % time.time()

plt.plot( 'Time stamp 1', 'pred1', data=data, linestyle='-', marker='o')
plt.title(' classifier 1')
plt.axhline(y=0.55,label='Threshold (=0.55)')
plt.legend()
plt.savefig(name_img_1,format='png')

plt.plot( 'Time stamp 1', 'pred2', data=data, linestyle='-', marker='o')
plt.title(' classifier 1')
plt.axhline(y=0.55,label='Threshold (=0.72)')
plt.legend()
plt.savefig(name_img_2,format='png')




return render_template("hello.html",user_image=name_img_1,user_image_2=name_img_3,form=form)

HTML 代码

<img src="{{ user_image }}" alt="User Image">
<img src="{{ user_image_2 }}" alt="User Imageq23">

我尝试创建两个单独的静态文件夹和子目录,但都不起作用。

Full code snippets

【问题讨论】:

    标签: python python-3.x matplotlib flask


    【解决方案1】:

    在您的 render_template 语句中,您为 user_image_2 传递了错误的名称。由于没有 name_image_3,jinja 忽略它

    return render_template("hello.html",user_image=name_img_1,user_image_2=name_img_3,form=form)
    

    试试

    return render_template("hello.html",user_image=name_img_1,user_image_2=name_img_2,form=form)
    

    【讨论】:

      猜你喜欢
      • 2019-09-19
      • 2021-03-15
      • 1970-01-01
      • 1970-01-01
      • 2020-01-14
      • 2014-06-06
      • 1970-01-01
      • 2020-05-10
      • 2022-01-03
      相关资源
      最近更新 更多