-1

I want to transfer label_count and card_m to my main flask python file. How do I do that? I already tried importing it it didn't work. And if there is any solution to card_m I don't want repeat request so many times

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)


    
  • You need to clarify what you mean by transfer. How are you going to be using label_count and card_m? Answer those two and I might be able to help out. But I don't know what situation you are trying to accomplish so i can't help. – TeddyBearSuicide Aug 03 '20 at 00:45
  • I mean I want to get card_m and label_count values from this python file and print it on my HTML file through flask app python – purevdorj anujin Aug 04 '20 at 01:47

2 Answers2

0

What do you mean, "transfer"? If you want to use them in another function, do this:

from main_python import other_function

print(label_count, card_m)
other_function(label_count, card_m)
GAEfan
  • 11,244
  • 2
  • 17
  • 33
0

Ok based on you comments i think i can help you out now. So two things you should do to make this as clean as possible and to avoid bugs later on.

Right now your code is in the global scope. You should avoid doing this at cost unless there is literally no other option. So first thing you should do is create a static class for holding this data. Maybe something like this.

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

Now we need to run that startup method in this class before we start up flask via app.run. So it can look something like this...

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

Now we have populated those static variables with the data. Now you just need to import that static class in whatever file you want and just call get_data and you will get what you want. So like this...

from labelhelper import LabelHelper

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

FYI in the from import labelhelper being lowercase if cause in general you would name the file containing that class labelhelper.py

TeddyBearSuicide
  • 1,377
  • 1
  • 6
  • 11