Skip to main content

metabase-python

main codecov Code style: black

An unofficial Python library for the Metabase API.

Installation

pip install metabase-python

Usage

Connection

Start by creating an instance of Metabase with your credentials.

from metabase import Metabase

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

Interacting with Endpoints

You can then interact with any of the supported endpoints through the classes included in this package. Methods that instantiate an object from the Metabase API require the using parameter which expects an instance of Metabase such as the one we just instantiated above. All changes are reflected in Metabase instantly.

from metabase import User

# get all objects
users = User.list(using=metabase)

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

# 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(
    using=metabase,
    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, using=metabase)

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", using=metabase)

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

Querying & MBQL

You can also execute queries and get results back as a Pandas DataFrame. You can provide the exact MBQL, or use the Query object to compile MBQL (i.e. Metabase Query Language) from Python classes included in this package.

from metabase import Dataset, Query, Count, GroupBy, TemporalOption

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

# compile the MBQL above using the Query object
dataset = Dataset.create(
    database=1,
    type="query",
    query=Query(
        table_id=2,
        aggregations=[Count()],
        group_by=[GroupBy(id=7, option=TemporalOption.YEAR)]
    ).compile(),
    using=metabase
)

df = dataset.to_pandas()

As shown above, the Query object allows you to easily compile MBQL from Python objects. Here is a more complete example:

from metabase import Query, Sum, Average, Metric, Greater, GroupBy, BinOption, TemporalOption

query = Query(
    table_id=5,
    aggregations=[
        Sum(id=5),                                  # Provide the ID for the Metabase field
        Average(id=5, name="Average of Price"),     # Optionally, you can provide a name
        Metric.get(5)                               # You can also provide your Metabase Metrics
    ],
    filters=[
        Greater(id=1, value=5.5)                    # Filter for values of FieldID 1 greater than 5.5
    ],
    group_by=[
        GroupBy(id=4),                              # Group by FieldID 4
        GroupBy(id=5, option=BinOption.AUTO),       # You can use Metabase's binning feature for numeric fields
        GroupBy(id=5, option=TemporalOption.YEAR)   # Or it's temporal option for date fields
    ]
)

print(query.compile())
{
    'source-table': 5,
    'aggregation': [
        ['sum', ['field', 5, None]],
        ['aggregation-options', ['avg', ['field', 5, None]], {'name': 'Average of Price', 'display-name': 'Average of Price'}],
        ["metric", 5]
    ],
    'breakout': [
        ['field', 4, None],
        ['field', 5, {'binning': {'strategy': 'default'}}],
        ['field', 5, {'temporal-unit': 'year'}]
    ],
    'filter': ['>', ['field', 1, None], 5.5]
}

This can also be used to more easily create Metric objects.

from metabase import Metric, Query, Count, EndsWith, CaseOption


metric = Metric.create(
    name="Gmail Users",
    description="Number of users with a @gmail.com email address.",
    table_id=2,
    definition=Query(
        table_id=1,
        aggregations=[Count()],
        filters=[EndsWith(id=4, value="@gmail.com", option=CaseOption.CASE_INSENSITIVE)]
    ).compile(),
    using=metabase
)

Endpoints

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

Endpoints Support Notes
Activity ❌
Alert ❌
Automagic dashboards ❌
Card ✅
Collection ❌
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.

Metadata

Release files for metabase-python 0.3.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for metabase-python 0.3.3
File Size Uploaded Provenance
metabase-python-0.3.3.tar.gz 17.0 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for metabase-python 0.3.3
File Interpreter ABI Platform
metabase_python-0.3.3-py3-none-any.whl Python 3 none any Details

Total release size: 38.8 kB

Release files / metabase-python-0.3.3.tar.gz

Download URL metabase-python-0.3.3.tar.gz
Size 17.0 kB
Tags Source
SHA-256 checksum
How to use checksums
fa1015dd4066993c85eed1d8207674f70d34c05a6c72b324b47988a0647ed43c
BLAKE2b-256 checksum
How to use checksums
132e1f16bb02d962d6ee42c15ee7fecfa536a24adb6af306b9877c4dce646026
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.13 CPython/3.6.9 Linux/5.4.0-100-generic

Release files / metabase_python-0.3.3-py3-none-any.whl

Download URL metabase_python-0.3.3-py3-none-any.whl
Size 21.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d2db1003186751fbe0e3da90b660473b85f4229ede60a00b82540a147bc895a9
BLAKE2b-256 checksum
How to use checksums
837cd87118b8f602942f058c566da8199a8fbeac5a3c92e3eefd7a243169a85d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.13 CPython/3.6.9 Linux/5.4.0-100-generic

Security for metabase-python 0.3.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page