python - 如何根据上传的带有 Flask 包的 csv 文件进行输出?
问题描述
我的目标是拥有一个 Flask 应用程序,让用户放置一个 csv 文件并且该应用程序返回一个特定的输出。
这是我想要的输出,让我向您展示我是如何在 jupyter notebook 中完成的:
for i in df['variable1'].unique():
data = df[(df['variable1'] == i)]
data['list'] = data['comms_clean'].apply(lambda x: str(x).split())
top = Counter([item for sublist in data['list'] for item in sublist])
mostcommon = pd.DataFrame(top.most_common(3))
mostcommon.columns = ['Common Word', 'Count']
print()
print('For the classe "{0}" who appears {1} time, here is the 3 most frequent word: '.format(i, data.shape[0]))
print()
print(mostcommon.head(3))
print()
结果如下:
For the classe "classe1" who appears 389 time, here is the 3 most frequent word:
Common Word Count
0 word10 267
1 word20 149
2 word30 46
For the classe "classe1" who appears 657 time, here is the 3 most frequent word:
Common Word Count
0 word40 234
1 word50 89
2 word60 34
For the classe "classe3" who appears 250 time, here is the 3 most frequent word:
Common Word Count
0 word70 90
1 word80 30
2 word90 19
...
但我无法弄清楚如何将其转换为 Flask。
这是我的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 == '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)
这是我的index.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>
但是这个应用程序给了我head()
上传的 csv 文件,我想要的具体输出很难实现,所以如果你能帮助我,那就太棒了。
我想我需要用我最常用的词创建一个数据框,因为 Flask 似乎需要一个数据框,但我并不完全确定。
有什么建议么 ?