Python 将请求rest api数据传递到flask html

Python 将请求rest api数据传递到flask html,python,html,rest,api,flask,Python,Html,Rest,Api,Flask,我刚开始玩flask和html 我有一个python文件,它向url发出请求并返回json格式的输出。输出打印在终端上。现在,我想在html页面中显示输出。我只听了几篇教程,但根据我自己的数据,很难理解和采用 不带任何flask/html的Python文件 def topo(): response = requests.get(network_url, auth = (user, pass)) print(response) for nodes in response.j

我刚开始玩flask和html

我有一个python文件,它向url发出请求并返回json格式的输出。输出打印在终端上。现在,我想在html页面中显示输出。我只听了几篇教程,但根据我自己的数据,很难理解和采用

不带任何flask/html的Python文件

def topo():
    response = requests.get(network_url, auth = (user, pass))
    print(response)
    for nodes in response.json()['network-topology']['topology']:
        node_info = nodes['node']
        for node in node_info:
            try:
                ip_address = node['host-tracker-service:addresses'][0]['ip']
                mac_address = node['host-tracker-service:addresses'][0]['mac']
                print 'MAC address %s IP address %s' % (mac_address, ip_address)
            except:
                pass

上述代码返回终端输出如下

MAC address 00:00:00:00:00:01 IP address 10.0.0.1
MAC address 00:00:00:00:00:02 IP address 10.0.0.2
下面是RESTAPI请求的响应

