如何有一个基于与Flask上传的文件的输出?



我想有一个Flask应用程序谁让用户放一个csv文件,它返回一个输出像head()或另一个(目前)

这是我的app.py:

from flask import Flask, render_template, request
from werkzeug.utils import secure_filename
import os
import pandas as pd
ALLOWED_EXTENSIONS = {'csv', 'txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'}
UPLOAD_FOLDER = 'uploads/'

app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
def allowed_file(filename):
return '.' in filename and filename.rsplit('.',1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/', methods=['GET','POST'])
def upload_file(filename=None,column=None, data=None):
if request.method == 'POST':
if 'file' not in request.files:
flash("No file part")
return redirect(request.url)
file = request.files['file']
if file.filename == '':
flash("No selected file")
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(file_path)
df = pd.read_csv(file_path, delimiter = ';')
column = list(df)
data = [list(df[d]) for d in column]
return render_template('index.html', filename=filename, data=data, column=column)

if __name__ == '__main__':
app.run(debug=True)

这是我的index.html文件:

<!doctype <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Upload File</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" media="screen" href="main.css" />
<script src="main.js"></script>
</head>
<body>
{% if data %}
<p>Data Found</p>
{% endif %}
<h1>Upload new File</h1>
<form method=post enctype=multipart/form-data>
<input type=file name=file>
<input type=submit value=Upload>
</form>
</body>
</html>

下面是我导入csv文件时的样子: 输入

输出我的问题是:而不是有"数据找到!",我怎么能像一个输出基于csv文件刚刚导入,如head(),或data['variable'].value_counts()为例。

一旦文件上传,就可以用HTML呈现Pandas表。

瓶代码:

from flask import Flask, render_template, request
from werkzeug.utils import secure_filename
import os
import pandas as pd
ALLOWED_EXTENSIONS = {'csv', 'txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'}
UPLOAD_FOLDER = 'uploads/'

app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
def allowed_file(filename):
return '.' in filename and filename.rsplit('.',1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/', methods=['GET','POST'])
def upload_file(filename=None,column=None, data=None):
if request.method == 'GET':
render_template('index.html')
if request.method == 'POST':
if 'file' not in request.files:
flash("No file part")
return redirect(request.url)
file = request.files['file']
if file.filename == '':
flash("No selected file")
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(file_path)
df = pd.read_csv(file_path, delimiter = ';')
column = list(df)
data = [list(df[d]) for d in column]
df_head = df.head()
return render_template('index.html', data=data, tables=[df_head.to_html(classes='data')], titles=df.columns.values)
return render_template('index.html')

if __name__ == '__main__':
app.run(debug=True)
HTML代码:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Upload File</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" media="screen" href="main.css" />
<script src="main.js"></script>
</head>
<body>
{% if data %}
{% for table in tables %}
{{titles[loop.index]}}
{{ table|safe }}
{% endfor %}
{% endif %}
<h1>Upload new File</h1>
<form method=post enctype=multipart/form-data>
<input type=file name=file>
<input type=submit value=Upload>
</form>
</body>
</html>

相关内容

  • 没有找到相关文章