Create Conditional Schedules Using Looker's API/SDK

  • 14 January 2021
  • 0 replies
  • 203 views

Userlevel 4
Badge

Hi all, 

I wanted to share with you all a template to create dynamic schedules and dynamic email lists based on the line items in a Look.

We now have conditional Alerts in Looker, but what if you want a dynamic email list or conditionally send a specific piece of content to some users and another piece of content to another group based off a dynamic value or attribute?

With the API/SDK we can make this happen. Say we have a list of users and we have a binary column of if they should receive an emailed report each day. We can download the data from the Look via the run_look endpoint, then with some custom processing (I used pandas to make the manipulation a bit easier) we can dynamic send emails or even specific pieces of content to members of this list. You can run this manually or set this up with a function similar to a cronjob to completely automate the process. 
 

import looker_sdk
import csv
import pandas as pd
# from typing import cast, MutableSequence, Sequence
####Initialize API/SDK for more info go here: https://pypi.org/project/looker-sdk/
from looker_sdk import methods40, models40
sdk = looker_sdk.init40() # or init40() for v4.0 API
me = sdk.me()
## Run look and write data as a CSV
look = sdk.run_look(look_id=1995,result_format="csv")
file = open('read.csv','w')
file.write(look)
file.close()
df = pd.read_csv("read.csv")
## Filter DataFrame so it only returns Yes values for YESNO field
sends = df[df['YESNO']=="Yes"]
#create a list of emails
emails = sends['Users Email'].tolist()
i=0
while i < len(emails):
schedule = sdk.scheduled_plan_run_once(body=models40.WriteScheduledPlan(name="New Dynamic Email List Test",run_as_recipient=False, dashboard_id=857, crontab="0 0 1 * *", scheduled_plan_destination = [models40.ScheduledPlanDestination(format="csv_zip", address=emails[i], type = "email")]))
i = i + 1

 

Please let me know if you have any questions or have similar use-cases!

 

Thanks,

Eric


0 replies

Be the first to reply!

Reply