{u'network-topology': {u'topology': [{u'node': [{u'opendaylight-topology-inventory:inventory-node-ref': u"/opendaylight-inventory:nodes/opendaylight-inventory:node[opendaylight-inventory:id='openflow:1']", u'node-id': u'openflow:1', u'termination-point': [{u'opendaylight-topology-inventory:inventory-node-connector-ref': u"/opendaylight-inventory:nodes/opendaylight-inventory:node[opendaylight-inventory:id='openflow:1']/opendaylight-inventory:node-connector[opendaylight-inventory:id='openflow:1:2']", u'tp-id': u'openflow:1:2'}, {u'opendaylight-topology-inventory:inventory-node-connector-ref': u"/opendaylight-inventory:nodes/opendaylight-inventory:node[opendaylight-inventory:id='openflow:1']/opendaylight-inventory:node-connector[opendaylight-inventory:id='openflow:1:1']", u'tp-id': u'openflow:1:1'}, {u'opendaylight-topology-inventory:inventory-node-connector-ref': u"/opendaylight-inventory:nodes/opendaylight-inventory:node[opendaylight-inventory:id='openflow:1']/opendaylight-inventory:node-connector[opendaylight-inventory:id='openflow:1:LOCAL']", u'tp-id': u'openflow:1:LOCAL'}]}, {u'host-tracker-service:addresses': [{u'first-seen': 1568245219561, u'ip': u'10.0.0.1', u'mac': u'00:00:00:00:00:01', u'id': 26, u'last-seen': 1568245219561}], u'host-tracker-service:id': u'00:00:00:00:00:01', u'host-tracker-service:attachment-points': [{u'active': True, u'corresponding-tp': u'host:00:00:00:00:00:01', u'tp-id': u'openflow:1:1'}], u'node-id': u'host:00:00:00:00:00:01', u'termination-point': [{u'tp-id': u'host:00:00:00:00:00:01'}]}, {u'opendaylight-topology-inventory:inventory-node-ref': u"/opendaylight-inventory:nodes/opendaylight-inventory:node[opendaylight-inventory:id='openflow:2']", u'node-id': u'openflow:2', u'termination-point': [{u'opendaylight-topology-inventory:inventory-node-connector-ref': u"/opendaylight-inventory:nodes/opendaylight-inventory:node[opendaylight-inventory:id='openflow:2']/opendaylight-inventory:node-connector[opendaylight-inventory:id='openflow:2:LOCAL']", u'tp-id': u'openflow:2:LOCAL'}, {u'opendaylight-topology-inventory:inventory-node-connector-ref': u"/opendaylight-inventory:nodes/opendaylight-inventory:node[opendaylight-inventory:id='openflow:2']/opendaylight-inventory:node-connector[opendaylight-inventory:id='openflow:2:1']", u'tp-id': u'openflow:2:1'}, {u'opendaylight-topology-inventory:inventory-node-connector-ref': u"/opendaylight-inventory:nodes/opendaylight-inventory:node[opendaylight-inventory:id='openflow:2']/opendaylight-inventory:node-connector[opendaylight-inventory:id='openflow:2:2']", u'tp-id': u'openflow:2:2'}]}, {u'host-tracker-service:addresses': [{u'first-seen': 1568245219558, u'ip': u'10.0.0.2', u'mac': u'00:00:00:00:00:02', u'id': 25, u'last-seen': 1568245219558}], u'host-tracker-service:id': u'00:00:00:00:00:02', u'host-tracker-service:attachment-points': [{u'active': True, u'corresponding-tp': u'host:00:00:00:00:00:02', u'tp-id': u'openflow:2:1'}], u'node-id': u'host:00:00:00:00:00:02', u'termination-point': [{u'tp-id': u'host:00:00:00:00:00:02'}]}], u'link': [{u'link-id': u'host:00:00:00:00:00:01/openflow:1:1', u'destination': {u'dest-node': u'openflow:1', u'dest-tp': u'openflow:1:1'}, u'source': {u'source-tp': u'host:00:00:00:00:00:01', u'source-node': u'host:00:00:00:00:00:01'}}, {u'link-id': u'openflow:2:1/host:00:00:00:00:00:02', u'destination': {u'dest-node': u'host:00:00:00:00:00:02', u'dest-tp': u'host:00:00:00:00:00:02'}, u'source': {u'source-tp': u'openflow:2:1', u'source-node': u'openflow:2'}}, {u'link-id': u'openflow:1:2', u'destination': {u'dest-node': u'openflow:2', u'dest-tp': u'openflow:2:2'}, u'source': {u'source-tp': u'openflow:1:2', u'source-node': u'openflow:1'}}, {u'link-id': u'openflow:2:2', u'destination': {u'dest-node': u'openflow:1', u'dest-tp': u'openflow:1:2'}, u'source': {u'source-tp': u'openflow:2:2', u'source-node': u'openflow:2'}}, {u'link-id': u'openflow:1:1/host:00:00:00:00:00:01', u'destination': {u'dest-node': u'host:00:00:00:00:00:01', u'dest-tp': u'host:00:00:00:00:00:01'}, u'source': {u'source-tp': u'openflow:1:1', u'source-node': u'openflow:1'}}, {u'link-id': u'host:00:00:00:00:00:02/openflow:2:1', u'destination': {u'dest-node': u'openflow:2', u'dest-tp': u'openflow:2:1'}, u'source': {u'source-tp': u'host:00:00:00:00:00:02', u'source-node': u'host:00:00:00:00:00:02'}}], u'topology-id': u'flow:1'}]}}

这是我在flask和html页面中添加路由的时候。也许我错了。请在此协助

@app.route('/')
def mypage():
   def topo():
    response = requests.get(network_url, auth = (user, pass))
    print(response)
    for nodes in response.json()['network-topology']['topology']:
          <snippet>


        return render_template('myweb.html', topo=json.loads(response.text)['network-topology'])

if __name__ == '__main__':
  app.run(host='0.0.0.0', debug=True)

我想有到html页面的输出。只是不清楚如何使用我自己的json数据。请告诉我做这件事的正确方法。感谢您的支持和帮助。

以下是一种可能的方法:

PYTHON方面

@app.route('/')
def mypage():
    def topo():
        response = requests.get(network_url, auth = (user, pass))
        print(response)
        list_of_mac_ip_address = []

        for nodes in response.json()['network-topology']['topology']:
            node_info = nodes['node']
            for node in node_info:
                dico = dict()
                try:
                    ip_address = node['host-tracker-service:addresses'][0]['ip']
                    mac_address = node['host-tracker-service:addresses'][0]['mac']
                    print 'MAC address %s IP address %s' % (mac_address, ip_address)

                    dico['IP'] = ip_address
                    dico['MAC'] = mac_address
                    list_of_mac_ip_address.append(dico)

                except:
                    pass

        return list_of_mac_ip_address

    list_of_mac_ip_address = topo()

    return render_template('myweb.html', list_ip_mac=list_of_mac_ip_address)

