Python 如何将for循环输出(列表)传递到html页面

Python 如何将for循环输出(列表)传递到html页面,python,json,flask,Python,Json,Flask,下面是来自SO专家的帮助,我能够将单for循环数据(列表)传递到html页面。现在我有2个for循环(节点列表和链接列表),每个循环都有自己的列表。然后将节点和链接合并到单个列表中。因此,我如何才能发送的内容节点和链接到html页面的数据列表…请建议 这是密码 ## 2 for loop to extract list of nodes and links @app.route('/') def mypage(): def topo(): resData = json.

下面是来自SO专家的帮助,我能够将单for循环数据(列表)传递到html页面。现在我有2个for循环(节点列表和链接列表),每个循环都有自己的列表。然后将节点和链接合并到单个列表中。因此,我如何才能发送的内容节点和链接到html页面的数据列表…请建议

这是密码

## 2 for loop to extract list of nodes and links
@app.route('/')  
def mypage():
   def topo():
        resData = json.load(open('odl_topo.txt'))
        topology = resData['network-topology']['topology'][0]

        nodes = []
        for node in topology['node']: #1st loop to get list of nodes
           <some code here>
        return nodes

        links = []
        for link in topology['link']: #2nd loop to get list of links
           <some code here>
        return links

        nodeslinks_topo = {'nodes': nodes, 'links': links}
        final_topo = json.dumps(nodeslinks_topo)
   final_topo = topo()
   #nodes = topo()
   #links = topo()

   ## Able to send to html page either nodes or links but not both nodes and 
   ## links data

   ##this work only if send to html list of nodes data
   #return render_template('myweb.html', mytopo=nodes)

   ##this work only if send to html list of links data
   #return render_template('myweb.html', mytopo=links)

   ##I want to send list of nodes and links together to html and this doesn't 
   work
   return render_template('myweb.html', mytopo=nodeslinks_topo)

#Flask web server
if __name__ == "__main__":
   app.run(host='0.0.0.0',debug = True)
我在看网页会呈现这样的东西


Topology

Nodes          Link
openflow:1     host:00:00:00:00:00:01 - openflow:1:1
10.0.0.1       openflow:2:1 - host:00:00:00:00:00:02
openflow:2     openflow:1:2 - openflow:1:2
10.0.0.2       openflow:2:2 - openflow:1:2
               openflow:1:1 - host:00:00:00:00:00:01
               host:00:00:00:00:00:02 - openflow:2:1

Really appreciate for your help and support on this matter.

Thank you
[{'id': 'fizz', 'source': 'buzz', 'target': 'fizzbuzz'},
 {'id': 'batman', 'source': 'testing', 'target': 'hello'}]

您的问题在for循环中。您试图从列表中提取项目

{% for i in mytopo %}
    <tr>
     <td>{{ i["id"] }}</td>
     <td>{{ i["source"] }}</td>
     <td>{{ i["target"] }}</td>
    </tr>
{% endfor %}

你好我的json字符串看起来和你提到的一样。它包含节点的内容列表和链接列表。从python文件…我不知道如何将列表传递到html(内容节点和链接)。。。。我已经分享了细节。ThanksyYour json对象定义为两个键“节点”和“链接”,其中包含其他嵌入式字典对象的列表。我建议您将创建字典拓扑的方式重写为类似于我上面提供的字典列表的方式。我们是否可以将2键dict传递到html…而不重写代码…如果您不这样更改dictionary对象,则必须更改在for循环中调用结果的方式:I['nodes'][0]['tpid'][0]。是否在此处看到问题?必须更改代码。
{% for i in mytopo %}
    <tr>
     <td>{{ i["id"] }}</td>
     <td>{{ i["source"] }}</td>
     <td>{{ i["target"] }}</td>
    </tr>
{% endfor %}
[{'id': 'fizz', 'source': 'buzz', 'target': 'fizzbuzz'},
 {'id': 'batman', 'source': 'testing', 'target': 'hello'}]