首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将循环输出(列表)传递给html页面

如何将循环输出(列表)传递给html页面
EN

Stack Overflow用户
提问于 2019-10-10 10:43:07
回答 1查看 200关注 0票数 0

在专家的帮助下,我可以将单个for循环数据(一个列表)传递给html页面。现在我有2个for循环(节点列表和链接列表),每个循环都有自己的列表。然后将节点和链接合并到单个列表中。因此,我如何才能将哪些内容节点和链接数据的列表发送到html page...please建议。

这是代码

代码语言:javascript
复制
## 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)

HTML页面

代码语言:javascript
复制
<html>
<body>
    <h1>Topology</h1>
    <table id="yourTableID" width="100%" cellspacing="5">
        <thead>
            <tr>
                <th> Nodes </th>
                <th> Links </th>  
            </tr>
        </thead>
        <tbody>
            {% for i in mytopo %}
                <tr>
                   <td>
                      {{ i["id"] }}
                   </td>
                   <td>
                     {{ i["source"] }}
                   </td>
                   <td>
                     {{ i["target"] }}
                   </td>
                </tr>
            {% endfor %}
        </tbody>                 
    </table>
</body>
</html>

jsonfile节点和链接的文件内容列表

代码语言:javascript
复制
{
  "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"
    }
  ]
}

我正在寻找页面会呈现这样的东西

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2019-10-10 11:14:42

您的问题在for循环中。您正在尝试从列表中提取一个项目

代码语言:javascript
复制
{% for i in mytopo %}
    <tr>
     <td>{{ i["id"] }}</td>
     <td>{{ i["source"] }}</td>
     <td>{{ i["target"] }}</td>
    </tr>
{% endfor %}

如果您的json对象如下所示,则可以使用它

代码语言:javascript
复制
[{'id': 'fizz', 'source': 'buzz', 'target': 'fizzbuzz'},
 {'id': 'batman', 'source': 'testing', 'target': 'hello'}]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58314718

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档