如何将for循环输出(list)传递到htmlpag

2024-05-15 02:38:16 发布

您现在位置:Python中文网/ 问答频道 /正文

下面是专家的帮助,我能够将单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)

HTML页

<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节点和链接的内容列表

{
  "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

Tags: oftoidhostsourcetarget列表for
1条回答
网友
1楼 · 发布于 2024-05-15 02:38:16

你的问题在你的循环中。你试图从列表中提取一个项目

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

如果您的json对象看起来像这样,您可以使用它

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

相关问题 更多 >

    热门问题