vvaezian / metabase_api_python Goto Github PK
View Code? Open in Web Editor NEWA python wrapper for Metabase API
License: MIT License
A python wrapper for Metabase API
License: MIT License
Currently we can get the questions results using the get_card_data
function. But this returns all the data. We should let the user pass filter values for the question and get the filtered results.
Related to this forum post.
I'd love to be able changing the underlying database with the clone_card function. The Use case I've got in mind is having multiple environments which all have the same question/card but different databases.
I've imagined something like this:
mb.clone_card(card_id=2, source_table_id=16, target_table_id=69, target_database_id=1 ,new_card_name='Cloned Card', new_card_collection_id=6, new_database_id=2, ignore_these_filters=[])
As far as I can tell, only the database_id
as well as the dataset_query.database
properties of the card have to be changed.
Traceback (most recent call last):
File "metabase.py", line 42, in
mb.copy_collection(source_collection_id=17, destination_parent_collection_id=8, destination_collection_name='Vlaanderen_copy', deepcopy_dashboards=True, verbose=True)
File "C:\Users\janwa\AppData\Local\Programs\Python\Python37\lib\site-packages\metabase_api\metabase_api.py", line 761, in copy_collection
if item['model'] == 'collection':
TypeError: string indices must be integers
When running clone_card the following error is generated:
filters_data = card_info['dataset_query']['native']['template-tags']
KeyError: 'native'
Metabase Version: 0.41.2
As a business analyst, I would like to have many other custom functions, as instance add_card_to_dashboard
to add a specific card to a specific dashboard
In Metabase v.49 it is possible to authenticate using an API Token. The documentation brings more detailed information.
Can it be added to the metabase-api project?
Thanks
Hello, found your project in metabase discourse and I'm already using it in my company's deployment pipelines, thankyou!
I had to fix an issue I've found when deepcopying dashboards, for some reason metabase api returned cards with card ids as None. I've added some None checking at metabase_api.py around lines 530-560.
I've forked you project with this fix for this case, if youre interested I could open an PR.
In create_card function, first check that all the mentioned columns are in the database
There was a case the function didn't produce any error and when trying to open the card in MB it failed.
Deepcopying dashboards is supported natively in v.45+.
Either a mapping is provided between columns of the old and new source, or it is assumed the column names are the same (in this case an initial check should be performed)
Hi!
I am experiencing the following problem:
The code I am trying to execute is :
from metabase_api import Metabase_API
Error message:
File "/usr/local/lib/python3.6/dist-packages/metabase_api/__init__.py", line 1, in <module>
from metabase_api import Metabase_API
File "/usr/local/lib/python3.6/dist-packages/metabase_api/metabase_api.py", line 705
self.verbose_print(verbose, 'Successfully Archived.') if res == 202 else print('Archiving Failed.')
^
SyntaxError: invalid syntax
I can't use the other functions because of this error. I commented the function (move_to_archive()) and saved using Admin permission and now it's working.
Are there any prerequisites for this function (move_to_archive ()) works correctly?
In the get_columns_name_id
function, it checks friendly_names_is_disabled
first before allowing us to use the function as seen in https://github.com/vvaezian/metabase_api_python/blob/master/metabase_api/metabase_api.py#L339. However, in the friendly_names_is_disabled
function, it does a self.get('/api/setting')
as seen in https://github.com/vvaezian/metabase_api_python/blob/master/metabase_api/metabase_api.py#L372. This is causing a problem because according to https://github.com/metabase/metabase/blob/master/docs/api-documentation.md#setting, GET /api/setting/
is only available to superuser, so even though Friendly Table and Field Names
is already set to disable by the Admin, a non superuser is still unable to use the get_columns_name_id
function because self.get('/api/setting')
returns False
for a non superuser. Is it possible to consider the scenarios for a non superuser?
When running the mb.copy_collection command,
Copying the dashboard "Report" ...
Traceback (most recent call last):
File "/root/./copy_metabase_reports.py", line 5, in
mb.copy_collection(source_collection_id=5, destination_parent_collection_id=11, deepcopy_dashboards=True, verbose=True)
File "/usr/local/lib/python3.10/dist-packages/metabase_api/metabase_api.py", line 931, in copy_collection
self.copy_dashboard(source_dashboard_id=dashboard_id,
File "/usr/local/lib/python3.10/dist-packages/metabase_api/metabase_api.py", line 838, in copy_dashboard
card_json[prop] = card[prop]
KeyError: 'sizeX'
root@localhost:~#
{ "browser-info": { "language": "en-GB", "platform": "Win32", "userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36", "vendor": "Google Inc." }, "system-info": { "file.encoding": "UTF-8", "java.runtime.name": "OpenJDK Runtime Environment", "java.runtime.version": "11.0.17+8", "java.vendor": "Eclipse Adoptium", "java.vendor.url": "https://adoptium.net/", "java.version": "11.0.17", "java.vm.name": "OpenJDK 64-Bit Server VM", "java.vm.version": "11.0.17+8", "os.name": "Linux", "os.version": "5.15.0-56-generic", "user.language": "en", "user.timezone": "GMT" }, "metabase-info": { "databases": [ "h2", "mysql" ], "hosting-env": "unknown", "application-database": "mysql", "application-database-details": { "database": { "name": "MySQL", "version": "8.0.31-0ubuntu0.22.04.1" }, "jdbc-driver": { "name": "MariaDB Connector/J", "version": "2.7.6" } }, "run-mode": "prod", "version": { "date": "2022-12-07", "tag": "v0.45.1", "branch": "release-x.45.x", "hash": "019d31c" }, "settings": { "report-timezone": null } } }
Hello,
I found that the function clone_card doesn't change the column_id to target_col_id for 'aggregation' part of the query here
My card_info has the following part
'dataset_query': {'database': 5,
'query': {'aggregation': [['distinct',
['field', 577, None]]],
'filter': ['and',
['=', ['field', 576, None], 'type'],
['=',
['field', 567, None],
'approved']],
'source-table': 103},
'type': 'query'},
I assume that we have to iterate over aggregation part as well.
Metabase v0.43.0
This PR fixes the issue. Should refactor after the release.
It will be part of the 0.46 release.
Context
Currently, the testing suite is based on a database setup private to @vvaezian . When adding new endpoint or helper functions, we can't run it locally (or it implies heavy setup) to be sure that it accomplishes what we want.
Feature
The Ruby wrapper of Metabase uses a gem called VCR that has an interesting way of working : we register APIs calls made with the wrapper against a Metabase instance in YAML / JSON files. When running the test, instead of running it against a Metabase instance, it uses the APIs calls previously registered to check the results.
While it may seem a bit huge as a work to do, the quoted wrapper has currently 34 registered "cassettes" that covers most of the endpoints currently developed in metabase_api_python
Roadmap
For the first version we can assume the source of all the cards on the dashboard are the same (an initial check needs to be done in this case).
The function for changing the data source of a card is used for each card of the dashboard.
The dashboard filters needs to be updated as well.
I installed the package and I tried to authenticate with both the command line and a script, yet it's not working. When I run this code
from metabase_api import Metabase_API
Metabase_API("http://localhost:4009/","[email protected]","test123")
I'm getting the following error in the my terminal
Traceback (most recent call last):
File "mbmap.py", line 59, in <module>
Metabase_API("http://localhost:4009/","[email protected]","test123")
File "/home/test/.local/lib/python3.8/site-packages/metabase_api/metabase_api.py", line 13, in __init__
self.authenticate()
File "/home/test/.local/lib/python3.8/site-packages/metabase_api/metabase_api.py", line 25, in authenticate
self.session_id = res.json()['id']
File "/usr/lib/python3/dist-packages/requests/models.py", line 897, in json
return complexjson.loads(self.text, **kwargs)
File "/usr/lib/python3/dist-packages/simplejson/__init__.py", line 518, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3/dist-packages/simplejson/decoder.py", line 370, in decode
obj, end = self.raw_decode(s)
File "/usr/lib/python3/dist-packages/simplejson/decoder.py", line 400, in raw_decode
return self.scan_once(s, idx=_w(s, idx).end())
simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
I am getting error when trying to use method get_card_data with chosing data format 'json'
Traceback (most recent call last): File "<input>", line 1, in <module> File "/usr/local/lib/python3.9/site-packages/metabase_api/metabase_api.py", line 837, in get_card_data return eval(res.text) File "<string>", line 2, in <module> NameError: name 'null' is not defined
Seems like eval() does not like 'null', maybe using of json.loads() instead could help.
is there any metabase api support auto change Field mapping from the table a to table b , table a,b have Same data structure
if has this api ,i'm pleasure to add it to this project
look forward to your reply , thanks
The create_collection method does not check if a collection with that name already exists.
I only noticed this when using the get_item_info method to get the details of the collection I had created and it returned the message "ValueError: There is more than one collection with the name"
Copying a card or creating a card using custom_json with description = '' results in an non-informative error. My sugestion is to either add an informative error mensage, or to create a conditional statement such as "if custom_json['description'] = '' then custom_json['description']= None".
Currently, the module is built quite simply with REST functions
at the beginning of the metabase-api
and helper functions that helps developers do the most commons actions made on a Metabase instance : create cards, modify them, get informations.
In order to have a more complete wrapper that embrace all the possibilities of the Metabase API and in the same maintaining the simplicity of this wrapper, it could be relevant to do some non-breaking refactoring:
metabase_api_python # we keep the upper level unchanged
README.md
LICENSE
...
metabase_api_python # here's come the changes
src
rest_functions.py # contain the REST functions needed by all endpoints
utils.py # get_item_id, get_item_info etc. go here
endpoints # containing relevant helpers functions relating to a specific endpoint,
cards.py
dashboards.py
collections.py
... # other endpoints not currently implemented ๐ฅ
metabase_api.py # keeping Metabase_API class here, that imports all functions from src folder
Funded by
Open Source Politics
As a developer, I want to get items from a Metabase instance only knowing their names.
Currently, get_item_id
and get_item_name
aren't fully working with objects like database
or collection
.
Complete those auxiliary with some new objects available : dashboard
, collection
andpulse
Funded by OpenSourcePolitics
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.