first instantiate a client object
from metabasepy import Client
cli = Client(username="XXX", password="****", base_url="https://your-remote-metabase-url.com")
than you can simply authenticate with
cli.authenticate()
Save new card with custom sql query:
cli.cards.post(database_id=1,
name="Card Name (eg: Available Stocks)",
query="select * from your_db.table_name;")
all_dbs = cli.databases.get()
print(all_dbs.__dict__)
cli.collections.post(name="Finance Reports", color="#ff0000")
cli.collections.get()
You can also query collection by its id:
cli.collections.get(collection_id=1)
from metabasepy import Client, MetabaseTableParser
cli = Client(username="XXX", password="****", base_url="https://your-remote-metabase-url.com")
query_response = cli.dataset.post(database_id=1, query="select * from customers;")
data_table = MetabaseTableParser.get_table(metabase_response=query_response)
print(data_table.__dict__)
from metabasepy import Client, MetabaseTableParser
cli = Client(username="XXX", password="****", base_url="https://your-remote-metabase-url.com")
query_response = cli.cards.query(card_id="1")
data_table = MetabaseTableParser.get_table(metabase_response=query_response)
print(data_table.__dict__)
Now you have table of query results (note that it will only list the first 2000 rows):
{
'status': 'completed',
'native_query': 'select \n u.first_name as "First Name",
\n u.last_name as "Last Name",
\n t.amount as "Amount",
\n t.description as "Description",
\n t.created_date as "Transaction Date"
from users_user u
inner join transactions_transaction t
on
t.user_id = u.id
where t.channel_id = 4;
',
'columns': [
'First Name',
'Last Name',
'Amount',
'Description',
'Transaction Date'
],
'rows': [...],
'database': 4
}
Than you can loop through rows & columns
for heading in data_table.columns:
print(heading)
from metabasepy import Client, MetabaseTableParser
cli = Client(username="XXX", password="****", base_url="https://your-remote-metabase-url.com")
cli.dataset.export(database_id=1, query="select * from customers;", export_format="xlsx")
Sample call:
In[8]: cli.dataset.export(database_id=2, query="select * from customers limit 10;", export_format="csv")
Out[8]: '/Users/john_doe/development/metabasepy/query_result_2020-10-30T10:55:30.663Z.csv'
from metabasepy import Client, MetabaseTableParser
import pandas as pd
cli = Client(username="XXX", password="****", base_url="https://your-remote-metabase-url.com")
json_result = cli.cards.download(card_id='123', format='json')
df = pd.DataFrame(json_result)
df.head()