Skip to content

Commit

Permalink
add format_rows option to get_card_data function and apply new ve…
Browse files Browse the repository at this point in the history
…rsioning format
  • Loading branch information
vvaezian committed Jun 1, 2024
1 parent 8ff485c commit 627b53a
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 3.4
### Changed
- New versioning format (3.4 instead of 0.3.4)
- Added `format_rows` option to `get_card_data` function

## 0.3.3
### Changed
- Authentication using API key
Expand Down
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,3 @@ Deletes the item (Card, Dashboard, Pulse). Currently Collections and Segments ca
```python
mb.delete_item('card', item_id=123)
```
## Notes
There are also two other Python wrappers for Metabase API [here](https://github.com/mertsalik/metabasepy) and [here](https://github.com/STUnitas/metabase-py).
17 changes: 11 additions & 6 deletions metabase_api/metabase_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,19 @@ def search(self, q, item_type=None):



def get_card_data(self, card_name=None, card_id=None, collection_name=None,
collection_id=None, data_format='json', parameters=None):
def get_card_data(self, card_name=None, card_id=None, collection_name=None, collection_id=None,
data_format='json', parameters=None, format_rows=False):
'''
Run the query associated with a card and get the results.
The data_format keyword specifies the format of the returned data:
Keyword arguments:
data_format -- specifies the format of the returned data:
- 'json': every row is a dictionary of <column-header, cell> key-value pairs
- 'csv': the entire result is returned as a string, where rows are separated by newlines and cells with commas.
To pass the filter values use 'parameters' param:
parameters -- can be used to pass filter values:
The format is like [{"type":"category","value":["val1","val2"],"target":["dimension",["template-tag","filter_variable_name"]]}]
See the network tab when exporting the results using the web interface to get the proper format pattern.
format_rows -- whether the returned results should be formatted or not
'''
assert data_format in [ 'json', 'csv' ]
if parameters:
Expand All @@ -113,9 +116,11 @@ def get_card_data(self, card_name=None, card_id=None, collection_name=None,
collection_id=collection_id,
item_type='card')

# add the filter values (if any)
import json
params_json = {'parameters':json.dumps(parameters)}
params_json = {
'parameters':json.dumps(parameters),
'format_rows':'true' if format_rows else 'false'
}

# get the results
res = self.post("/api/card/{}/query/{}".format(card_id, data_format), 'raw', data=params_json)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="metabase-api",
version="0.3.3.1",
version="3.4",
author="Vahid Vaezian",
author_email="[email protected]",
description="A Python Wrapper for Metabase API",
Expand Down

0 comments on commit 627b53a

Please sign in to comment.