Flask Python:上传csv文件并在应用程序中使用其数据
我是烧瓶新手,对烧瓶的工具不太熟悉。 所以,我试图上传一个CSV文件到我的应用程序;为了使用它的数据用Pygal绘制一些图形 这是我的尝试,但没有成功:Flask Python:上传csv文件并在应用程序中使用其数据,python,flask,Python,Flask,我是烧瓶新手,对烧瓶的工具不太熟悉。 所以,我试图上传一个CSV文件到我的应用程序;为了使用它的数据用Pygal绘制一些图形 这是我的尝试,但没有成功: def upload_file(): return render_template('upload.html') @app.route('/uploader', methods = ['GET', 'POST']) def upload(): if request.method == 'POST': f = reque
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 %}
{%extends“layout.html”%}
{%block content%}
{%endblock%}
错误消息是:
'错误:执行中止'
这个感叹号也出现在“graph.add”行旁边,这是一个空格错误
有人知道如何才能达到预期的效果吗?谢谢!
我刚刚解决了这个问题,实际上,这是因为对请求方法缺乏理解。
简单地说,在“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)
x=np.asarray(f)
似乎很奇怪asarray
不接受文件。也许您应该查看Python中的genfromtxt
或甚至内置的csv
模块,以将.csv
数据转换为Python列表/etc。感叹号还警告您需要在逗号后留一个空格——这只是一个使代码更具可读性的标准。