Get Trello Boards data with py-trello

Pull data from trello boards and output results in a DataFrame in python

Abonia Sojasingarayar
IBM Data Science in Practice

--

Image by Author

In this article we will be discuss about the py-trello, a python wrapper around the Trello API and its usage. So we will be discussing :

  1. Py-Trello Overview
  2. Installation
  3. Usage
  4. Working with Trello Board
  5. Create a dataframe/Tabularize the data

1. Overview

A wrapper around the Trello API written in Python.This package automates most of the trello API calls with a line of code. We can get the list of boards available in trello, lists inside the board, Cards informations such as card name, description,activities, custom fields etc.

For more info py-trello official documentation and github repo.

2. Installation

pip

Use pip to install the Py-Trello library as follow:

pip install py-trello

conda-forge

To install py-trello and its core dependencies you can use:

conda install -c auto py-trello

3. Usage

Import trello client from trello package installed as in the above step. As like any other API client call we should provide API key, secret and token to establish the connection with trello.

For to get the connection information, sign in to trello developer account that you wish to work with and create your project.

from trello import TrelloClientclient = TrelloClient(
api_key='your-key',
api_secret='your-secret',
token='your-oauth-token-key',
token_secret='your-oauth-token-secret'
)

4. Working with board

4.1 Get all the trello board

So from the below code we can work and access the boards information.Useclientlist_boards to list all the boards available in your trello account.

boards = client.list_boards()
for board in boards:
print(board.name)
print(board.id)
print(board.url)

4.2. Get specific Board information

  • Get all the list in the board by specifying the board id as follows:
#Assign the board id with which you want to work BOARD_ID = "xxxx"
board = client.get_board(BOARD_ID)
#Get list list
lists = board.all_lists()
#List name, list ID
for list_ in lists:
print(list_.name)
print(list_.id)
  • Get all the cards in the board by specifying the board id as follows:
#Assign the board id with which you want to workBOARD_ID = "xxxx"
board = client.get_board(BOARD_ID)
#Get a list of cards
cards = board.get_cards()
#Card name, description, ID, URL
for card in cards:
print(card.name)
print(card.desc)
print(card.id)
print(card.url)

4.3 Add description and comment

Add the decription and comment for a specified card using its id.Use card.set_descriptionto add the description and card.comment to add the comment.

#Assign the card id with which you want to work
CARD_ID="xxx"
#Select a card
card=client.get_card(CARD_ID)
#Add a description
card.set_description("Lorem ipsum dolor sit amet, etiam non quam.")
#Add comment
card.comment("Dui faucibus in ornare quam viverra orci sagittis.")

4.4 Attach a file to Card

Attach the file in card as below by specifing its card id.Use card.attach to attach a file.Also mention path to the file you want to attach and name for the attachment.

#Assign the card id with which you want to work
CARD_ID="xxx"
#Select a card
card=client.get_card(CARD_ID)
file_path= "/path to file/"
file = open(file_path, 'rb')
card.attach(name="attachement name",file=file)

5. Get the custom fields of Card in Dataframe

We can get the custom fields of the cards. Also we can get name of the custom field as column and value of custom field as column values of dataframe. This can be used for the further data analysis tasks.

#Assign the board id with which you want to work
BOARD_ID = "xxxx"
# Get the board
board = client.get_board(BOARD_ID)
# Get all the list in the board
all_lists = board.open_lists()
df = pd.DataFrame()
# Get list name and cards in it (Card name)
for list in all_lists:
for card in list.list_cards():
dict = {}
df1 = pd.DataFrame()
if not card.closed:
dict['Card Name']=[card.name)
dict['List Name']=[list.name]
# Get the custom fields
for custom_fields in card.custom_fields:
dict[custom_fields.name]= [custom_fields.value]

# Create a data frame with card ,list , custom field in card
df1 = pd.DataFrame(dict)
# Merge the dataframe in order to add all card's custom fields
df =pd.concat([df, df1], ignore_index=True, sort=False)

So from the above code sample, we get a dataframe with columns such as Card Name, List Name, Custom field Names etc. Consider like every card in trello have the custom fields in order to get those data in a tabular format .

Here we consider only we have three list with each holding one card and all cards have the cutom fields. If there is no custom field the above code will let ‘NaN’ value in the place. The dataframe result will be as follow:

Dataframe with trello card custom fields as columns and its value as column value(Image by Author)

Conclusion:

In this article, we discuss about how py-trello facilitate the process of mining the trello board with minimum code. By using this library we can concentrate more time on data analysis and retriving the useful insight than scrapping and getting the data in first place.So see you all soon in my next article.

Ask your questions in comment and I will do my best to answer it.

Thank You for Reading

Connect with me on Linkedin

Find me on Github

Visit my technical channel on Youtube

Support: Buy me a Cofee/Chai

If you like this content, please dont forget to give your support by a clap. Be sure to SUBSCRIBE to never miss another article on data science topics, projects, guides and more!

--

--

Abonia Sojasingarayar
IBM Data Science in Practice

Principal Research Scientist | Machine Learning & Ops Engineer | Data Scientist | NLP Engineer | Computer Vision Engineer | AI Analyst