sarumont / py-trello

Python API wrapper around Trello's API

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Can't get the labels from a card using card.list_labels()

leo-smi opened this issue · comments

py-trello: version 0.18.0

from trello import TrelloClient

client = TrelloClient( 
    api_key    = 'api_key',
    api_secret = 'api_secret',
)

all_boards = client.list_boards()


# getting a specific board
my_board_name = 'my_board_name'
for board in all_boards:
	if board.name == my_board_name:
		board_my_board_name = board
		print(board)
		break


# list all lists from a board (includes archived/closed)
lists_board_my_board_name = board_my_board_name.all_lists() 


# getting all cards from all lists (includes archived/closed)
print(f"\nThere's {len(lists_board_my_board_name)} lists in the board {my_board_name}, are they:\n")

for lst in lists_board_my_board_name:
    print(lst.name)
    for card in lst.list_cards():
        print("    ", card.name)
        print(card)
        for label in card.list_labels():
            print("        ", label)

Error:

AttributeError: 'Card' object has no attribute 'Labels'

I dit it!
sources:
https://python.hotexamples.com/pt/examples/trello/TrelloClient/-/python-trelloclient-class-examples.html
https://py-trello-dev.readthedocs.io/en/latest/trello.html#module-trello.board

for lst in lists_board_my_board_name:
    print(lst.name)
    for card in lst.list_cards():
        print(" "*4, card.name)
        # print(card.idLabels)
        card_labels_ids = card.idLabels
        if len(card_labels_ids) > 0:
            for label_id in card_labels_ids:
                print(" "*8, 
                      client.get_label(label_id, board_my_board_id).name
                )
        else:
            print(" "*8, "The card haven't a label")

Another option is to create a label dictionary first to avoid future delayed requests

# getting a specific board
my_board_name = 'my_board_name'
for board in all_boards:
	if board.name == my_board_name:
		my_board_name = board
		my_board_id   = my_board_name.id
		print(my_board_name)
		print(my_board_id)
		break

# creating a boar labels dictionary
my_board_labels      = my_board_name.get_labels(fields='all', limit=50)
my_board_labels_dict = {}
for label in my_board_labels:
    my_board_labels_dict.update({str(label.id):str(label.name)})
    print(label.id, label.name)

# list all lists from a board (includes archived/closed)
my_board_lists = my_board_name.all_lists()


# getting all cards from all lists (includes archived/closed)
print(f"\nThere's {len(my_board_lists)} lists in the board {my_board_name}, are they:\n")

for lst in my_board_lists:
    print(lst.name)
    for card in lst.list_cards():
        print(" "*4, card.name)
        # print(card.idLabels)
        card_labels_ids = card.idLabels
        if len(card_labels_ids) > 0:
            for label_id in card_labels_ids:
                print(" "*8, 
                      my_board_labels_dict[label_id]
                )
        else:
            print(" "*8, "The card haven't a label")

# show closed lists and cards
closed_lists_my_board = my_board_name.closed_lists() # already gets 
closed_cards_my_board = my_board_name.closed_cards() # already gets 

print("ARCHIVED LISTS:")
for lst in closed_lists_my_board:
    print("    ", lst.name)

print()

print("ARCHIVED CARDS:")
for card in closed_cards_my_board:
    print("    ", card.name)