Tutorial

Scheduled export of visualizations and dashboards

  • 14 June 2023
  • 0 replies
  • 227 views
Scheduled export of visualizations and dashboards
Userlevel 1

If you want to export your dashboard data on a regular basis, you can do it by utilizing GoodData Python SDK.

 

Disclaimer: the code below uses the development release of GoodData Python SDK.

You can install the development release of GoodData Python SDK by executing the following command:

pip install gooddata-sdk==1.3.1.dev4

 

CSV or XLSX Export

You can use tabular export in case you want to use data in CSV, or XLSX format of particular visualization. The following code block shows how you can do it via GoodData Python SDK.

from gooddata_sdk import GoodDataSdk, ExportRequest


# GoodData base URL, e.g. "https://www.example.com"
host = "https://www.example.com"
token = "<your_personal_access_token>"
sdk = GoodDataSdk.create(host, token)


# Export a particular insight in the desired format (CSV / XLSX)
sdk.export.export_tabular_by_insight_id(
workspace_id = "demo",
insight_id = "revenue",
file_format = "CSV",
# The flag use_labels ensures that names of columns are according to your model
use_labels = True,
file_name = "revenue_export.csv"
)

PDF Export 

You can use visual export if you want to export the whole dashboards in PDF format. The following code block shows how you can do it via GoodData Python SDK.

from gooddata_sdk import GoodDataSdk, ExportRequest


# GoodData base URL, e.g. "https://www.example.com"
host = "https://www.example.com"
token = "<your_personal_access_token>"
sdk = GoodDataSdk.create(host, token)

# Export a dashboard in PDF format
sdk.export.export_pdf(
workspace_id = "demo",
dashboard_id = "dashboard_overview",
file_name = "dashboard_overview_export.pdf"
)

Schedule Export

If you want to schedule your exports (for example, every morning at 8:00), you can use a schedule library and GoodData Python SDK.

host = "https://www.example.com"
token = "<your_personal_access_token>"
sdk = GoodDataSdk.create(host, token)


def export_pdf():
# Export a dashboard in PDF format
sdk.export.export_pdf(
workspace_id = "demo",
dashboard_id = "dashboard_overview",
file_name = "dashboard_overview_export.pdf"
)


# Schedule call to export dashboard in PDF every morning at 8:00.
schedule.every().day.at("8:00").do(export_pdf)


while True:
schedule.run_pending()
time.sleep(1)

Sending Export via E-mail

You can send exported data to email by utilizing smtplib, email, schedule, and GoodData Python SDK libraries.

from gooddata_sdk import GoodDataSdk
import schedule
import smtplib
from os.path import basename
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.utils import COMMASPACE, formatdate


# GoodData base URL, e.g. "https://www.example.com"
host = "https://www.example.com"
token = "<your_personal_access_token>"
sdk = GoodDataSdk.create(host, token)


def send_mail(send_from, send_to, subject, text, files, server):
msg = MIMEMultipart()
msg["From"] = send_from
msg["To"] = send_to
msg["Date"] = formatdate(localtime=True)
msg["Subject"] = subject

msg.attach(MIMEText(text))

# Open PDF file(s) and add it to email attachment
for f in files or []:
with open(f, "rb") as fil:
part = MIMEApplication(
fil.read(),
Name=basename(f)
)
part["Content-Disposition"] = "attachment; filename=\"%s\"" % basename(f)
msg.attach(part)

smtp = smtplib.SMTP(server)
smtp.sendmail(send_from, send_to, msg.as_string())
smtp.close()


def export_pdf():
# Export a dashboard in PDF format
sdk.export.export_pdf(
workspace_id = "demo",
dashboard_id = "dashboard_overview",
file_name = "dashboard_overview_export.pdf"
)

send_mail(
send_from = "your@email.com",
send_to = "to@email.com",
subject = "Scheduled export",
text = "Scheduled export of dashboard 'dashboard_overview'",
# Name of exported file from the method call 'export_pdf'
files = "dashboard_overview_export.pdf",
server = "<your_smtp_server>"
)


# Schedule call to export dashboard in PDF and sends via e-email every morning at 8:00.
schedule.every().day.at("8:00").do(export_pdf)


while True:
schedule.run_pending()
time.sleep(1)

 


0 replies

Be the first to reply!

Reply