我是Flask的新手,不熟悉其工具。 所以,我正在尝试将CSV文件上传到我的应用程序;为了使用它的数据使用Pygal绘制一些图形。
这是我的尝试,但它不起作用:
def upload_file():
return render_template('upload.html')
@app.route('/uploader', methods = ['GET', 'POST'])
def upload():
if request.method == 'POST':
f = request.files['file[]']
f.save(secure_filename(f.filename))
x= np.asarray(f)
graph = pygal.Line()
graph.title = '% Change Coolness of programming languages over time.'
graph.add('Graph', x[1,:])
graph_data = graph.render_data_uri()
return render_template("upload.html", graph_data = graph_data)
HTML代码:
{% extends "layout.html" %}
{% block content %}
<body class="body">
<div class="container" align="left">
<embed type="image/svg+xml" src={{graph_data|safe}} style='max-width:1000px'/>
</div>
<form action = "http://localhost:5000/uploader" method = "POST" enctype = "multipart/form-data">
<input type = "file" name ="file" />
<input type = "submit"/>
</form>
</body>
{% endblock %}
错误信息是:
'错误:执行中止'
并且此感叹号出现在“graph.add”行旁边,作为空格错误!
答案 0 :(得分:0)
谢谢! 我刚解决了这个问题,实际上,这是由于对请求方法缺乏了解。 简单地说,在我在“genfromtxt”中使用此路径之前,我使用os.path方法确定了上传的文件路径,就像我们通常那样。
这是我的解决方案:
@app.route('/upload')
def upload_file():
return render_template('upload.html')
@app.route('/uploader', methods = ['GET', 'POST'])
def upload():
if request.method == 'POST':
f = request.files['file']
f.save(secure_filename(f.filename))
data= genfromtxt(os.path.abspath(f.filename) , delimiter=',')
graph = pygal.Line()
if data.shape[1]==0:
graph.add('Data', data)
else:
graph.add('Data', data[1,:])
graph_data = graph.render_data_uri()
return render_template("upload.html", graph_data = graph_data)