|
1 |
| -# Metabase Python |
| 1 | +# metabase-python |
2 | 2 | [](https://github.com/chasleslr/metabase-python/actions/workflows/main.yml)
|
3 | 3 | [](https://codecov.io/gh/chasleslr/metabase-python)
|
4 | 4 | [](https://github.com/psf/black)
|
| 5 | + |
| 6 | +An unofficial Python library for the [Metabase API](https://www.metabase.com/learn/administration/metabase-api). |
| 7 | + |
| 8 | +This API is still experimental and may change significantly between minor versions. |
| 9 | + |
| 10 | +## Installation |
| 11 | + |
| 12 | +``` |
| 13 | +pip install metabase-python |
| 14 | +``` |
| 15 | + |
| 16 | +## Usage |
| 17 | + |
| 18 | +Start by creating an instance of Metabase with your credentials. This connection will automatically be used by any |
| 19 | +object that interacts with the Metabase API. |
| 20 | +```python |
| 21 | +from metabase import Metabase |
| 22 | + |
| 23 | +metabase = Metabase( |
| 24 | + host="<host>", |
| 25 | + user="<username/email>", |
| 26 | + password="<password>", |
| 27 | +) |
| 28 | +``` |
| 29 | + |
| 30 | +You can then interact with any of the supported endpoints. All changes are reflected in Metabase instantly. |
| 31 | + |
| 32 | +```python |
| 33 | +from metabase import User |
| 34 | + |
| 35 | +# get all objects |
| 36 | +users = User.list() |
| 37 | + |
| 38 | +# get an object by ID |
| 39 | +user = User.get(1) |
| 40 | + |
| 41 | +# attributes are automatically loaded and available in the instance |
| 42 | +if user.is_active: |
| 43 | + print("User is active!") |
| 44 | + |
| 45 | +# update any available attribute |
| 46 | +user.update(is_superuser=True) |
| 47 | + |
| 48 | +# delete an object |
| 49 | +user.delete() |
| 50 | + |
| 51 | +# create an object |
| 52 | +new_user = User.create( |
| 53 | + first_name="<first_name>", |
| 54 | + last_name="<last_name>", |
| 55 | + email="<email>", |
| 56 | + password="<password>" |
| 57 | +) |
| 58 | +``` |
| 59 | + |
| 60 | +The methods `.list()`, `.get()`, `.create()`, `.update()`, `.delete()` are available on all |
| 61 | +endpoints that support them in Metabase API. |
| 62 | + |
| 63 | +Some endpoints also support additional methods: |
| 64 | + |
| 65 | +```python |
| 66 | +from metabase import User |
| 67 | + |
| 68 | +user = User.get(1) |
| 69 | + |
| 70 | +user.reactivate() # Reactivate user |
| 71 | +user.send_invite() # Resend the user invite email for a given user. |
| 72 | +``` |
| 73 | + |
| 74 | +Here's a slightly more advanced example: |
| 75 | +```python |
| 76 | +from metabase import User, PermissionGroup, PermissionMembership |
| 77 | + |
| 78 | +# create a new PermissionGroup |
| 79 | +my_group = PermissionGroup.create(name="My Group") |
| 80 | + |
| 81 | +for user in User.list(): |
| 82 | + # add all users to my_group |
| 83 | + PermissionMembership.create( |
| 84 | + group_id=my_group.id, |
| 85 | + user_id=user.id |
| 86 | + ) |
| 87 | +``` |
| 88 | + |
| 89 | +You can also execute queries and get results back as a Pandas DataFrame. Currently, you need to provide |
| 90 | +the exact MBQL (i.e. Metabase Query Language) as the `query` argument -- future iterations will improve this workflow. |
| 91 | +```python |
| 92 | +from metabase import Dataset |
| 93 | + |
| 94 | +dataset = Dataset.create( |
| 95 | + database=1, |
| 96 | + type="query", |
| 97 | + query={ |
| 98 | + "source-table": 1, |
| 99 | + "aggregation": [["count"]], |
| 100 | + "breakout": ["field", 7, {"temporal-unit": "year"},], |
| 101 | + }, |
| 102 | +) |
| 103 | + |
| 104 | +df = dataset.to_pandas() |
| 105 | +``` |
| 106 | + |
| 107 | + |
| 108 | +## Endpoints |
| 109 | + |
| 110 | +For a full list of endpoints and methods, see [Metabase API](https://www.metabase.com/docs/latest/api-documentation.html). |
| 111 | + |
| 112 | +| Endpoints | Support | |
| 113 | +|-----------------------|:----------:| |
| 114 | +| Activity | ❌ | |
| 115 | +| Alert | ❌ | |
| 116 | +| Automagic dashboards | ❌ | |
| 117 | +| Card | ❌ | |
| 118 | +| Collection | ❌ | |
| 119 | +| Card | ❌ | |
| 120 | +| Dashboard | ❌ | |
| 121 | +| Database | ❌ | |
| 122 | +| Dataset | ✅ | |
| 123 | +| Email | ❌ | |
| 124 | +| Embed | ❌ | |
| 125 | +| Field | ✅ | |
| 126 | +| Geojson | ❌ | |
| 127 | +| Ldap | ❌ | |
| 128 | +| Login history | ❌ | |
| 129 | +| Metric | ✅ | |
| 130 | +| Native query snippet | ❌ | |
| 131 | +| Notify | ❌ | |
| 132 | +| Permissions | ❌ | |
| 133 | +| Premium features | ❌ | |
| 134 | +| Preview embed | ❌ | |
| 135 | +| Public | ❌ | |
| 136 | +| Pulse | ❌ | |
| 137 | +| Revision | ❌ | |
| 138 | +| Search | ❌ | |
| 139 | +| Segment | ✅ | |
| 140 | +| Session | ❌ | |
| 141 | +| Setting | ❌ | |
| 142 | +| Setup | ❌ | |
| 143 | +| Slack | ❌ | |
| 144 | +| Table | ✅ | |
| 145 | +| Task | ❌ | |
| 146 | +| Tiles | ❌ | |
| 147 | +| Transform | ❌ | |
| 148 | +| User | ✅ | |
| 149 | +| Util | ❌ | |
| 150 | + |
| 151 | +## Contributing |
| 152 | +Contributions are welcome! |
| 153 | + |
| 154 | +## License |
| 155 | +This library is distributed under the MIT license. |
0 commit comments