Basic Usage#
Installation#
To use metabase-tools, first install it using pip or poetry:
(.venv) $ pip install metabase-tools
OR
(.venv) $ poetry add metabase-tools
General Use#
The most basic use of the wrapper is to call a Metabase API endpoint so you do not need to manage authentication yourself.
There are two kinds of endpoints currently supported: - Endpoints that either create new objects or deal with groups (i.e. lists) of existing objects (e.g. getting a list) - Endpoints acting on an existing object (e.g. updates)
These two kinds of endpoints are handled separately in the wrapper to make calling their methods more convenient. For details on available methods, you will need to see the relevant page for each object type.
Type 1 - New or grouped object methods#
The following methods are supported for all current objects: - create - get - search
You will need to access these methods through the appropriate member of MetabaseApi after initialization, as seen here:
import metabase_tools
metabase_url = "http://localhost:3000"
credentials = {"username": "jim@dundermifflin.com", "password": "BAZouVa3saWgW89z"}
api = metabase_tools.MetabaseApi(metabase_url=metabase_url, credentials=credentials)
all_cards = api.cards.get()
for card in all_cards:
print(f"{card.id} - {card.name}")
targets = [1, 2, 3] # IDs of the card to fetch
some_cards = api.cards.get(targets=targets)
for card in some_cards:
print(f"{card.id} - {card.name}")
Type 2 - Single, existing object methods#
If a method acts on a single, existing object, you need to get an instance of that object first and call the method from that object, as seen here:
import metabase_tools
metabase_url = "http://localhost:3000"
credentials = {"username": "jim@dundermifflin.com", "password": "BAZouVa3saWgW89z"}
api = metabase_tools.MetabaseApi(metabase_url=metabase_url, credentials=credentials)
targets = [1] # ID of the card to fetch
card = api.cards.get()[0] # Return list so [0] will extract the item from that list
archived_card = card.archive() # Returns CardItem
If you need to apply an action to many objects at once, you can loop through them in a list comprehension:
1import metabase_tools
2
3metabase_url = "http://localhost:3000"
4credentials = {"username": "jim@dundermifflin.com", "password": "BAZouVa3saWgW89z"}
5api = metabase_tools.MetabaseApi(metabase_url=metabase_url, credentials=credentials)
6
7targets = [1, 2, 3] # IDs of the cards to fetch
8cards = api.cards.get()
9archived_cards = [card.archive() for card in cards]