停止烧瓶复制加载的变量 [英] stop flask duplicating a loaded variable

查看:65
本文介绍了停止烧瓶复制加载的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个基本的云基础架构管理站点,并且列出虚拟机的页面有问题.

I'm building a basic cloud infrastructure management site and have a problem with the page that lists virtual machines.

flask应用会提取通过各种云平台的API生成的列表,格式如下:

The flask app pulls a list that is generated via various cloud platform's APIs, and is in the below format:

vm_list = {
    'vmid': [],
    'name': [],
    'state': [],
    'platform': []
}

通过遍历API输出并附加每个值来填充列表,如下所示:

the list is populated by looping through the API output and appending each value like so:

def zip_list():
    ...
    for node in driver.list_nodes():
        vm_list["vmid"].append(node.uuid)
        vm_list["name"].append(node.name)
        vm_list["state"].append(node.state)
        vm_list["platform"].append(driver.name)
    ...
        myVms = zip(vm_list['name'], vm_list['vmid'], vm_list['platform'], vm_list['state'])
        return myVms

我正在通过我的烧瓶应用程序加载它,

I'm loading this via my flask app like this:

@app.route('/vms/')
def vms():
    myVms = {}
    myVms = vm.zip_list()
    return render_template('VMs.html', vm_list=myVms)

VMs.html页面将此数据加载到表中:

The VMs.html page loads this data into a table:

<table class="tableClass">
  <tr>
    <th>Name</th>
    <th>id</th>
    <th>Plaform</th>
    <th>State</th>
  </tr>
  {% for row in vm_list %}
  <tr>
    <td>{{ row[0] }}</td>
    <td>{{ row[1] }}</td>
    <td>{{ row[2] }}</td>
    <td>{{ row[3] }}</td>
  <tr>
  {% endfor %}
</table>

这很好,可以按预期加载数据.但是,我的问题是每次刷新页面时,数据都会再次加载并追加到列表中,从而使表大小增加了一倍.每次刷新都会将整个vm_list列表再次添加到表中.

And this works fine, loading the data as expected. However my problem is each time I refresh the page, the data is loaded and appended to the list once again, doubling the table size. Each refresh adds the whole vm_list list to the table once more.

我曾经认为可以通过在烧瓶应用程序脚本和/或zip_list函数中每次调用myVms变量(即myVms = {})来解决此问题,但这似乎不起作用;问题仍然存在.

I had thought this may be resolved by "nulling" the myVms variable each time it's called (i.e. myVms = {}) in the flask app script and/or the zip_list function but that doesn't seem to work; the issue still persists.

我还查看了烧瓶缓存,以查看每次重新加载后清除烧瓶缓存的方法是否可以解决它,但似乎没有.

I also looked into flask-caching to see if clearing flask's cache each reload would fix it but it appears not to.

我认为我可以更改html文件中的某些内容,以强制每次会话仅加载一次或类似的内容,但是我的前端技能并没有达到这么高的水平.

I'm thinking that I can change something in the html file to force this to only load once per session or something similar, but my front-end skills don't reach out that far.

有人知道我在这种情况下或在哪里出问题的时候可以做什么吗?任何建议,我们将不胜感激.

Does anyone have any idea what I can do in this situation or where I'm going wrong? Any advice is greatly appreciated.

推荐答案

您很接近-每次实际需要重置的变量不是myVms而是vm_list,如下所示:

You are close - the variable you actually need to reset each time is not myVms but vm_list, as follows:

class Node:
    counter = 0

    def __init__(self):
        c_str = str(Node.counter)
        self.uuid = "asdf" + c_str
        self.name = "test " + c_str
        self.state = "wow " + c_str + " such state"
        Node.counter += 1


class Driver:
    def __init__(self, number_of_nodes):
        self.nodes = []
        for x in range(number_of_nodes):
            self.nodes.append(Node())
        self.name = "the greatest driver"

    def list_nodes(self) -> list:
        return self.nodes


driver = Driver(10)


def zip_list():
    vm_list = {'vmid': [], 'name': [], 'state': [], 'platform': []}
    for node in driver.list_nodes():
        vm_list["vmid"].append(node.uuid)
        vm_list["name"].append(node.name)
        vm_list["state"].append(node.state)
        vm_list["platform"].append(driver.name)

    myVms = zip(vm_list['name'], vm_list['vmid'], vm_list['platform'], vm_list['state'])
    return myVms


print("First time:")

my_list = zip_list()
for i in my_list:
    print(i)

print("Second time:")

my_list = zip_list()
for i in my_list:
    print(i)

如果您在zip_list()函数之外初始化vm_list,则会看到正在经历的翻倍.

If you initialise vm_list outside of the zip_list() function instead, you will see the doubling up that you are experiencing.

这篇关于停止烧瓶复制加载的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