【发布时间】:2017-06-25 09:38:36
【问题描述】:
我使用Dropzone.js 允许通过Flask 网站拖放上传CSV 文件。上传过程效果很好。我将上传的文件保存到我指定的文件夹,然后可以使用df.to_html() 将dataframe 转换为HTML 代码,然后将其传递给我的模板。它在代码中到达了这一点,但它不呈现模板并且不会引发任何错误。所以我的问题是为什么Dropzone.js 会阻止渲染发生?
我也试过只从表中返回HTML 代码而不使用render_template,但这也不起作用。
初始化.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的内容是什么? -
实际上基本上只是通过
render_template传递的表格的html 代码。我已经添加了问题。
标签: javascript python python-2.7 flask dropzone.js