我需要使用python flask将pandas数据帧中的数据添加到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>
<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>
我期望的是,应该将pandas数据框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>
将python scikit学习模型导出到pmml - python我想将python scikit-learn模型导出到PMML。哪个python软件包最合适?我阅读了有关Augustus的内容,但是我无法使用scikit-learn模型找到任何示例。 python大神给出的解决方案 SkLearn2PMML是 JPMML-SkLearn命令行应用程序周围的薄包装。有关受支持的Scikit-Learn Estimator和…
Python numpy数据指针地址无需更改即可更改 - python编辑经过一些摆弄之后,到目前为止,我已经隔离了以下状态:一维数组在直接输入变量时提供两个不同的地址,而在使用print()时仅提供一个地址2D数组(或矩阵)在直接输入变量时提供三个不同的地址,在使用print()时提供两个地址3D数组在直接输入变量时提供两个不同的地址,而在使用print()时仅给出一个(显然与一维数组相同)像这样:>>> …
Python pytz时区函数返回的时区为9分钟 - python由于某些原因,我无法从以下代码中找出原因:>>> from pytz import timezone >>> timezone('America/Chicago') 我得到:<DstTzInfo 'America/Chicago' LMT-1 day, 18:09:00 STD…
用大写字母拆分字符串,但忽略AAA Python Regex - python我的正则表达式:vendor = "MyNameIsJoe. I'mWorkerInAAAinc." ven = re.split(r'(?<=[a-z])[A-Z]|[A-Z](?=[a-z])', vendor) 以大写字母分割字符串,例如:'我的名字是乔。 I'mWorkerInAAAinc”变成…
Python sqlite3数据库已锁定 - python我在Windows上使用Python 3和sqlite3。我正在开发一个使用数据库存储联系人的小型应用程序。我注意到,如果应用程序被强制关闭(通过错误或通过任务管理器结束),则会收到sqlite3错误(sqlite3.OperationalError:数据库已锁定)。我想这是因为在应用程序关闭之前,我没有正确关闭数据库连接。我已经试过了: connectio…