英文:
How to dynamically populate dropdown menus with column names from csv in Flask or Pandas?
问题
我正在构建一个Flask应用程序,允许用户上传CSV文件并使用下拉菜单选择文件中的三列。我希望动态填充下拉菜单,使用上传文件的列名。最终目标是将列名分配给变量col1、col2、col3,以便我可以在Python脚本中引用它们。
我正在尝试让用户映射列,以便我知道要用Python对数据进行怎样的操作。对Flask还很生疏,但感觉我已经很接近了!
**我尝试过的方法:**
我尝试修改上传功能,从上传的文件中读取列名,并将它们传递到upload.html模板。但是我仍然遇到下拉菜单未填充列名的问题。
以下是我的代码:
**app.py**
from flask import Flask, render_template, request
import pandas as pd
app = Flask(__name__)
@app.route('/')
def home():
return render_template('home.html')
@app.route('/upload', methods=['GET', 'POST'])
def upload():
if request.method == 'POST':
file = request.files.get('file')
if file and file.filename.endswith('.csv'):
df = pd.read_csv(file)
col_names = df.columns.tolist()
return render_template('upload.html', col_names=col_names)
else:
return "Invalid file format"
else:
return render_template('upload.html')
if __name__ == '__main__':
app.run(debug=True)
**upload.html**
<form action="/upload" method="POST" enctype="multipart/form-data">
<label for="file">File:</label>
<input type="file" id="file" name="file"><br />
<label for="col1">Column 1:</label>
<select id="col1" name="col1">
{% for col in col_names %}
<option value="{{ col }}">{{ col }}</option>
{% endfor %}
</select><br />
<label for="col2">Column 2:</label>
<select id="col2" name="col2">
{% for col in col_names %}
<option value="{{ col }}">{{ col }}</option>
{% endfor %}
</select><br />
<label for="col3">Column 3:</label>
<select id="col3" name="col3">
{% for col in col_names %}
<option value="{{ col }}">{{ col }}</option>
{% endfor %}
</select><br />
<button>Upload</button>
</form>
英文:
I'm building a Flask app that allows users to upload a CSV file and select three columns from the file using dropdown menus. I want to dynamically populate the dropdown menus with the column names from the uploaded file. The end goal is to assign the column names to variables col1, col2, col3 so that I can reference them in a python script.
I'm trying to get the user to map columns so I know what the data is to manipulate it with Python. New to Flask, but feel like I'm close!
What I've tried:
I've tried modifying the upload function to read the column names from the uploaded file and pass them to the upload.html template. I'm still having issues with the dropdown menus not being populated with the column names.
Here's my code:
app.py
from flask import Flask, render_template, request
import pandas as pd
app = Flask(__name__)
@app.route('/')
def home():
return render_template('home.html')
@app.route('/upload', methods=['GET', 'POST'])
def upload():
if request.method == 'POST':
file = request.files.get('file')
if file and file.filename.endswith('.csv'):
df = pd.read_csv(file)
col_names = df.columns.tolist()
return render_template('upload.html', col_names=col_names)
else:
return "Invalid file format"
else:
return render_template('upload.html')
if __name__ == '__main__':
app.run(debug=True)
upload.html
<form action="/upload" method="POST" enctype="multipart/form-data">
<label for="file">File:</label>
<input type="file" id="file" name="file"><br />
<label for="col1">Column 1:</label>
<select id="col1" name="col1">
{% for col in col_names %}
<option value="{{ col }}">{{ col }}</option>
{% endfor %}
</select><br />
<label for="col2">Column 2:</label>
<select id="col2" name="col2">
{% for col in col_names %}
<option value="{{ col }}">{{ col }}</option>
{% endfor %}
</select><br />
<label for="col3">Column 3:</label>
<select id="col3" name="col3">
{% for col in col_names %}
<option value="{{ col }}">{{ col }}</option>
{% endfor %}
</select><br />
<button>Upload</button>
</form>
答案1
得分: 1
在upload.html中:
<form action="/upload?col_names={{ col_names }}" method="POST" enctype="multipart/form-data">
在app.py中:
@app.route('/upload', methods=['GET', 'POST'])
def upload():
col_names = request.args.get('col_names')
if request.method == 'POST':
file = request.files.get('file')
if file and file.filename.endswith('.csv'):
df = pd.read_csv(file)
col1 = request.form.get('col1')
col2 = request.form.get('col2')
col3 = request.form.get('col3')
return render_template('upload.html', col_names=col_names, col1=col1, col2=col2, col3=col3)
else:
return "Invalid file format"
else:
return render_template('upload.html', col_names=col_names)
这样,您可以在upload()
函数中访问col1
、col2
和col3
的值,并将它们用于使用Python操作CSV文件中的数据。
英文:
Add the column names to the form action in the upload.html template and then retrieve the values with the request.form.get() method in the upload() function in app.py.
In upload.html:
<form action="/upload?col_names={{ col_names }}" method="POST" enctype="multipart/form-data">
In app.py:
@app.route('/upload', methods=['GET', 'POST'])
def upload():
col_names = request.args.get('col_names')
if request.method == 'POST':
file = request.files.get('file')
if file and file.filename.endswith('.csv'):
df = pd.read_csv(file)
col1 = request.form.get('col1')
col2 = request.form.get('col2')
col3 = request.form.get('col3')
return render_template('upload.html', col_names=col_names, col1=col1, col2=col2, col3=col3)
else:
return "Invalid file format"
else:
return render_template('upload.html', col_names=col_names)
With that you can access the values of col1, col2, and col3 in the upload() function and use them to manipulate the data in the CSV file with Python.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论