在html页面怎么写循环输出,如何将循环输出(列表)传递到html页面(示例代码)...

在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

links = []

for link in topology['link']: #2nd loop to get list of links

nodeslinks_topo = {'nodes': nodes, 'links': links}

final_topo = json.dumps(nodeslinks_topo)

nodeslinks_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)

HTML页面

Topology

Nodes Links

{% for i in mytopo %}

{{ i["id"] }}

{{ i["source"] }}

{{ i["target"] }}

{% endfor %}

节点和链接的jsonfile内容列表{

"nodes": [

{

"id": "openflow:1",

"tpid": [

"openflow:1:2",

"openflow:1:1",

"openflow:1:LOCAL"

]

},

{

"ip": "10.0.0.1",

"mac": "00:00:00:00:00:01",

"id": "host:00:00:00:00:00:01",

"tpid": [

"host:00:00:00:00:00:01"

]

},

{

"id": "openflow:2",

"tpid": [

"openflow:2:LOCAL",

"openflow:2:1",

"openflow:2:2"

]

},

{

"ip": "10.0.0.2",

"mac": "00:00:00:00:00:02",

"id": "host:00:00:00:00:00:02",

"tpid": [

"host:00:00:00:00:00:02"

]

}

],

"links": [

{

"source": "host:00:00:00:00:00:01",

"id": "host:00:00:00:00:00:01/openflow:1:1",

"target": "openflow:1:1"

},

{

"source": "openflow:2:1",

"id": "openflow:2:1/host:00:00:00:00:00:02",

"target": "host:00:00:00:00:00:02"

},

{

"source": "openflow:1:2",

"id": "openflow:1:2",

"target": "openflow:2:2"

},

{

"source": "openflow:2:2",

"id": "openflow:2:2",

"target": "openflow:1:2"

},

{

"source": "openflow:1:1",

"id": "openflow:1:1/host:00:00:00:00:00:01",

"target": "host:00:00:00:00:00:01"

},

{

"source": "host:00:00:00:00:00:02",

"id": "host:00:00:00:00:00:02/openflow:2:1",

"target": "openflow:2:1"

}

]

}

我正在查看页面会显示类似这样的内容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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值