【问题标题】:Python (Flask) - Show loading message/gif for matplotlib generated image (html)Python (Flask) - 显示 matplotlib 生成图像 (html) 的加载消息/gif
【发布时间】:2015-02-10 02:58:07
【问题描述】:

总的来说,我对 Web 开发还比较陌生,所以这个问题可能显示出一些经验不足。我很抱歉。

工作应用程序:用户在 /showgraph 路径中输入 x 和 y 值 --> matplotlib 生成显示在同一路径中的图。

这是与我的问题类似的代码部分:

html

...
<form class="graphs" method="POST", action="{{ url_for('showgraph') }}">
  <input placeholder="x-value" name="valx" id="xvalue">
  <input placeholder="y-value" name="valy" id="yvalue">
  <input type="submit" name="showgraph" value="Show Graph" id="inputvalues">
</form>
{% if buttonclick[0]=="Show Graph" %}   
    <img src="{{ url_for('fig', val=val) }}" alt="Image Placeholder"  height="400">
{% endif %}
...

Python(使用 Flask)

@app.route('/fig/<val>', methods=['GET', 'POST'])
def fig(val):
    import matplotlib.pyplot as plt  
    import StringIO
    import ast
    val=ast.literal_eval(str(val))
    x=[float(i) for i in val[0][0].strip('[]').split(',')]
    y=[float(i) for i in val[1][0].strip('[]').split(',')]
    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)
    ax.plot(x,y)
    img = StringIO.StringIO()
    fig.savefig(img)
    img.seek(0)
    return send_file(img, mimetype='image/png')


@app.route('/showgraph/',methods=['GET','POST'])
def showgraph():
   valx=(request.form.getlist('valx'))
   valy=(request.form.getlist('valy'))
   val='('+str(valx)+','+str(valy)+')'
   buttonclick=request.form.getlist('showgraph') 
   return render_template("graphs.html",buttonclick=buttonclick,val=val)

现在,当我输入一些 x 和 y 值(例如 x=[1,2,3] 和 y=[3,2,1])时,页面首先显示 alt(“图像占位符”),然后显示 5 或显示图像(图表)的 6 秒。

有没有办法解决这个问题,只显示加载消息,gif,..?

(我仍在研究一种更高效、更简洁的方法来制作输入值数组。我可以想象这种方法对某些人来说可能看起来有点曲折。)

谢谢!

【问题讨论】:

    标签: python html image matplotlib flask


    【解决方案1】:

    你可以使用img标签的lowsrc attribute。而不是

    <img src="{{ url_for('fig', val=val) }}" alt="Image Placeholder"  height="400">
    

    试试

    <img src="{{ url_for('fig', val=val) }}" alt="Image Placeholder"  height="400" 
         lowsrc="/images/loading.gif">
    

    其中loading.gif 是一个占位符图像,显示(可能是动画?)加载消息。

    提示:您可以在http://www.ajaxload.info/创建加载 GIF

    【讨论】:

    • 这似乎不能解决问题。我也尝试用 CSS(背景图像)解决它,但得到了相同的结果。
    猜你喜欢
    • 2013-11-26
    • 2015-06-26
    • 2012-01-08
    • 1970-01-01
    • 2014-04-10
    • 2012-06-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多