如何将变量从 python 文件传输到 python 烧瓶

How to transfer variable from python file to python flask

我想将 label_count 和 card_m 转移到我的主烧瓶 python 文件中。我怎么做?我已经尝试导入它,但没有用。如果有任何解决方案 card_m 我不想重复请求那么多次

import requests
import json
from itertools import chain 
from collections import Counter
  
url = "https://api.trello.com/1/boards/OIeEN1vG/cards"
query = {
    'key': 'e8cac9f95a86819d54194324e95d4db8',
    'token': 'aee28b52f9f8486297d8656c82a467bb4991a1099e23db539604ac35954d5633'
    }

response = requests.request(
    "GET",
    url,
    params=query
    )
data = response.json()
card_labels_string = list(chain.from_iterable([d['labels']for d in data]))
card_labels = [c ["color"] for c in card_labels_string]
label_count = dict((i, card_labels.count(i)) for i in card_labels)

cards = dict(zip([d['name']for d in data],[d['shortLink']for d in data]))
card_m = {}
for key,value in cards.items():
    url_card = "https://api.trello.com/1/cards/{}/members".format(value)
    res = requests.request(
        "GET",
        url_card,
        params=query
        )
    names = [f['fullName']for f in res.json()]
    card_m.update({key : names})

print(label_count, card_m)


    

“转移”是什么意思?如果您想在另一个函数中使用它们,请执行以下操作:

from main_python import other_function

print(label_count, card_m)
other_function(label_count, card_m)

好的,根据您的评论,我想我现在可以帮助您了。所以你应该做两件事来使它尽可能干净并避免以后出现错误。

现在您的代码在全局范围内。除非确实没有其他选择,否则您应该避免以成本为代价这样做。所以你应该做的第一件事是创建一个静态 class 来保存这些数据。也许是这样的。

class LabelHelper(object):
    card_m = {}
    label_count = None

    @classmethod
    def startup(cls):
        url = "https://api.trello.com/1/boards/OIeEN1vG/cards"
        query = {
            'key': 'e8cac9f95a86819d54194324e95d4db8',
            'token': 'aee28b52f9f8486297d8656c82a467bb4991a1099e23db539604ac35954d5633'
        }

        response = requests.request(
            "GET",
            url,
            params=query
        )
        data = response.json()
        card_labels_string = list(chain.from_iterable([d['labels'] for d in data]))
        card_labels = [c["color"] for c in card_labels_string]
        cls.label_count = dict((i, card_labels.count(i)) for i in card_labels)

        cards = dict(zip([d['name'] for d in data], [d['shortLink'] for d in data]))
        for key, value in cards.items():
            url_card = "https://api.trello.com/1/cards/{}/members".format(value)
            res = requests.request(
                "GET",
                url_card,
                params=query
            )
            names = [f['fullName'] for f in res.json()]
            cls.card_m.update({key: names})
            
    @classmethod
    def get_data(cls):
        return cls.label_count, cls.card_m

现在我们需要 运行 这个 class 中的 startup 方法,然后再通过 app.run 启动烧瓶。所以它看起来像这样...

if __name__ == '__main__':
    LabelHelper.startup()
    app.run("your interface", your_port)

现在我们已经用数据填充了这些静态变量。现在你只需要在你想要的任何文件中导入那个静态 class 然后调用 get_data 你就会得到你想要的。所以像这样...

from labelhelper import LabelHelper

def some_function():
    label_count, card_m = LabelHelper.get_data()

FYI 在 from import labelhelper 中是小写的,因为通常你会命名包含该 class labelhelper.py

的文件