【发布时间】: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