我正在使用Dropzone.js
实现通过Flask
网站进行拖放上传CSV
文件的功能。上传过程很顺利,我将上传的文件保存到指定的文件夹中,然后可以使用df.to_html()
将dataframe
转换为HTML
代码,并将其传递给我的模板。它能够执行到这个代码段,但不会渲染模板,也不会抛出任何错误。所以我的问题是,为什么Dropzone.js
会阻止渲染?
我还尝试过直接返回表格的HTML
代码而不使用render_template
,但这也没有起作用。
init.py
import os
from flask import Flask, render_template, request
import pandas as pd
app = Flask(__name__)
# get the current folder
APP_ROOT = os.path.dirname(os.path.abspath(__file__))
@app.route('/')
def index():
return render_template('upload1.html')
@app.route('/upload', methods=['POST'])
def upload():
# set the target save path
target = os.path.join(APP_ROOT, 'uploads/')
# loop over files since we allow multiple files
for file in request.files.getlist("file"):
# get the filename
filename = file.filename
# combine filename and path
destination = "/".join([target, filename])
# save the file
file.save(destination)
#upload the file
df = pd.read_csv(destination)
table += df.to_html()
return render_template('complete.html', table=table)
if __name__ == '__main__':
app.run(port=4555, debug=True)
上传1文件.html
<!DOCTYPE html>
<meta charset="utf-8">
<script src="https://rawgit.com/enyo/dropzone/master/dist/dropzone.js"></script>
<link rel="stylesheet" href="https://rawgit.com/enyo/dropzone/master/dist/dropzone.css">
<table width="500">
<tr>
<td>
<form action="{{ url_for('upload') }}", method="POST" class="dropzone"></form>
</td>
</tr>
</table>
编辑
这是我正在上传的csv
数据示例:
Person,Count
A,10
B,12
C,13
完成.html
<html>
<body>
{{table | safe }}
</body>
</html>
complete.html
的内容是什么? - HassenPyrender_template
传递的表格的 HTML 代码。我已经将其添加到问题中了。 - user2242044