if __name__ == '__main__':
    app.run(host='0.0.0.0', debug=True)
<html>
<body>
    <h1>Topology</h1>
    <table id="yourTableID" width="100%" cellspacing="5">
        <thead>
            <tr>
                <th> MAC Address </th>
                <th> IP Address </th>   
            </tr>
        </thead>
        <tbody>
            {% for i in list_ip_mac %}
                <tr>
                    <td>
                        {{ i["IP"] }}
                    </td>
                    <td>
                        {{ i["MAC"] }}
                    </td>
                </tr>
            {% endfor %}
        </tbody>                 
    </table>
</body>
</html>
myweb.html

@app.route('/')
def mypage():
    def topo():
        response = requests.get(network_url, auth = (user, pass))
        print(response)
        list_of_mac_ip_address = []

        for nodes in response.json()['network-topology']['topology']:
            node_info = nodes['node']
            for node in node_info:
                dico = dict()
                try:
                    ip_address = node['host-tracker-service:addresses'][0]['ip']
                    mac_address = node['host-tracker-service:addresses'][0]['mac']
                    print 'MAC address %s IP address %s' % (mac_address, ip_address)

                    dico['IP'] = ip_address
                    dico['MAC'] = mac_address
                    list_of_mac_ip_address.append(dico)

                except:
                    pass

        return list_of_mac_ip_address

    list_of_mac_ip_address = topo()

    return render_template('myweb.html', list_ip_mac=list_of_mac_ip_address)

if __name__ == '__main__':
    app.run(host='0.0.0.0', debug=True)
<html>
<body>
    <h1>Topology</h1>
    <table id="yourTableID" width="100%" cellspacing="5">
        <thead>
            <tr>
                <th> MAC Address </th>
                <th> IP Address </th>   
            </tr>
        </thead>
        <tbody>
            {% for i in list_ip_mac %}
                <tr>
                    <td>
                        {{ i["IP"] }}
                    </td>
                    <td>
                        {{ i["MAC"] }}
                    </td>
                </tr>
            {% endfor %}
        </tbody>                 
    </table>
</body>
</html>

拓扑学
MAC地址
IP地址
{列表中i的%u ip\u mac%}
{{i[“IP”]}
{{i[“MAC”]}
{%endfor%}

如您所见,我创建了一个在HTML端发送的词典列表。使用jinja,我反复浏览这个列表,以检索不同的IP和MAC地址。

您的代码太多了,以至于无法确切知道问题出在哪里。它是在python端还是HTML端?您目前在python方面得到了什么结果?您希望在页面上显示什么内容?请提供更多细节。你放了一个循环,但你永远看不到循环中发生了什么。你在那里做什么治疗?他必须产生什么样的结果?嗨,托宾,谢谢你的回答。我已经更新了细节。请进一步建议。谢谢…先生…html页面可以浏览…但它只有标题(h1)和标题信息(th)…页面上没有显示mac地址和ip地址的数据。从python文件中,它确实显示-->MAC地址00:00:00:00:00:01 IP地址10.0.0.1 MAC地址00:00:00:00:00:02 IP地址10.0.0.2 10.44.45.10---[03/Oct/2019 05:46:47]“GET/HTTP/1.1”200-只是数据没有填充到html页面转到第行
列出\u MAC\u IP\u地址=topo()
。试着把它打印出来。你得到了什么?然后检查变量的名称…它返回如下的零列表---->[]要检查哪个变量?无论如何。。。除了在这里创建列表并在html页面上迭代之外,还有其他方法吗?我们是否必须将其简化为干净的列表(已经提取了特定的键),并且html仅迭代列表…感谢您在每个级别添加打印,尤其是在try语句中。检查dico和列表中的内容,并确保topo函数返回一些内容。