From e0eb899d36ae2cc2f4044a942c9b91dbd1644813 Mon Sep 17 00:00:00 2001 From: Ryan Jewell <55762801+thatryan55@users.noreply.github.com> Date: Mon, 2 Feb 2026 15:34:51 -0600 Subject: [PATCH 1/2] Add files via upload Script tested here: https://platform.civisanalytics.com/spa/#/scripts/python3/340703515 --- .../dynamic-csv-export.py | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 python/email_reporting_python/dynamic-csv-export.py diff --git a/python/email_reporting_python/dynamic-csv-export.py b/python/email_reporting_python/dynamic-csv-export.py new file mode 100644 index 0000000..40602e0 --- /dev/null +++ b/python/email_reporting_python/dynamic-csv-export.py @@ -0,0 +1,87 @@ +# This script exports data from Redshift to a CSV file with a dynamically +# generated filename that includes the current date in MMDDYYYY format. +# The file is uploaded to Civis Files and a download link is included +# in the success notification email. + +import civis +import os +import json +from datetime import datetime + +# Step 1: Open a new Python script in Platform, and paste this entire file +# into the script body. +# +# Step 2: Update the database name and SQL query below to match your data. + +# Swap out name of database for your own +DATABASE = 'your-database-name' + +# Swap out the SQL query for your own +SQL = 'SELECT * FROM schema.your_table' + +# Swap out the filename prefix for your own (date will be appended automatically) +FILENAME_PREFIX = 'your_export_' + +def main(): + # Generate filename with today's date in MMDDYYYY format + # Example output: your_export_02022026.csv + today = datetime.now().strftime('%m%d%Y') + filename = f"{FILENAME_PREFIX}{today}.csv" + + # Export query results to a temporary CSV file on the script's container + # The .result() call waits for the export job to complete before continuing + local_path = f"/tmp/{filename}" + civis.io.civis_to_csv(local_path, SQL, DATABASE).result() + + # Upload the CSV to Civis Files with the dynamic filename + # This returns a file_id that can be used to retrieve the file later + file_id = civis.io.file_to_civis(local_path, name=filename) + + # Retrieve the file metadata to get the download URL + client = civis.APIClient() + file_info = client.files.get(file_id) + file_url = file_info.download_url + + # Create a dictionary with the download link + # The key 'file_link' will become the variable {{file_link}} in the email + json_value_dict = { + 'file_link': file_url + } + + # Upload the dictionary as a run output so it can be used in the email + post_json_run_output(json_value_dict) + + # Print confirmation to the logs + print(f"File uploaded with ID: {file_id}") + print(f"Filename: {filename}") + print(f"Download URL: {file_url}") + +# Step 3: Configure the notification settings. +# +# Open the Notify pane (paper airplane icon) and set the Email Body to +# include the {{file_link}} variable. For example: +# +# Download your file here: {{file_link}} +# +# The {{file_link}} variable will be replaced with the actual download URL +# when the email is sent. Note that this link expires after 30 days by default. +# +# Step 4: Run the job, schedule it, and/or receive your email with the file link. +# +# **** +# Function below will not require editing, but +# needs to be included in the script + +def post_json_run_output(json_value_dict): + client = civis.APIClient() + json_value_object = client.json_values.post( + json.dumps(json_value_dict), + name='email_outputs') + client.scripts.post_python3_runs_outputs( + os.environ['CIVIS_JOB_ID'], + os.environ['CIVIS_RUN_ID'], + 'JSONValue', + json_value_object.id) + +if __name__ == '__main__': + main() From c958d9e7b979320bdfad203af63a65d1e22ec86c Mon Sep 17 00:00:00 2001 From: lhoesly Date: Thu, 5 Feb 2026 12:12:52 -0500 Subject: [PATCH 2/2] circleci fixes --- .../email_reporting_python/dynamic-csv-export.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/python/email_reporting_python/dynamic-csv-export.py b/python/email_reporting_python/dynamic-csv-export.py index 40602e0..6b8ed12 100644 --- a/python/email_reporting_python/dynamic-csv-export.py +++ b/python/email_reporting_python/dynamic-csv-export.py @@ -22,35 +22,36 @@ # Swap out the filename prefix for your own (date will be appended automatically) FILENAME_PREFIX = 'your_export_' + def main(): # Generate filename with today's date in MMDDYYYY format # Example output: your_export_02022026.csv today = datetime.now().strftime('%m%d%Y') filename = f"{FILENAME_PREFIX}{today}.csv" - + # Export query results to a temporary CSV file on the script's container # The .result() call waits for the export job to complete before continuing local_path = f"/tmp/{filename}" civis.io.civis_to_csv(local_path, SQL, DATABASE).result() - + # Upload the CSV to Civis Files with the dynamic filename # This returns a file_id that can be used to retrieve the file later file_id = civis.io.file_to_civis(local_path, name=filename) - + # Retrieve the file metadata to get the download URL client = civis.APIClient() file_info = client.files.get(file_id) file_url = file_info.download_url - + # Create a dictionary with the download link # The key 'file_link' will become the variable {{file_link}} in the email json_value_dict = { 'file_link': file_url } - + # Upload the dictionary as a run output so it can be used in the email post_json_run_output(json_value_dict) - + # Print confirmation to the logs print(f"File uploaded with ID: {file_id}") print(f"Filename: {filename}") @@ -72,6 +73,7 @@ def main(): # Function below will not require editing, but # needs to be included in the script + def post_json_run_output(json_value_dict): client = civis.APIClient() json_value_object = client.json_values.post( @@ -83,5 +85,6 @@ def post_json_run_output(json_value_dict): 'JSONValue', json_value_object.id) + if __name__ == '__main__': main()