首页 > 解决方案 > 通过 python 列表循环并在烧瓶中显示结果

问题描述

python函数返回一个python列表

python模块函数

with open(xml_append_back) as fd1:
    doc = xmltodict.parse(fd1.read())
    codes = []

    for p in doc['Des']['Config']:
            codes.append(p['@Id'])
            codes.append(pl['@name'])


print(codes)
return codes

codes = ['f2ee4681', 'Conf. no: 1', '89282c5b', 'Conf. no: 2', '3e9dd219', 'Conf. no: 3', '773044b9'] # returned from python  to flask template result.html

我在我的模板/flask.html 中调用这个变量,就像这个烧瓶文件一样

@app.route('/result',methods = ['POST', 'GET'])
def result():

const_ids=run_d.run_de_selected_configs() # this function returns "codes" 

return render_template("result.html",result = 
constraint_names_from_form,result1=constraint_ids)

结果.html 文件

{% for key,key1  in result1 %}
<tr class="even"><td>{{ key }}</td><td>{{ key1 }}</td></tr> 

应该

<tr class="even"><td>f2ee4681</td><td>Conf. no: 1</td></tr>
{% endfor %}

我究竟做错了什么

标签: pythonjinja2

解决方案


为了回答我自己的问题,我在 python 代码中使用了 zip 实用程序,因为 zip 在烧瓶中不可用

function returncodes()
-------
--------- 

return zip(codes,codeNames) # in my case

烧瓶模板没有变化

@app.route('/result',methods = ['POST', 'GET'])
def result():

const_ids=run_d.run_de_selected_configs() # this function returns "codes" 

return render_template("result.html",result = 
constraint_names_from_form,result1=constraint_ids)

现在在我的 result.html 中

{% for keys,keys2 in result1 %}
<tr class="even"><td>{{keys}}</td><td>{{keys2}}</td></tr>

 {% endfor %}

推荐阅读