Skip to main content

A Python wrapper for interacting with Metabase's API.

Project description

metabase-python

main codecov Code style: black

An unofficial Python library for the Metabase API.

This API is still experimental and may change significantly between minor versions.

Installation

pip install metabase-python

Usage

Start by creating an instance of Metabase with your credentials. This connection will automatically be used by any object that interacts with the Metabase API.

from metabase import Metabase

metabase = Metabase(
    host="<host>",
    user="<username/email>",
    password="<password>",
)

You can then interact with any of the supported endpoints. All changes are reflected in Metabase instantly.

from metabase import User

# get all objects
users = User.list()

# get an object by ID
user = User.get(1)

# attributes are automatically loaded and available in the instance
if user.is_active:
    print("User is active!")

# update any available attribute
user.update(is_superuser=True)

# delete an object
user.delete()

# create an object
new_user = User.create(
    first_name="<first_name>",
    last_name="<last_name>",
    email="<email>",
    password="<password>"
)

The methods .list(), .get(), .create(), .update(), .delete() are available on all endpoints that support them in Metabase API.

Some endpoints also support additional methods:

from metabase import User

user = User.get(1)

user.reactivate()   # Reactivate user
user.send_invite()  # Resend the user invite email for a given user.

Here's a slightly more advanced example:

from metabase import User, PermissionGroup, PermissionMembership

# create a new PermissionGroup
my_group = PermissionGroup.create(name="My Group")

for user in User.list():
    # add all users to my_group
    PermissionMembership.create(
        group_id=my_group.id,
        user_id=user.id
    )

You can also execute queries and get results back as a Pandas DataFrame. Currently, you need to provide the exact MBQL (i.e. Metabase Query Language) as the query argument -- future iterations will improve this workflow.

from metabase import Dataset

dataset = Dataset.create(
    database=1,
    type="query",
    query={
        "source-table": 1,
        "aggregation": [["count"]],
        "breakout": ["field", 7, {"temporal-unit": "year"},],
    },
)

df = dataset.to_pandas()

Endpoints

For a full list of endpoints and methods, see Metabase API.

Endpoints Support
Activity
Alert
Automagic dashboards
Card
Collection
Card
Dashboard
Database
Dataset
Email
Embed
Field
Geojson
Ldap
Login history
Metric
Native query snippet
Notify
Permissions
Premium features
Preview embed
Public
Pulse
Revision
Search
Segment
Session
Setting
Setup
Slack
Table
Task
Tiles
Transform
User
Util

Contributing

Contributions are welcome!

License

This library is distributed under the MIT license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

metabase-python-0.1.dev63.tar.gz (32.8 kB view hashes)

Uploaded Source

Built Distribution

metabase_python-0.1.dev63-py3-none-any.whl (13.7 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page