如何使用python flask从pandas数据框中获取下拉列表项?
我需要使用python将pandas dataframe中的数据添加到html文档的下拉列表中如何使用python flask从pandas数据框中获取下拉列表项?,python,pandas,flask,dropdown,Python,Pandas,Flask,Dropdown,我需要使用python将pandas dataframe中的数据添加到html文档的下拉列表中 @app.route('/api/v1/resources/getservices', methods=['GET']) def api_services(): return render_template('view.html',table=df.to_html()) <!DOCTYPE html> <html lang="en"> <head>
@app.route('/api/v1/resources/getservices', methods=['GET'])
def api_services():
return render_template('view.html',table=df.to_html())
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Dropdown</title>
<h1>Services</h1>
</head>
<body>
<select name="table" method="GET" action="/">
<option value="{{table[0]}}" selected>{{table[0]}}</option>
{% for colour in table[1:] %}
<option value="{{colour}}">{{colour}}</option>
{% endfor %}
</select>
</body>
</html>
@app.route('/api/v1/resources/getservices',methods=['GET'])
def api_服务():
返回render_模板('view.html',table=df.to_html())
下拉列表
服务
{{表[0]}
{表[1:]%中颜色的百分比}
{{color}}
{%endfor%}
我所期望的是,熊猫数据框架df中“服务”列的数据应该作为项目添加到html文件的下拉列表中。。但每当我尝试使用上述代码时,都会创建一个没有任何项目的下拉列表…当您调用
to_html()
方法时,它将创建html表,您无法对其进行迭代。我不知道你的df数据是什么,但我认为这可能适合你
app.py
@app.route('/api/v1/resources/getservices', methods=['GET'])
def api_services():
d = {'Services': ["red", "green", "blue"]}
df = pd.DataFrame(data=d)
return render_template('view.html', table=df)
view.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Dropdown</title>
<h1>Services</h1>
</head>
<body>
<select name="table" method="GET" action="/">
{% for colour in table["Services"] %}
<option value="{{ colour }}">{{ colour }}</option>
{% endfor %}
</select>
</body>
</html>
下拉列表
服务
{%表示表[“服务”]%中的颜色}
{{color}}
{%endfor%}
前端的调试是什么(例如,Chrome中的F12)?python flask和html