1 file was added to this release more than 14 days after its initial publication. Inspect the release files before installing.
pbipy
A Power BI Rest API wrapper for Python.
pbipy aims to simplyify working with the Power BI Rest API and support programatic administration of Power BI in Python.
NOTE: At the moment pbipy is only implementing GET based methods, i.e., getting data from the Rest API. Functionality to update and create new items on the Power BI Instance will be implemented soon.
Installation
pip install pbipy
Authentication
To use pbipy you'll first need to acquire a bearer_token.
How do I get a bearer_token?
To acquire a bearer_token you'll need to authenticate against your Registered Azure Power BI App. Registering is the first step in turning on the Power BI Rest API, so from this point on we're assuming that registration is taken care of.
To authenticate against the Registered App, Microsoft provides the MSAL and azure-identity python libraries. These libraries support many different ways of acquiring a bearer_token.
Because there are multiple ways to acquire the token, pbipy assumes you'll do this in the way that suits, rather than directly handling authentication (of course, this might change in future).
This README doesn't cover Authentication in detail, however, these are some helpful resources that look at acquiring a bearer_token in the context of Power BI:
- Power BI REST API with Python and MSAL. Part II.
- Power BI REST API with Python Part III, azure-identity
- Monitoring Power BI using REST APIs from Python
See below for an example that uses the msal library.
Using pbipy
Start by creating the PowerBI() client. All interactions with the Power BI Rest API go through this object.
The example uses msal authentication methods to get a bearer_token.
import msal
from pbipy import PowerBI
def acquire_bearer_token(username, password, azure_tenant_id, client_id, scopes):
app = msal.PublicClientApplication(client_id, authority=azure_tenant_id)
result = app.acquire_token_by_username_password(username, password, scopes)
return result["access_token"]
bearer_token = acquire_bearer_token(
username="your-username",
password="your-password",
azure_tenant_id="https://login.microsoftonline.com/your-azure-tenant-id",
client_id="your-pbi-client-id",
scopes=["https://analysis.windows.net/powerbi/api/.default"],
)
# Create Client
pbi = PowerBI(bearer_token)
Operation Groups
pbipy methods are organized into Operation Groups that mirror the operation groups of the Power BI Rest API:
For example:
pbi.groups.get_groups()
pbi.datasets.get_dataset("cfafbeb1-8037-4d0c-896e-a46fb27ff229")
...
Functionality
To interact with the API, simply call the relevant method from the client.
groups = pbi.groups.groups.get_groups()
print(groups[0])
# Group(id='3d9b93c6-7b6d-4801-a491-1738910904fd', name='marketing group', type='Workspace', ...)
Most methods support passing in an object id...
refresh_history = pbi.datasets.get_refresh_history("cfafbeb1-8037-4d0c-896e-a46fb27ff229")
print(refresh_history[0])
# Refresh(id=None, request_id='9399bb89-25d1-44f8-8576-136d7e9014b1', refresh_type='ViaApi', ...)
... or just pass in the object itself.
sales = pbi.datasets.get_dataset("cfafbeb1-8037-4d0c-896e-a46fb27ff229")
refresh_history = pbi.datasets.get_refresh_history(sales)
print(refresh_history[0])
# Refresh(id=None, request_id='9399bb89-25d1-44f8-8576-136d7e9014b1', refresh_type='ViaApi', ...)
pbipy converts API responses into Python objects.
sales = pbi.datasets.get_dataset("cfafbeb1-8037-4d0c-896e-a46fb27ff229")
print(type(sales))
# <class 'pbipy.models.Dataset'>
To make life easier, attributes are also translated into sensible types.
sales = pbi.datasets.get_dataset("cfafbeb1-8037-4d0c-896e-a46fb27ff229")
print(isinstance(sales.created_date, datetime))
# True
Objects are standardized to have consistent attributes, even when a key is missing from an API response.
sales = Dataset(id="cfafbeb1-8037-4d0c-896e-a46fb27ff229", name="SalesMarketing")
print(hasattr(sales, "is_refreshable"))
print(sales.is_refreshable)
# True
# None
And if you need to access the raw json representation, this is supported to.
sales = pbi.datasets.get_dataset("cfafbeb1-8037-4d0c-896e-a46fb27ff229")
print(sales.raw)
# {'id': 'cfafbeb1-8037-4d0c-896e-a46fb27ff229', 'name': 'SalesMarketing', 'addRowsAPIEnabled': False, ...},
Power BI Rest API Reference
In general, most of the PowerBI() methods follow the resources laid out in the Power BI Rest API Reference:
Power BI REST APIs for embedded analytics and automation - Power BI REST API
Acknowledgements
The design of this library was inspired by the pycontribs/jira library. It also borrows elements of cmberryay's pypowerbi wrapper. A personal Thank You to all the contributors to these libraries for the great examples of what an API Wrapper can be.
Metadata
Release files for pbipy 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
File added late
1 file was uploaded more than 14 days after the first file in this release.
While project maintainers occasionally add legitimate files to an existing release, late additions can also indicate a security compromise.
We recommend inspecting the release files before installing.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| pbipy-1.0.0.tar.gz | 16.3 kB | Details |
Built distributions (wheels)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pbipy-1.1.1-py2.py3-none-any.whl | Python 3, Python 2 | none | any | Details |
| pbipy-1.0.0-py2.py3-none-any.whl | Python 3, Python 2 | none | any | Details |
Total release size: 68.1 kB
Release files / pbipy-1.0.0.tar.gz
| Download URL | pbipy-1.0.0.tar.gz |
|---|---|
| Size | 16.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
47619b35f4ab33f22c9c31ded093a3e2273a85f474a36be80a0830711f510098
|
|
BLAKE2b-256 checksum How to use checksums |
23d1bfe2e139bf4e4e5a2d7fe3e03e95977729786744063bb27b1b270a33abcd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.10.6
|
Release files / pbipy-1.1.1-py2.py3-none-any.whl
File added late
This file was uploaded more than 14 days after the first file in this release.
While project maintainers occasionally add legitimate files to an existing release, late additions can also indicate a security compromise.
We recommend inspecting the release file before installing.
| Download URL | pbipy-1.1.1-py2.py3-none-any.whl |
|---|---|
| Size | 30.3 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
4eda9b947c80984d9ae57a4ea983b7e428a5c83f8b182bcda57e096f738095fc
|
|
BLAKE2b-256 checksum How to use checksums |
3a1e8721bed5d953b32a13f0dbd8b1e7ab80445368e84f9a94f78e76d6bbe5b9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.10.6
|
Release files / pbipy-1.0.0-py2.py3-none-any.whl
| Download URL | pbipy-1.0.0-py2.py3-none-any.whl |
|---|---|
| Size | 21.5 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
2c0806c86fa6e2db1fffa74a910e3a969345b9c4572a598c7e046f3e2710cbf9
|
|
BLAKE2b-256 checksum How to use checksums |
63cc70f00445d2ccdbc6605611fe15689557dc72fc6c5d76f0c5078ce60bd853
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.10.6
|