Skip to main content

Collection of useful python code for automation in Microsoft Fabric.

Project description

Fabric Python Helper

Collection of useful python code for automation in Microsoft Fabric.

PBI Admin

In Fabric notebook run:

from fabric_python_helper import pbi_admin as pbi

Access Tokens

Can be initiated directly:

access_token = pbi.AccessTokens().get_token_as_fabric_notebook_owner()

Dataflows

access_token = pbi.AccessTokens().get_token_as_fabric_notebook_owner()
workspace_id = "7509f..."
dataflow_id = "46aed9..."

dataflow = pbi.Dataflows(workspace_id, dataflow_id, access_token)
dataflow.refresh_dataflow(expected_duration=60, loop_interval=20, wait_for_completion=True)

Semantic Models

access_token = pbi.AccessTokens().get_token_as_fabric_notebook_owner()
semantic_model_id = "d0c24..."

semantic_model = pbi.SemanticModels(semantic_model_id, access_token)
semantic_model.refresh_semantic_model(expected_duration=60, loop_interval=20, wait_for_completion=True)

GraphAPI

Requires an app registration with delegated User.Read, Mail.ReadWrite, Mail.Send and offline_access scopes. The public client flow must also be enabled. Requires an azure key vault which the notebook owner has permissions to update and read secrets on.

In Fabric notebook run:

from fabric_python_helper import graph_api as gr

Emails

Create an instance for an email account:

tennant_id = "23...."
client_id = "00df..."
akv_url = "https://name.vault.azure.net/"
refresh_secret_name = "Secret Name"

email_account = gr.Emails(tennant_id, client_id, akv_url, refresh_secret_name)

Initial Authentication

On first run you will need to authenticate, this is using device flow authentication:

email_account.get_initial_tokens()

It will give you a link and device code which you need to follow and authenticate with.

Connect to Graph API

email_account.connect()

This retrieves an access token for the current session (lasts an hour) and replaces the refresh token in the key vault. Currently unclear as to whether this resets the expiry period.

Find the message id (currently returns first email only)

message_id = email_account.search_message_by_subject_and_sender("Attachment Test", "email@address.com")

Get list of attachment ids:

attachments = email_account.get_attachment_ids_and_names(message_id)

Get attachments:

(You can loop through the list of attachments if required)

attachment = email_account.download_attachment(message_id = message_id, attachment_id = attachments[0][0], is_binary=False, encoding="utf-8")

Save to onelake:

Assuming it's a text file eg you're here because you're grabbing a csv from an email you can just use

mssparkutils.fs.put(file_path, attachment, overwrite=True)

If you returned a binary other methods would be required.

Delete the email:

email_account.delete_email(message_id)

Send an email

email_addresses = ["email1@address.co.uk", "email2@address.co.uk"]

subject = "Test Email"

content = """
Hi,
I'm the content of the email.
I'm on my own line.

    From Ben               
"""

email_account.send_email(subject, content, email_addresses)

Building a Wheel File

In order to build I had to add to create a pip.ini file in C:\ProgramData\pip\
Which contained:

[global]
trusted-host = files.pythonhosted.org
               pypi.org

You can then build with:

  • pip install build
  • python -m build

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

fabric_python_helper-0.3.0.tar.gz (12.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fabric_python_helper-0.3.0-py3-none-any.whl (12.6 kB view details)

Uploaded Python 3

File details

Details for the file fabric_python_helper-0.3.0.tar.gz.

File metadata

  • Download URL: fabric_python_helper-0.3.0.tar.gz
  • Upload date:
  • Size: 12.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for fabric_python_helper-0.3.0.tar.gz
Algorithm Hash digest
SHA256 e396f2b9e104cb7597f8d3c28e0a6b56d456d2fc456c70fcef29799f90d9bec0
MD5 ba2f3f563550a8bb3fb503fc6c55e53d
BLAKE2b-256 19ae956f3a3cee3878aefe6ef7757a00a66d831666ba626f39766444b1ed83a5

See more details on using hashes here.

File details

Details for the file fabric_python_helper-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for fabric_python_helper-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8ab07d3e62df1cbc85fcab4023dff316f7bdac69ae0edd5977269f05d0bfecc8
MD5 5ae4c062274973f8d58a2024e40c88c2
BLAKE2b-256 35e4512b2c2db4ca5f33d032a163084f7edc5585f54b2e6019e196f1a8920558

See more details on using hashes here.

Supported by

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